Skip to content

Commit

Permalink
correctly mark attributes as expressions and dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrsh committed May 13, 2018
1 parent 347af97 commit 72aca99
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 37 deletions.
44 changes: 26 additions & 18 deletions dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@
return index;
};

var expressionRE = /"[^"]*"|'[^']*'|\d+[a-zA-Z$_]\w*|\.[a-zA-Z$_]\w*|[a-zA-Z$_]\w*:|([a-zA-Z$_]\w*)/g;

var parseTemplate = function (expression, dependencies, locals) {
var info, dynamic = false;

while ((info = expressionRE.exec(expression)) !== null) {
var name = info[1];
if (name !== undefined && locals.indexOf(name) === -1) {
dependencies.push(name);
dynamic = true;
}
}

return dynamic;
};

var config = {
silent: ("development" === "production") || (typeof console === "undefined")
};
Expand All @@ -48,7 +64,7 @@

var whitespaceRE = /\s/;

var parseAttributes = function (index, input, length, attributes) {
var parseAttributes = function (index, input, length, attributes, dependencies, locals) {
while (index < length) {
var char = input[index];

Expand All @@ -60,7 +76,7 @@
} else {
var key = "";
var value = "";
var literal = false;
var expression = false;

while (index < length) {
char = input[index];
Expand All @@ -86,7 +102,7 @@
index += 1;
} else if (char === "{") {
quote = "}";
literal = true;
expression = true;
index += 1;
} else {
quote = whitespaceRE;
Expand All @@ -110,15 +126,16 @@
attributes.push({
key: key,
value: value,
literal: literal
expression: expression,
dynamic: parseTemplate(expression, dependencies, locals)
});
}
}

return index;
};

var parseOpeningTag = function (index, input, length, stack) {
var parseOpeningTag = function (index, input, length, stack, dependencies, locals) {
var type = "";
var attributes = [];

Expand Down Expand Up @@ -150,7 +167,7 @@
break;
} else if (whitespaceRE.test(char)) {
attributes = [];
index = parseAttributes(index + 1, input, length, attributes);
index = parseAttributes(index + 1, input, length, attributes, dependencies, locals);
} else {
type += char;
index += 1;
Expand Down Expand Up @@ -216,8 +233,6 @@
return index;
};

var expressionRE = /"[^"]*"|'[^']*'|\d+[a-zA-Z$_]\w*|\.[a-zA-Z$_]\w*|[a-zA-Z$_]\w*:|([a-zA-Z$_]\w*)/g;

var parseExpression = function (index, input, length, stack, dependencies, locals) {
var expression = "";

Expand All @@ -232,18 +247,11 @@
}
}

var info;
while ((info = expressionRE.exec(expression)) !== null) {
var name = info[1];
if (name !== undefined && locals.indexOf(name) === -1) {
dependencies.push(name);
}
}

pushChild({
index: stack.parseIndex++,
type: "m-expression",
content: expression
content: expression,
dynamic: parseTemplate(expression, dependencies, locals)
}, stack);

return index;
Expand Down Expand Up @@ -272,7 +280,7 @@
} else if (input[i + 1] === "/") {
i = parseClosingTag(i + 2, input, length, stack);
} else {
i = parseOpeningTag(i + 1, input, length, stack);
i = parseOpeningTag(i + 1, input, length, stack, dependencies, locals);
}
} else if (char === "{") {
i = parseExpression(i + 1, input, length, stack, dependencies, locals);
Expand Down
2 changes: 1 addition & 1 deletion dist/moon.min.js

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

14 changes: 3 additions & 11 deletions src/compiler/parser/expression.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { parseTemplate } from "./template";
import { pushChild } from "./util";

const expressionRE = /"[^"]*"|'[^']*'|\d+[a-zA-Z$_]\w*|\.[a-zA-Z$_]\w*|[a-zA-Z$_]\w*:|([a-zA-Z$_]\w*)/g;

export const parseExpression = (index, input, length, stack, dependencies, locals) => {
let expression = "";

Expand All @@ -16,18 +15,11 @@ export const parseExpression = (index, input, length, stack, dependencies, local
}
}

let info;
while ((info = expressionRE.exec(expression)) !== null) {
let name = info[1];
if (name !== undefined && locals.indexOf(name) === -1) {
dependencies.push(name);
}
}

pushChild({
index: stack.parseIndex++,
type: "m-expression",
content: expression
content: expression,
dynamic: parseTemplate(expression, dependencies, locals)
}, stack);

return index;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const parse = (input) => {
} else if (input[i + 1] === "/") {
i = parseClosingTag(i + 2, input, length, stack);
} else {
i = parseOpeningTag(i + 1, input, length, stack);
i = parseOpeningTag(i + 1, input, length, stack, dependencies, locals);
}
} else if (char === "{") {
i = parseExpression(i + 1, input, length, stack, dependencies, locals);
Expand Down
14 changes: 8 additions & 6 deletions src/compiler/parser/tag.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { parseTemplate } from "./template";
import { error, pushChild } from "./util";

const whitespaceRE = /\s/;

const parseAttributes = (index, input, length, attributes) => {
const parseAttributes = (index, input, length, attributes, dependencies, locals) => {
while (index < length) {
let char = input[index];

Expand All @@ -14,7 +15,7 @@ const parseAttributes = (index, input, length, attributes) => {
} else {
let key = "";
let value = "";
let literal = false;
let expression = false;

while (index < length) {
char = input[index];
Expand All @@ -40,7 +41,7 @@ const parseAttributes = (index, input, length, attributes) => {
index += 1;
} else if (char === "{") {
quote = "}";
literal = true;
expression = true;
index += 1;
} else {
quote = whitespaceRE;
Expand All @@ -64,15 +65,16 @@ const parseAttributes = (index, input, length, attributes) => {
attributes.push({
key: key,
value: value,
literal: literal
expression: expression,
dynamic: parseTemplate(expression, dependencies, locals)
});
}
}

return index;
};

export const parseOpeningTag = (index, input, length, stack) => {
export const parseOpeningTag = (index, input, length, stack, dependencies, locals) => {
let type = "";
let attributes = [];

Expand Down Expand Up @@ -104,7 +106,7 @@ export const parseOpeningTag = (index, input, length, stack) => {
break;
} else if (whitespaceRE.test(char)) {
attributes = [];
index = parseAttributes(index + 1, input, length, attributes);
index = parseAttributes(index + 1, input, length, attributes, dependencies, locals);
} else {
type += char;
index += 1;
Expand Down
15 changes: 15 additions & 0 deletions src/compiler/parser/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const expressionRE = /"[^"]*"|'[^']*'|\d+[a-zA-Z$_]\w*|\.[a-zA-Z$_]\w*|[a-zA-Z$_]\w*:|([a-zA-Z$_]\w*)/g;

export const parseTemplate = (expression, dependencies, locals) => {
let info, dynamic = false;

while ((info = expressionRE.exec(expression)) !== null) {
let name = info[1];
if (name !== undefined && locals.indexOf(name) === -1) {
dependencies.push(name);
dynamic = true;
}
}

return dynamic;
};

0 comments on commit 72aca99

Please sign in to comment.