Skip to content

Commit

Permalink
Add raw tag to handle things that aren't proper xml
Browse files Browse the repository at this point in the history
  • Loading branch information
kball committed Jun 17, 2016
1 parent 5f4cb49 commit dbbc3c8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
29 changes: 27 additions & 2 deletions lib/inky.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ Inky.prototype.releaseTheKraken = function(xmlString, cheerioOpts) {
// <center> is an exception: the selector is center:not([data-parsed])
// Otherwise the parser gets caught in an infinite loop where it continually tries to process the same <center> tags
//
var $ = cheerio.load(xmlString, Inky.mergeCheerioOpts(cheerioOpts));
var set = Inky.extractRaws(xmlString);
var raws = set[0], string = set[1];
var $ = cheerio.load(string, Inky.mergeCheerioOpts(cheerioOpts));
var tags = this.componentTags.map(function(tag) {
if (tag == 'center') {
return tag + ':not([data-parsed])';
Expand All @@ -59,7 +61,8 @@ Inky.prototype.releaseTheKraken = function(xmlString, cheerioOpts) {
elem.replaceWith(newHtml);
}

return $.html();
string = $.html();
return Inky.reInjectRaws(string, raws);
}

Inky.mergeCheerioOpts = function(opts) {
Expand All @@ -70,6 +73,28 @@ Inky.mergeCheerioOpts = function(opts) {
return opts;
};

Inky.extractRaws = function(string) {
var raws = [];
var i = 0;
var raw;
var str = string
var regex = /\< *raw *\>(.*?)\<\/ *raw *\>/i;
while(raw = str.match(regex)) {
raws[i] = raw[1];
str = str.replace(regex, '###RAW' + i + '###');
i = i+1;
}
return [raws, str];
};

Inky.reInjectRaws = function(string, raws) {
var str = string;
for (var i in raws) {
str = str.replace('###RAW' + i + '###', raws[i])
}
return str;
};

Inky.prototype.componentFactory = require('./componentFactory');

Inky.prototype.makeColumn = require('./makeColumn');
14 changes: 14 additions & 0 deletions test/inky.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ describe('Inky', () => {
compare(input, expected, { decodeEntities: false });
});

it(`doesn't muck with stuff inside raw`, () => {
var input = '<raw><%= test %></raw>';
var expected = '<%= test %>';

compare(input, expected);
});

it(`can handle multiple raw tags`, () => {
var input = '<h1><raw><%= test %></raw></h1><h2>< raw >!!!</ raw ></h2>';
var expected = '<h1><%= test %></h1><h2>!!!</h2>';

compare(input, expected);
});

});

describe('Inky wrappers', () => {
Expand Down

0 comments on commit dbbc3c8

Please sign in to comment.