Skip to content

Commit

Permalink
feat: bound attributes
Browse files Browse the repository at this point in the history
(cherry picked from commit 7287b94)
  • Loading branch information
mlrawlings authored and DylanPiercey committed Oct 12, 2021
1 parent fd04465 commit 0625668
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 10 deletions.
12 changes: 11 additions & 1 deletion src/states/ATTRIBUTE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,15 @@ export const ATTRIBUTE = Parser.createState({
char(ch, code, attr) {
if (isWhitespaceCode(code)) {
return;
} else if (code === CODE.EQUAL) {
} else if (
code === CODE.EQUAL ||
(code === CODE.COLON && this.lookAtCharCodeAhead(1) === CODE.EQUAL)
) {
if (code === CODE.COLON) {
attr.bound = true;
this.skip(1);
}

// TODO: make expressions consume beginning whitespace?
this.consumeWhitespace();
this.enterState(STATE.EXPRESSION, {
Expand Down Expand Up @@ -166,9 +174,11 @@ export const ATTRIBUTE = Parser.createState({
this.enterState(STATE.EXPRESSION, {
part: "NAME",
terminatedByWhitespace: true,
skipOperators: true,
terminator: [
this.isConcise ? "]" : "/>",
this.isConcise ? ";" : ">",
":=",
"=",
",",
"("
Expand Down
3 changes: 1 addition & 2 deletions src/states/EXPRESSION.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,10 @@ export const EXPRESSION = Parser.createState({

char(ch, code, expression) {
let depth = expression.groupStack.length;
let parentState = expression.parentState;

if (depth === 0) {
if (expression.terminatedByWhitespace && isWhitespaceCode(code)) {
var operator = this.checkForOperator();
var operator = !expression.skipOperators && this.checkForOperator();

if (operator) {
expression.isStringLiteral = false;
Expand Down
10 changes: 7 additions & 3 deletions src/states/OPEN_TAG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,17 @@ export const OPEN_TAG = Parser.createState({
if (nextCharCode === CODE.PERIOD || nextCharCode === CODE.NUMBER_SIGN) {
this.enterState(STATE.EXPRESSION, {
part: "NAME",
skipOperators: true,
terminatedByWhitespace: true,
terminator: [
this.isConcise ? ";" : ">",
"/",
"(",
"|",
".",
"#"
"#",
"=",
":="
]
});
}
Expand Down Expand Up @@ -394,10 +397,11 @@ export const OPEN_TAG = Parser.createState({
} else if (code === CODE.FORWARD_SLASH && !this.currentOpenTag.attributes.length) {
this.enterState(STATE.EXPRESSION, {
part: "VARIABLE",
skipOperators: true,
terminatedByWhitespace: true,
terminator: this.isConcise
? [";", "(", "|", "="]
: [">", "/>", "(", "|", "="]
? [";", "(", "|", "=", ":="]
: [">", "/>", "(", "|", "=", ":="]
});
} else if (code === CODE.OPEN_PAREN && !this.currentOpenTag.attributes.length) {
if (this.currentOpenTag.argument != null) {
Expand Down
12 changes: 11 additions & 1 deletion src/states/TAG_NAME.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,17 @@ export const TAG_NAME = Parser.createState({

tagName.text += nextCh;
tagName.rawValue += ch + nextCh;
} else if (isWhitespaceCode(code) || code === CODE.EQUAL || code === CODE.OPEN_PAREN || code === CODE.FORWARD_SLASH || code === CODE.PIPE || (this.isConcise ? code === CODE.SEMICOLON : code === CODE.CLOSE_ANGLE_BRACKET)) {
} else if (
isWhitespaceCode(code) ||
code === CODE.EQUAL ||
(code === CODE.COLON && this.lookAtCharCodeAhead(1) === CODE.EQUAL) ||
code === CODE.OPEN_PAREN ||
code === CODE.FORWARD_SLASH ||
code === CODE.PIPE ||
(this.isConcise
? code === CODE.SEMICOLON
: code === CODE.CLOSE_ANGLE_BRACKET)
) {
this.exitState();
} else if (code === CODE.PERIOD || code === CODE.NUMBER_SIGN) {
if (!tagName.shorthandCharCode) {
Expand Down
1 change: 1 addition & 0 deletions src/util/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export enum CODE {
PERCENT = 37,
PERIOD = 46,
COMMA = 44,
COLON = 58,
SEMICOLON = 59,
NUMBER_SIGN = 35,
PIPE = 124,
Expand Down
1 change: 1 addition & 0 deletions src/util/notify-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export function createNotifiers(parser, listeners) {
endPos: attr.endPos,
argument: attr.argument,
method: attr.method,
bound: attr.bound,
};

if (attr.hasOwnProperty("literalValue")) {
Expand Down
23 changes: 23 additions & 0 deletions test/autotest/attr-bound/expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<a x=BOUND(foo)>
text:"test"
</a>
text:"\n"
<a x=BOUND(foo)>
text:"test"
</a>
text:"\n"
<a DEFAULT=BOUND(foo)>
text:"test"
</a>
text:"\n"
<a DEFAULT=BOUND(foo)>
text:"test"
</a>
text:"\n"
<a VAR=(bar) DEFAULT=BOUND(foo)>
text:"test"
</a>
text:"\n"
<a VAR=(bar) DEFAULT=BOUND(foo)>
text:"test"
</a>
6 changes: 6 additions & 0 deletions test/autotest/attr-bound/input.htmljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<a x:=foo>test</a>
<a x := foo>test</a>
<a:=foo>test</a>
<a := foo>test</a>
<a/bar:=foo>test</a>
<a/bar := foo>test</a>
2 changes: 1 addition & 1 deletion test/autotest/tag-var-declaration/expected.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<let VAR=(foo) DEFAULT=1 SELF_CLOSED>
</let>
text:"\n"
<let VAR=(foo + 1) SELF_CLOSED>
<let VAR=(foo) +=(EMPTY) 1=(EMPTY) SELF_CLOSED>
</let>
text:"\n"
<let VAR=({ x, y }) SELF_CLOSED>
Expand Down
7 changes: 5 additions & 2 deletions test/util/TreeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ function attributeAssignmentToString(attr, includeLiteralValues) {
var result = "";

if (attr.value) {
result += '=' + attr.value;
if (attr.bound) {
result += '=BOUND(' + attr.value + ")";
} else {
result += '=' + attr.value;
}
} else if (!attr.argument) {
result += '=(EMPTY)';
}
Expand Down Expand Up @@ -285,7 +289,6 @@ class TreeBuilder {
if (event.concise) {
expect(src.substring(startPos, startPos + tagName.length)).to.equal(tagName);
} else {
debugger;
expect(src.substring(startPos, startPos + 1 + tagName.length)).to.equal('<' + tagName);

if (event.selfClosed) {
Expand Down

0 comments on commit 0625668

Please sign in to comment.