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

View File

@ -1,6 +1,16 @@
var isValidJavaScriptVarName = require('../../compiler/util/isValidJavaScriptVarName'); var isValidJavaScriptVarName = require('../../compiler/util/isValidJavaScriptVarName');
module.exports = function nodeFactory(el, context) { 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 builder = context.builder;
var hasError = false; var hasError = false;
@ -27,4 +37,4 @@ module.exports = function nodeFactory(el, context) {
} }
return context.builder.vars(declarations); return context.builder.vars(declarations);
}; };