Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/@glimmer/syntax/lib/parser/tokenizer-event-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ function validateEndTag(
// <input> or <br />, so we need to check for that here. Otherwise, we would
// throw an error for those cases.
error = 'Invalid end tag ' + formatEndTagInfo(tag) + ' (void elements cannot have end tags).';
} else if (mustacheInClosingTag(tag, element)) {
error =
'Invalid end tag ' + formatEndTagInfo(tag) + '. (closing tags cannot contain modifiers).';
} else if (element.tag === undefined) {
error = 'Closing tag ' + formatEndTagInfo(tag) + ' without an open tag.';
} else if (element.tag !== tag.name) {
Expand All @@ -320,6 +323,28 @@ function validateEndTag(
}
}

function mustacheInClosingTag(tag: Tag<'StartTag' | 'EndTag'>, element: AST.ElementNode) {
let children = element.children || [];
return children.some(({ type, loc: { start, end } }) => {
if (
type === 'MustacheStatement' &&
start.line >= tag.loc.start.line &&
end.line <= tag.loc.end.line
) {
if (start.line === tag.loc.start.line) {
return start.column > tag.loc.start.column;
}

if (end.line === tag.loc.end.line) {
return end.column < tag.loc.start.column;
}

return true;
}
return false;
});
}

function formatEndTagInfo(tag: Tag<'StartTag' | 'EndTag'>) {
return '`' + tag.name + '` (on line ' + tag.loc.end.line + ')';
}
Expand Down
14 changes: 14 additions & 0 deletions packages/@glimmer/syntax/test/parser-node-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,20 @@ test('disallowed mustaches in the tagName space', function(assert) {
}, /Cannot use mustaches in an elements tagname: `{{bar` at L1:C6/);
});

test('disallowed mustaches in closing tag', function(assert) {
assert.throws(() => {
parse('\nbefore <div></ {{some-modifier}} div>');
}, 'Error: Invalid end tag `div` (on line 2). (closing tags cannot contain modifiers).');

assert.throws(() => {
parse('\nbefore <div></div {{some-modifier}}>');
}, 'Error: Invalid end tag `div` (on line 2). (closing tags cannot contain modifiers).');

assert.throws(() => {
parse('\nbefore <div><span></span {{some-modifier}}></div>');
}, 'Error: Invalid end tag `div` (on line 2). (closing tags cannot contain modifiers).');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should error point into span instead of div?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lifeart Thanks for this! You're absolutely right. This comment made me realize my tests weren't actually testing correctly, which lead me down the path the determining how to solve this whole issue much more elegantly.

});

test('mustache immediately followed by self closing tag does not error', function() {
let ast = parse('<FooBar data-foo={{blah}}/>');
let element = b.element('FooBar/', ['attrs', ['data-foo', b.mustache('blah')]]);
Expand Down