Skip to content

Commit

Permalink
fix: avoid namespace pollution
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes committed Oct 27, 2017
1 parent bea042a commit 40a4590
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 46 deletions.
7 changes: 3 additions & 4 deletions test/utils.test.js
Expand Up @@ -31,14 +31,13 @@ const template = `

describe('utils', () => {
it('#astTreeToString', () => {
const ast = utils.parseTemplate(template);
utils.postprocess = (frag, ast) => {
const astTree = utils.parseTemplate(template);
assert(utils.astTreeToString(astTree, (frag, ast) => {
if (ast.tag === 'block' && ast.text === 'body') {
return frag.replace(ast.innerText, ast.innerText + '<123123>');
} else {
return frag;
}
};
assert(utils.astTreeToString(ast).includes('<123123>{% endblock %}'))
}).includes('<123123>{% endblock %}'))
});
});
83 changes: 41 additions & 42 deletions utils.js
Expand Up @@ -5,44 +5,26 @@ const constant = require('node-mus/lib/compile/constant');
const regexEscapeRE = /[-.*+?^${}()|[\]\/\\]/g;

module.exports = {
preprocess: {
comment() {},
pagelet() {},
require(el) { el.isUnary = true; },
datalet(el) { el.isUnary = true; },
html() {},
head() {},
body() {},
title() {},
for() {},
uri(el) { el.isUnary = true; },
script() {},
variable() {},
ATF(el) { el.isUnary = true; },
macro() {},
extends(el) { el.isUnary = true; },
block() {},
include(el) { el.isUnary = true; },
import(el) { el.isUnary = true; },
autoescape() {},
verbatim() {},
call() {},
parent(el) { el.isUnary = true; },
spaceless() {},
},
postprocess: text => text,

astTreeToString(astTree) {
astTreeToString(astTree, process) {
let html = '';
if (!astTree) {
return html;
}

const astNodeToString = ast => {
let html = `${ast._ast.blockStart} ${ast.tag}`;
const expr = ast.text.trim();
html += expr ? ` ${expr}` : '';
html += ' ' + ast._ast.blockEnd;
ast.innerText = this.astTreeToString(ast.children, process);
return html + ast.innerText;
};

astTree.forEach(ast => {
let fragment = '';
switch (ast.type) {
case constant.TYPE_TAG:
fragment += this.astNodeToString(ast);
fragment += astNodeToString(ast);
break;
case constant.TYPE_TEXT:
fragment += ast.text;
Expand All @@ -56,38 +38,55 @@ module.exports = {

if (ast.elseifBlock) {
ast.elseifBlock.forEach(_a => {
fragment += this.astNodeToString(_a);
fragment += astNodeToString(_a);
});
}

if (ast.elseBlock) {
fragment += this.astNodeToString(ast.elseBlock);
fragment += astNodeToString(ast.elseBlock);
}

if (!ast.isUnary && ast.type === constant.TYPE_TAG) {
fragment += `${ast._ast.blockStart} end${ast.tag} ${ast._ast.blockEnd}`;
}

html += this.postprocess(fragment, ast);
html += process
? process(fragment, ast)
: fragment;
});

return html;
},

astNodeToString(ast) {
let html = `${ast._ast.blockStart} ${ast.tag}`;
const expr = ast.text.trim();
html += expr ? ` ${expr}` : '';
html += ' ' + ast._ast.blockEnd;
ast.innerText = this.astTreeToString(ast.children);
return html + ast.innerText;
},

parseTemplate(str, options = {}) {
// parse template by node-mus
/* istanbul ignore next */
return ast(str, Object.assign(options, {
processor: Object.assign({}, this.preprocess, options.processor),
processor: Object.assign({
comment() {},
pagelet() {},
require(el) { el.isUnary = true; },
datalet(el) { el.isUnary = true; },
html() {},
head() {},
body() {},
title() {},
for() {},
uri(el) { el.isUnary = true; },
script() {},
variable() {},
ATF(el) { el.isUnary = true; },
macro() {},
extends(el) { el.isUnary = true; },
block() {},
include(el) { el.isUnary = true; },
import(el) { el.isUnary = true; },
autoescape() {},
verbatim() {},
call() {},
parent(el) { el.isUnary = true; },
spaceless() {},
}, options.processor),
})).root;
},

Expand Down

0 comments on commit 40a4590

Please sign in to comment.