Skip to content

Commit d06fe5d

Browse files
committed
Rename allowInElementComments option to allowStartTagComments, Move the attribute comment to the HTML AST angular@826017d
1 parent 63a1493 commit d06fe5d

4 files changed

Lines changed: 40 additions & 20 deletions

File tree

‎packages/angular-html-parser/src/index.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ export interface HtmlParseOptions {
1717
*/
1818
allowHtmComponentClosingTags?: boolean;
1919
/**
20-
* allow in-element comments
20+
* allow comments in start tag
2121
*
2222
* defaults to false
2323
*/
24-
allowInElementComments?: boolean;
24+
allowStartTagComments?: boolean;
2525
/**
2626
* do not lowercase tag names before querying their tag definitions
2727
*
@@ -62,7 +62,7 @@ export function parseHtml(
6262
const {
6363
canSelfClose = false,
6464
allowHtmComponentClosingTags = false,
65-
allowInElementComments = false,
65+
allowStartTagComments = false,
6666
isTagNameCaseSensitive = false,
6767
getTagContentType,
6868
tokenizeAngularBlocks = false,
@@ -78,7 +78,7 @@ export function parseHtml(
7878
tokenizeExpansionForms: tokenizeAngularBlocks,
7979
canSelfClose,
8080
allowHtmComponentClosingTags,
81-
allowInElementComments,
81+
allowStartTagComments,
8282
tokenizeBlocks: tokenizeAngularBlocks,
8383
tokenizeLet: tokenizeAngularLetDeclaration,
8484
selectorlessEnabled: enableAngularSelectorlessSyntax,

‎packages/angular-html-parser/test/index_spec.ts‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,25 +243,41 @@ describe("AST format", () => {
243243
});
244244
});
245245

246-
it("in-element comments", () => {
246+
it("Start tag comments", () => {
247247
expect(parse("<div/* comment */></div>").errors[0]).toMatchInlineSnapshot(
248248
`[Error: Opening tag "div" not terminated.]`,
249249
);
250250
expect(
251-
humanizeDom(
252-
parse("<div/* block comment */></div>", { allowInElementComments: true }),
253-
),
251+
parse("<div/* block comment */></div>", { allowStartTagComments: true })
252+
.rootNodes,
254253
).toEqual([
255-
[ast.Comment, " block comment ", 0],
256-
[ast.Element, "div", 0],
254+
expect.objectContaining({
255+
kind: "element",
256+
name: "div",
257+
comments: [
258+
expect.objectContaining({
259+
kind: "startTagComment",
260+
type: "multi",
261+
value: " block comment ",
262+
}),
263+
],
264+
}),
257265
]);
258266
expect(
259-
humanizeDom(
260-
parse("<div// line comment\n></div>", { allowInElementComments: true }),
261-
),
267+
parse("<div// line comment\n></div>", { allowStartTagComments: true })
268+
.rootNodes,
262269
).toEqual([
263-
[ast.Comment, " line comment", 0],
264-
[ast.Element, "div", 0],
270+
expect.objectContaining({
271+
kind: "element",
272+
name: "div",
273+
comments: [
274+
expect.objectContaining({
275+
kind: "startTagComment",
276+
type: "single",
277+
value: " line comment",
278+
}),
279+
],
280+
}),
265281
]);
266282
});
267283

‎packages/compiler/src/ml_parser/ast.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ export class StartTagComment implements BaseNode {
134134
visit(visitor: Visitor, context: any): any {
135135
return visitor.visitAttributeComment ? visitor.visitAttributeComment(this, context) : undefined;
136136
}
137+
readonly kind = 'startTagComment';
137138
}
138139

139140
export class Element extends NodeWithI18n {
@@ -171,7 +172,10 @@ export class Comment implements BaseNode {
171172
}
172173

173174
export class DocType implements BaseNode {
174-
constructor(public value: string|null, public sourceSpan: ParseSourceSpan) {}
175+
constructor(
176+
public value: string | null,
177+
public sourceSpan: ParseSourceSpan,
178+
) {}
175179
visit(visitor: Visitor, context: any): any {
176180
return visitor.visitDocType(this, context);
177181
}

‎packages/compiler/src/ml_parser/lexer.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export interface TokenizeOptions {
107107

108108
canSelfClose?: boolean;
109109
allowHtmComponentClosingTags?: boolean;
110-
allowInElementComments?: boolean,
110+
allowStartTagComments?: boolean,
111111
}
112112

113113
export function tokenize(
@@ -171,7 +171,7 @@ class _Tokenizer {
171171
private _leadingTriviaCodePoints: number[] | undefined;
172172
private _canSelfClose: boolean;
173173
private _allowHtmComponentClosingTags: boolean;
174-
private _allowInElementComments: boolean;
174+
private _allowStartTagComments: boolean;
175175
private _currentTokenStart: CharacterCursor | null = null;
176176
private _currentTokenType: TokenType | null = null;
177177
private _expansionCaseStack: TokenType[] = [];
@@ -205,7 +205,7 @@ class _Tokenizer {
205205
options.leadingTriviaChars && options.leadingTriviaChars.map((c) => c.codePointAt(0) || 0);
206206
this._canSelfClose = options.canSelfClose || false;
207207
this._allowHtmComponentClosingTags = options.allowHtmComponentClosingTags || false;
208-
this._allowInElementComments = options.allowInElementComments ?? true;
208+
this._allowStartTagComments = options.allowStartTagComments ?? true;
209209
const range = options.range || {
210210
endPos: _file.content.length,
211211
startPos: 0,
@@ -923,7 +923,7 @@ class _Tokenizer {
923923
}
924924

925925
while (true) {
926-
if (this._allowInElementComments) {
926+
if (this._allowStartTagComments) {
927927
const commentStart = this._cursor.clone();
928928

929929
if (this._attemptStr('//')) {

0 commit comments

Comments
 (0)