Close multiple PRs by vicb · Pull Request #5688 · angular/angular · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
70 changes: 57 additions & 13 deletions modules/angular2/src/compiler/html_lexer.ts
13 changes: 12 additions & 1 deletion modules/angular2/src/compiler/html_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,18 @@ class TreeBuilder {
}

private _consumeText(token: HtmlToken) {
this._addToParent(new HtmlTextAst(token.parts[0], token.sourceSpan));
let text = token.parts[0];
if (text.length > 0 && text[0] == '\n') {
let parent = this._getParentElement();
if (isPresent(parent) && parent.children.length == 0 &&
getHtmlTagDefinition(parent.name).ignoreFirstLf) {
text = text.substring(1);
}
}

if (text.length > 0) {
this._addToParent(new HtmlTextAst(text, token.sourceSpan));
}
}

private _closeVoidElement(): void {
Expand Down
24 changes: 19 additions & 5 deletions modules/angular2/src/compiler/html_tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,17 @@ export class HtmlTagDefinition {
public implicitNamespacePrefix: string;
public contentType: HtmlTagContentType;
public isVoid: boolean;
public ignoreFirstLf: boolean;

constructor({closedByChildren, requiredParents, implicitNamespacePrefix, contentType,
closedByParent, isVoid}: {
closedByParent, isVoid, ignoreFirstLf}: {
closedByChildren?: string[],
closedByParent?: boolean,
requiredParents?: string[],
implicitNamespacePrefix?: string,
contentType?: HtmlTagContentType,
isVoid?: boolean
isVoid?: boolean,
ignoreFirstLf?: boolean
} = {}) {
if (isPresent(closedByChildren) && closedByChildren.length > 0) {
closedByChildren.forEach(tagName => this.closedByChildren[tagName] = true);
Expand All @@ -301,11 +303,20 @@ export class HtmlTagDefinition {
}
this.implicitNamespacePrefix = implicitNamespacePrefix;
this.contentType = isPresent(contentType) ? contentType : HtmlTagContentType.PARSABLE_DATA;
this.ignoreFirstLf = normalizeBool(ignoreFirstLf);
}

requireExtraParent(currentParent: string): boolean {
return isPresent(this.requiredParents) &&
(isBlank(currentParent) || this.requiredParents[currentParent.toLowerCase()] != true);
if (isBlank(this.requiredParents)) {
return false;
}

if (isBlank(currentParent)) {
return true;
}

let lcParent = currentParent.toLowerCase();
return this.requiredParents[lcParent] != true && lcParent != 'template';
}

isClosedByChild(name: string): boolean {
Expand Down Expand Up @@ -380,10 +391,13 @@ var TAG_DEFINITIONS: {[key: string]: HtmlTagDefinition} = {
'rp': new HtmlTagDefinition({closedByChildren: ['rb', 'rt', 'rtc', 'rp'], closedByParent: true}),
'optgroup': new HtmlTagDefinition({closedByChildren: ['optgroup'], closedByParent: true}),
'option': new HtmlTagDefinition({closedByChildren: ['option', 'optgroup'], closedByParent: true}),
'pre': new HtmlTagDefinition({ignoreFirstLf: true}),
'listing': new HtmlTagDefinition({ignoreFirstLf: true}),
'style': new HtmlTagDefinition({contentType: HtmlTagContentType.RAW_TEXT}),
'script': new HtmlTagDefinition({contentType: HtmlTagContentType.RAW_TEXT}),
'title': new HtmlTagDefinition({contentType: HtmlTagContentType.ESCAPABLE_RAW_TEXT}),
'textarea': new HtmlTagDefinition({contentType: HtmlTagContentType.ESCAPABLE_RAW_TEXT}),
'textarea': new HtmlTagDefinition(
{contentType: HtmlTagContentType.ESCAPABLE_RAW_TEXT, ignoreFirstLf: true}),
};

var DEFAULT_TAG_DEFINITION = new HtmlTagDefinition();
Expand Down
60 changes: 35 additions & 25 deletions modules/angular2/test/compiler/html_lexer_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ export function main() {
]);
});

it('should allow whitespace', () => {
expect(tokenizeAndHumanizeParts('< test >'))
it('should allow whitespace after the tag name', () => {
expect(tokenizeAndHumanizeParts('<test >'))
.toEqual([
[HtmlTokenType.TAG_OPEN_START, null, 'test'],
[HtmlTokenType.TAG_OPEN_END],
Expand All @@ -192,15 +192,6 @@ export function main() {
]);
});

it('should report missing name after <', () => {
expect(tokenizeAndHumanizeErrors('<'))
.toEqual([[HtmlTokenType.TAG_OPEN_START, 'Unexpected character "EOF"', '0:1']]);
});

it('should report missing >', () => {
expect(tokenizeAndHumanizeErrors('<name'))
.toEqual([[HtmlTokenType.TAG_OPEN_START, 'Unexpected character "EOF"', '0:5']]);
});
});

describe('attributes', () => {
Expand Down Expand Up @@ -335,20 +326,6 @@ export function main() {
]);
});

it('should report missing value after =', () => {
expect(tokenizeAndHumanizeErrors('<name a='))
.toEqual([[HtmlTokenType.ATTR_VALUE, 'Unexpected character "EOF"', '0:8']]);
});

it('should report missing end quote for \'', () => {
expect(tokenizeAndHumanizeErrors('<name a=\''))
.toEqual([[HtmlTokenType.ATTR_VALUE, 'Unexpected character "EOF"', '0:9']]);
});

it('should report missing end quote for "', () => {
expect(tokenizeAndHumanizeErrors('<name a="'))
.toEqual([[HtmlTokenType.ATTR_VALUE, 'Unexpected character "EOF"', '0:9']]);
});
});

