Use tagString for handling the var tag

This commit is contained in:
Patrick Steele-Idem 2017-01-18 16:25:46 -07:00
parent 9e4a638c51
commit 05028dc02c
4 changed files with 69 additions and 19 deletions

View File

@ -195,19 +195,19 @@ class Parser {
this.prevTextNode = null;
var tagDef = el.tagName ? this.context.getTagDef(el.tagName) : null;
var attributeParseErrors = [];
// <div class="foo"> -> "div class=foo"
var tagString = parser.substring(el.pos, el.endPos)
.replace(/<|\/>|>/g, "").trim();
.replace(/^<|\/>$|>$/g, "").trim();
var elDef = {
tagName: tagName,
argument: argument,
tagString,
openTagOnly: el.openTagOnly === true,
selfClosed: el.selfClosed === true,
pos: el.pos,
attributes: attributes.map((attr) => {
var shouldParsedAttributes = !tagDef || tagDef.parseAttributes !== false;
var parsedAttributes = [];
if (shouldParsedAttributes) {
attributes.forEach((attr) => {
var attrValue;
if (attr.hasOwnProperty('literalValue')) {
attrValue = builder.literal(attr.literalValue);
@ -219,8 +219,14 @@ class Parser {
try {
parsedExpression = builder.parseExpression(attr.value);
} catch(e) {
valid = false;
attributeParseErrors.push('Invalid JavaScript expression for attribute "' + attr.name + '": ' + e);
if (shouldParsedAttributes) {
valid = false;
attributeParseErrors.push('Invalid JavaScript expression for attribute "' + attr.name + '": ' + e);
} else {
// Attribute failed to parse. Skip it...
return;
}
}
if (valid) {
@ -245,8 +251,18 @@ class Parser {
attrDef.argument = attr.argument.value;
}
return attrDef;
})
parsedAttributes.push(attrDef);
});
}
var elDef = {
tagName: tagName,
argument: argument,
tagString,
openTagOnly: el.openTagOnly === true,
selfClosed: el.selfClosed === true,
pos: el.pos,
attributes: parsedAttributes
};
var node;
@ -254,9 +270,6 @@ class Parser {
if (raw) {
node = builder.htmlElement(elDef);
node.pos = elDef.pos;
let taglibLookup = this.context.taglibLookup;
let tagDef = taglibLookup.getTag(tagName);
node.tagDef = tagDef;
} else {
node = this.context.createNodeForEl(elDef);
@ -409,4 +422,4 @@ class Parser {
}
}
module.exports = Parser;
module.exports = Parser;

View File

@ -612,6 +612,10 @@ class TagLoader {
deprecated(value) {
this.tag.deprecated = value;
}
parseAttributes(value) {
this.tag.parseAttributes = value;
}
}
function isSupportedProperty(name) {
@ -636,4 +640,4 @@ function loadTag(tagProps, filePath, dependencyChain) {
}
exports.loadTag = loadTag;
exports.isSupportedProperty = isSupportedProperty;
exports.isSupportedProperty = isSupportedProperty;

View File

@ -213,6 +213,29 @@ function parseExpression(src, builder, isExpression) {
return builder.updateExpression(argument, node.operator, node.prefix);
}
case 'VariableDeclarator': {
var id = convert(node.id);
if (!id) {
return null;
}
var init;
if (node.init) {
init = convert(node.init);
if (!init) {
return null;
}
}
return builder.variableDeclarator(id, init);
}
case 'VariableDeclaration': {
var kind = node.kind;
var declarations = convert(node.declarations);
return builder.vars(declarations, kind);
}
default:
return null;
}

View File

@ -1,6 +1,16 @@
var isValidJavaScriptVarName = require('../../compiler/util/isValidJavaScriptVarName');
module.exports = function nodeFactory(el, context) {
var vars;
try {
vars = context.builder.parseStatement(el.tagString);
} catch(e) {}
if (vars) {
return vars;
}
var builder = context.builder;
var hasError = false;
@ -27,4 +37,4 @@ module.exports = function nodeFactory(el, context) {
}
return context.builder.vars(declarations);
};
};