Skip to content

Commit

Permalink
scope attribute and text expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Apr 22, 2019
1 parent 9e5f907 commit 4008d25
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
18 changes: 14 additions & 4 deletions packages/moon/dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,25 @@
*/

var globals = ["NaN", "false", "in", "null", "this", "true", "typeof", "undefined", "window"];
/**
* Scope an expression to use variables within the `data` object.
*
* @param {string} expression
*/

function scopeExpression(expression) {
return expression.replace(expressionRE, function (match, name) {
return name === undefined || globals.indexOf(name) !== -1 ? match : "data." + name;
});
}
/**
* Convert a token into a string, accounting for `<text/>` components.
*
* @param {Object} token
* @returns {String} Token converted into a string
*/


function tokenString(token) {
if (token.type === "tagOpen") {
if (token.value === "text") {
Expand Down Expand Up @@ -255,7 +267,7 @@
} else {
// Store the key/value pair using the matched value or
// expression.
attributes[attributeKey] = attributeExpression === undefined ? attributeValue : attributeExpression;
attributes[attributeKey] = attributeExpression === undefined ? attributeValue : scopeExpression(attributeExpression);
}
} // Append an opening tag token with the type, attributes, and optional
// self-closing slash.
Expand Down Expand Up @@ -289,9 +301,7 @@
type: "tagOpen",
value: "text",
attributes: {
"": expression.replace(expressionRE, function (match, name) {
return name === undefined || globals.indexOf(name) !== -1 ? match : "data." + name;
})
"": scopeExpression(expression)
},
closed: true
});
Expand Down
2 changes: 1 addition & 1 deletion packages/moon/dist/moon.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 15 additions & 6 deletions packages/moon/src/compiler/lexer/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ const expressionRE = /"[^"]*"|'[^']*'|\d+[a-zA-Z$_]\w*|\.[a-zA-Z$_]\w*|[a-zA-Z$_
*/
const globals = ["NaN", "false", "in", "null", "this", "true", "typeof", "undefined", "window"];

/**
* Scope an expression to use variables within the `data` object.
*
* @param {string} expression
*/
function scopeExpression(expression) {
return expression.replace(expressionRE, (match, name) =>
(name === undefined || globals.indexOf(name) !== -1) ?
match :
`data.${name}`
);
}

/**
* Convert a token into a string, accounting for `<text/>` components.
*
Expand Down Expand Up @@ -200,7 +213,7 @@ export function lex(input) {
attributes[attributeKey] =
attributeExpression === undefined ?
attributeValue :
attributeExpression;
scopeExpression(attributeExpression);
}
}

Expand Down Expand Up @@ -236,11 +249,7 @@ export function lex(input) {
type: "tagOpen",
value: "text",
attributes: {
"": expression.replace(expressionRE, (match, name) =>
(name === undefined || globals.indexOf(name) !== -1) ?
match :
`data.${name}`
)
"": scopeExpression(expression)
},
closed: true
});
Expand Down

0 comments on commit 4008d25

Please sign in to comment.