describe('closing tags', () => {
Expand Down Expand Up @@ -448,6 +425,39 @@ export function main() {
expect(tokenizeAndHumanizeSourceSpans('a'))
.toEqual([[HtmlTokenType.TEXT, 'a'], [HtmlTokenType.EOF, '']]);
});

it('should allow "<" in text nodes', () => {
expect(tokenizeAndHumanizeParts('{{ a < b ? c : d }}'))
.toEqual([[HtmlTokenType.TEXT, '{{ a < b ? c : d }}'], [HtmlTokenType.EOF]]);

expect(tokenizeAndHumanizeSourceSpans('<p>a<b</p>'))
.toEqual([
[HtmlTokenType.TAG_OPEN_START, '<p'],
[HtmlTokenType.TAG_OPEN_END, '>'],
[HtmlTokenType.TEXT, 'a<b'],
[HtmlTokenType.TAG_CLOSE, '</p>'],
[HtmlTokenType.EOF, ''],
]);

expect(tokenizeAndHumanizeParts('< a>'))
.toEqual([[HtmlTokenType.TEXT, '< a>'], [HtmlTokenType.EOF]]);
});

// TODO(vicb): make the lexer aware of Angular expressions
// see https://github.com/angular/angular/issues/5679
it('should parse valid start tag in interpolation', () => {
expect(tokenizeAndHumanizeParts('{{ a <b && c > d }}'))
.toEqual([
[HtmlTokenType.TEXT, '{{ a '],
[HtmlTokenType.TAG_OPEN_START, null, 'b'],
[HtmlTokenType.ATTR_NAME, null, '&&'],
[HtmlTokenType.ATTR_NAME, null, 'c'],
[HtmlTokenType.TAG_OPEN_END],
[HtmlTokenType.TEXT, ' d }}'],
[HtmlTokenType.EOF]
]);
});

});

describe('raw text', () => {
Expand Down
24 changes: 24 additions & 0 deletions modules/angular2/test/compiler/html_parser_spec.ts