Skip to content

Commit

Permalink
add expression scoping to data
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed Apr 18, 2019
1 parent ef8ecc9 commit 0af6175
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
15 changes: 14 additions & 1 deletion packages/moon/dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@
*/

var attributeRE = /\s*([\w\d-_:@]*)(?:=(?:("[^"]*"|'[^']*')|{([^{}]*)}))?/g;
/**
* Capture the variables in expressions to scope them within the data
* parameter. This ignores property names and deep object accesses.
*/

var expressionRE = /"[^"]*"|'[^']*'|\d+[a-zA-Z$_]\w*|\.[a-zA-Z$_]\w*|[a-zA-Z$_]\w*:|([a-zA-Z$_]\w*)/g;
/**
* List of global variables to ignore in expression scoping
*/

var globals = ["NaN", "false", "in", "null", "this", "true", "typeof", "undefined", "window"];
/**
* Convert a token into a string, accounting for `<text/>` components.
*
Expand Down Expand Up @@ -278,7 +289,9 @@
type: "tagOpen",
value: "text",
attributes: {
"": expression
"": expression.replace(expressionRE, function (match, name) {
return name === undefined || globals.indexOf(name) !== -1 ? match : "data." + name;
})
},
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: 18 additions & 3 deletions packages/moon/src/compiler/lexer/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ const typeRE = /<([\w\d-_]+)([^>]*?)(\/?)>/g;
*/
const attributeRE = /\s*([\w\d-_:@]*)(?:=(?:("[^"]*"|'[^']*')|{([^{}]*)}))?/g;

/**
* Capture the variables in expressions to scope them within the data
* parameter. This ignores property names and deep object accesses.
*/
const expressionRE = /"[^"]*"|'[^']*'|\d+[a-zA-Z$_]\w*|\.[a-zA-Z$_]\w*|[a-zA-Z$_]\w*:|([a-zA-Z$_]\w*)/g;

/**
* List of global variables to ignore in expression scoping
*/
const globals = ["NaN", "false", "in", "null", "this", "true", "typeof", "undefined", "window"];

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

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

0 comments on commit 0af6175

Please sign in to comment.