Skip to content

Commit

Permalink
feat(parser): support for inline strings, comments
Browse files Browse the repository at this point in the history
  • Loading branch information
voischev committed Aug 12, 2015
1 parent b0d147a commit e7f4083
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,28 @@ class Convert {

let naming = bemNaming(options),
bufArray = [],
results = [],
doctype;
results = [];

bufArray.last = function() {
return this[this.length - 1];
};

let parser = new htmlparser.Parser({
onprocessinginstruction: function(name, data) {
name === '!doctype' && results.push(`<${data}>`);
},
oncomment: function(data) {
let comment = `<!-- ${data.trim()} -->`,
last = bufArray.last();
if (!last) {
results.push(comment);
return;
}
last.content || (last.content = []);
last.content.push(comment);
},
onopentag: function(tag, attrs) {
var buf = {},
let buf = {},
classes = attrs.class && attrs.class.split(' '),
block = classes && naming.parse(classes.shift()),
i;
Expand All @@ -29,7 +41,7 @@ class Convert {

if (classes && classes.length) {
classes.map(naming.parse, naming).forEach(entity => {
var modFieldName = entity.elem ? 'elemMods' : 'mods';
let modFieldName = entity.elem ? 'elemMods' : 'mods';

if (
entity.block === buf.block &&
Expand All @@ -40,11 +52,11 @@ class Convert {
buf[modFieldName][entity.modName] = entity.modVal;
} else { // build mixes
if (entity.modName) {
var mixes = buf.mix,
let mixes = buf.mix,
currentMixingItem;

if (mixes) {
for (var i = 0; i < buf.mix.length; i++) {
for (let i = 0; i < buf.mix.length; i++) {
if ((mixes[i].block === entity.block) && mixes[i].elem === entity.elem) {
currentMixingItem = mixes[i];
}
Expand Down Expand Up @@ -114,40 +126,40 @@ class Convert {
bufArray.push(buf);
},
onclosetag: function() {
var buf = bufArray.pop();
let buf = bufArray.pop();

if (bufArray.length === 0) {
results.push(buf);

return;
}

var last = bufArray.last();
let last = bufArray.last();
if (!(last.content instanceof Array)) {
last.content = [];
}
last.content.push(buf);
},
ontext: function(text) {
if (text.match(/(^\n|^\n\s+$)/g)) return;
if (text.match(/(^[\s\n]+$)/g)) return;

var last = bufArray.last();
if (!last) return;
text = text.trim();

let last = bufArray.last();
if (!last) {
results.push(text);
return;
}

last.content || (last.content = []);
last.content.push(text);
},
onprocessinginstruction: function(name, data) {
if (name === '!doctype') {
doctype = `<${data}>`;
}
}
});

parser.write(html);
parser.end();

return doctype ? [].concat(doctype, results) : results;
return results;
}

}
Expand Down

0 comments on commit e7f4083

Please sign in to comment.