Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat <title>, <script>, and <style> contents as text #69

Merged
merged 4 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
37 changes: 33 additions & 4 deletions src/evented-tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class EventedTokenizer {
reset() {
this.transitionTo(TokenizerState.beforeData);
this.input = '';
this.tagNameBuffer = '';

this.index = 0;
this.line = 1;
Expand Down Expand Up @@ -113,21 +114,31 @@ export default class EventedTokenizer {
this.delegate.appendToTagName(char);
}

private isIgnoredEndTag(): boolean {
let tag = this.tagNameBuffer.toLowerCase();

return (tag === 'title' && this.input.substr(this.index, 8) !== '</title>') ||
CvX marked this conversation as resolved.
Show resolved Hide resolved
(tag === 'style' && this.input.substr(this.index, 8) !== '</style>') ||
(tag === 'script' && this.input.substr(this.index, 9) !== '</script>');
}

states: { [k in TokenizerState]?: (this: EventedTokenizer) => void } = {
beforeData() {
let char = this.peek();

if (char === '<') {
if (char === '<' && !this.isIgnoredEndTag()) {
this.transitionTo(TokenizerState.tagOpen);
this.markTagStart();
this.consume();
} else {
if (char === '\n') {
let tag = this.tagNameBuffer.toLowerCase();

if (tag === 'pre' || tag === 'textarea') {
this.consume();
}
}

this.transitionTo(TokenizerState.data);
this.delegate.beginData();
}
Expand All @@ -136,7 +147,7 @@ export default class EventedTokenizer {
data() {
let char = this.peek();

if (char === '<') {
if (char === '<' && !this.isIgnoredEndTag()) {
this.delegate.finishData();
this.transitionTo(TokenizerState.tagOpen);
this.markTagStart();
Expand Down Expand Up @@ -168,7 +179,7 @@ export default class EventedTokenizer {
markupDeclarationOpen() {
let char = this.consume();

if (char === '-' && this.input.charAt(this.index) === '-') {
if (char === '-' && this.peek() === '-') {
this.consume();
this.transitionTo(TokenizerState.commentStart);
this.delegate.beginComment();
Expand Down Expand Up @@ -251,6 +262,24 @@ export default class EventedTokenizer {
}
},

endTagName() {
let char = this.consume();

if (isSpace(char)) {
this.transitionTo(TokenizerState.beforeAttributeName);
this.tagNameBuffer = '';
} else if (char === '/') {
this.transitionTo(TokenizerState.selfClosingStartTag);
this.tagNameBuffer = '';
} else if (char === '>') {
this.delegate.finishTag();
this.transitionTo(TokenizerState.beforeData);
this.tagNameBuffer = '';
} else {
this.appendToTagName(char);
}
},

beforeAttributeName() {
let char = this.peek();

Expand Down Expand Up @@ -453,7 +482,7 @@ export default class EventedTokenizer {
let char = this.consume();

if (char === '@' || char === ':' || isAlpha(char)) {
this.transitionTo(TokenizerState.tagName);
this.transitionTo(TokenizerState.endTagName);
this.tagNameBuffer = '';
this.delegate.beginEndTag();
this.appendToTagName(char);
Expand Down
1 change: 1 addition & 0 deletions src/generated/tokenizer-states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const enum TokenizerState {
tagOpen = 'tagOpen',
endTagOpen = 'endTagOpen',
tagName = 'tagName',
endTagName = 'endTagName',
rcdataLessThanSign = 'rcdataLessThanSign',
rcdataEndTagOpen = 'rcdataEndTagOpen',
rcdataEndTagName = 'rcdataEndTagName',
Expand Down
37 changes: 37 additions & 0 deletions tests/tokenizer-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ QUnit.test('A newline immediately following a <pre> tag is stripped', function(a
assert.deepEqual(tokens, [startTag('pre'), chars('hello'), endTag('pre')]);
});

QUnit.test('A newline immediately following a closing </pre> tag is not stripped', function(assert) {
let tokens = tokenize("\n<pre>\nhello</pre>\n");
assert.deepEqual(tokens, [chars('\n'), startTag('pre'), chars('hello'), endTag('pre'), chars('\n')]);
});

// https://html.spec.whatwg.org/multipage/syntax.html#element-restrictions
QUnit.test('A newline immediately following a <PRE> tag is stripped', function(assert) {
let tokens = tokenize("<PRE>\nhello</PRE>");
Expand All @@ -217,6 +222,38 @@ QUnit.test('A newline immediately following a <textarea> tag is stripped', funct
assert.deepEqual(tokens, [startTag('textarea'), chars('hello'), endTag('textarea')]);
});

// https://html.spec.whatwg.org/multipage/semantics.html#the-title-element
QUnit.test('The title element content is always text', function(assert) {
let tokens = tokenize("<title>&quot;hey <b>there</b><!-- comment --></title>");
assert.deepEqual(tokens, [startTag('title'), chars('"hey <b>there</b><!-- comment -->'), endTag('title')]);
});

// https://html.spec.whatwg.org/multipage/semantics.html#the-style-element
QUnit.test('The style element content is always text', function(assert) {
let tokens = tokenize("<style>&quot;hey <b>there</b><!-- comment --></style>");
assert.deepEqual(tokens, [startTag('style'), chars('"hey <b>there</b><!-- comment -->'), endTag('style')]);
});

// https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
QUnit.test('The script element content restrictions', function(assert) {
let tokens = tokenize("<script>&quot;hey <b>there</b><!-- comment --></script>");
assert.deepEqual(tokens, [startTag('script'), chars('"hey <b>there</b><!-- comment -->'), endTag('script')]);
CvX marked this conversation as resolved.
Show resolved Hide resolved
});

QUnit.test('Two following script tags', function(assert) {
let tokens = tokenize("<script><!-- comment --></script> <script>second</script>");

assert.deepEqual(tokens, [
startTag('script'),
chars('<!-- comment -->'),
endTag('script'),
chars(' '),
startTag('script'),
chars('second'),
endTag('script')
]);
});

// https://github.com/emberjs/rfcs/blob/master/text/0311-angle-bracket-invocation.md#dynamic-invocations
QUnit.test('An Emberish named arg invocation', function(assert) {
let tokens = tokenize('<@foo></@foo>');
Expand Down