|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import * as html from '../../src/ml_parser/ast'; |
| 10 | +import {Parser} from '../../src/ml_parser/parser'; |
| 11 | +import {TagContentType} from '../../src/ml_parser/tags'; |
| 12 | + |
| 13 | +describe('Inline comments in attributes', () => { |
| 14 | + const parser = new Parser((tagName) => ({ |
| 15 | + closedByParent: false, |
| 16 | + implicitNamespacePrefix: null, |
| 17 | + isVoid: false, |
| 18 | + ignoreFirstLf: false, |
| 19 | + canSelfClose: false, |
| 20 | + preventNamespaceInheritance: false, |
| 21 | + isClosedByChild: () => false, |
| 22 | + getContentType: () => TagContentType.PARSABLE_DATA, |
| 23 | + })); |
| 24 | + |
| 25 | + it('should ignore single line comments between attributes', () => { |
| 26 | + const source = ` |
| 27 | + <div |
| 28 | + // comment 1 |
| 29 | + attr1="value1" |
| 30 | + // comment 2 |
| 31 | + attr2="value2" |
| 32 | + ></div> |
| 33 | + `; |
| 34 | + const result = parser.parse(source, 'url'); |
| 35 | + expect(result.errors.length).toBe(0); |
| 36 | + const element = result.rootNodes.find((n) => n instanceof html.Element) as html.Element; |
| 37 | + expect(element.attrs.length).toBe(2); |
| 38 | + expect(element.attrs[0].name).toBe('attr1'); |
| 39 | + expect(element.attrs[1].name).toBe('attr2'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should ignore single line comments between inputs and outputs', () => { |
| 43 | + const source = ` |
| 44 | + <div |
| 45 | + // comment 1 |
| 46 | + [input]="value1" |
| 47 | + // comment 2 |
| 48 | + (output)="handler()" |
| 49 | + ></div> |
| 50 | + `; |
| 51 | + const result = parser.parse(source, 'url'); |
| 52 | + expect(result.errors.length).toBe(0); |
| 53 | + const element = result.rootNodes.find((n) => n instanceof html.Element) as html.Element; |
| 54 | + expect(element.attrs.length).toBe(2); |
| 55 | + expect(element.attrs[0].name).toBe('[input]'); |
| 56 | + expect(element.attrs[1].name).toBe('(output)'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should ignore single line comments at the end of tag', () => { |
| 60 | + const source = `<div attr1="value1" // comment |
| 61 | + ></div>`; |
| 62 | + const result = parser.parse(source, 'url'); |
| 63 | + expect(result.errors.length).toBe(0); |
| 64 | + const element = result.rootNodes.find((n) => n instanceof html.Element) as html.Element; |
| 65 | + expect(element.attrs.length).toBe(1); |
| 66 | + expect(element.attrs[0].name).toBe('attr1'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should handle commented out attribute', () => { |
| 70 | + const source = `<div /* attr1="value1" */ attr2="value2"></div>`; |
| 71 | + const result = parser.parse(source, 'url'); |
| 72 | + expect(result.errors.length).toBe(0); |
| 73 | + const element = result.rootNodes.find((n) => n instanceof html.Element) as html.Element; |
| 74 | + expect(element.attrs.length).toBe(1); |
| 75 | + expect(element.attrs[0].name).toBe('attr2'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should comment an attribute with a // on a new line', () => { |
| 79 | + const source = `<div |
| 80 | + // attr1="value1" |
| 81 | + attr2="value2"></div>`; |
| 82 | + const result = parser.parse(source, 'url'); |
| 83 | + expect(result.errors.length).toBe(0); |
| 84 | + const element = result.rootNodes.find((n) => n instanceof html.Element) as html.Element; |
| 85 | + expect(element.attrs.length).toBe(1); |
| 86 | + expect(element.attrs[0].name).toBe('attr2'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should ignore multi-line comments between attributes', () => { |
| 90 | + const source = ` |
| 91 | + <div |
| 92 | + /* comment 1 */ |
| 93 | + attr1="value1" |
| 94 | + /* |
| 95 | + comment 2 |
| 96 | + spanning multiple lines |
| 97 | + */ |
| 98 | + attr2="value2" |
| 99 | + ></div> |
| 100 | + `; |
| 101 | + const result = parser.parse(source, 'url'); |
| 102 | + expect(result.errors.length).toBe(0); |
| 103 | + const element = result.rootNodes.find((n) => n instanceof html.Element) as html.Element; |
| 104 | + expect(element.attrs.length).toBe(2); |
| 105 | + expect(element.attrs[0].name).toBe('attr1'); |
| 106 | + expect(element.attrs[1].name).toBe('attr2'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should ignore multi-line comments at the end of tag', () => { |
| 110 | + const source = `<div attr1="value1" /* comment */ ></div>`; |
| 111 | + const result = parser.parse(source, 'url'); |
| 112 | + expect(result.errors.length).toBe(0); |
| 113 | + const element = result.rootNodes.find((n) => n instanceof html.Element) as html.Element; |
| 114 | + expect(element.attrs.length).toBe(1); |
| 115 | + expect(element.attrs[0].name).toBe('attr1'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should handle * inside multi-line comments', () => { |
| 119 | + const source = `<div attr1="value1" /* comment with * inside */ attr2="value2"></div>`; |
| 120 | + const result = parser.parse(source, 'url'); |
| 121 | + expect(result.errors.length).toBe(0); |
| 122 | + const element = result.rootNodes.find((n) => n instanceof html.Element) as html.Element; |
| 123 | + expect(element.attrs.length).toBe(2); |
| 124 | + expect(element.attrs[0].name).toBe('attr1'); |
| 125 | + expect(element.attrs[1].name).toBe('attr2'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should maintain correct source spans with comments', () => { |
| 129 | + const source = `<div attr1="a" /* comment */ attr2="b"></div>`; |
| 130 | + const result = parser.parse(source, 'url'); |
| 131 | + expect(result.errors.length).toBe(0); |
| 132 | + const element = result.rootNodes.find((n) => n instanceof html.Element) as html.Element; |
| 133 | + expect(element.attrs.length).toBe(2); |
| 134 | + |
| 135 | + const attr1 = element.attrs[0]; |
| 136 | + expect(attr1.name).toBe('attr1'); |
| 137 | + expect(attr1.sourceSpan.start.offset).toBe(5); |
| 138 | + expect(attr1.sourceSpan.end.offset).toBe(14); |
| 139 | + |
| 140 | + const attr2 = element.attrs[1]; |
| 141 | + expect(attr2.name).toBe('attr2'); |
| 142 | + expect(attr2.sourceSpan.start.offset).toBe(29); |
| 143 | + expect(attr2.sourceSpan.end.offset).toBe(38); |
| 144 | + }); |
| 145 | +}); |
0 commit comments