From 9e07e3f77ff705bb1e548af61c6d4b30442282a6 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 17 Oct 2018 17:33:32 -0400 Subject: [PATCH] include solidus in attribute values - fixes #1772 --- src/parse/index.ts | 16 +++++--- src/parse/state/tag.ts | 12 +++--- .../attribute-containing-solidus/input.html | 1 + .../attribute-containing-solidus/output.json | 41 +++++++++++++++++++ 4 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 test/parser/samples/attribute-containing-solidus/input.html create mode 100644 test/parser/samples/attribute-containing-solidus/output.json diff --git a/src/parse/index.ts b/src/parse/index.ts index f81a341273..c16148631e 100644 --- a/src/parse/index.ts +++ b/src/parse/index.ts @@ -136,6 +136,13 @@ export class Parser { return this.template.slice(this.index, this.index + str.length) === str; } + matchRegex(pattern: RegExp) { + const match = pattern.exec(this.template.slice(this.index)); + if (!match || match.index !== 0) return null; + + return match[0]; + } + allowWhitespace() { while ( this.index < this.template.length && @@ -146,12 +153,9 @@ export class Parser { } read(pattern: RegExp) { - const match = pattern.exec(this.template.slice(this.index)); - if (!match || match.index !== 0) return null; - - this.index += match[0].length; - - return match[0]; + const result = this.matchRegex(pattern); + if (result) this.index += result.length; + return result; } readIdentifier() { diff --git a/src/parse/state/tag.ts b/src/parse/state/tag.ts index bc112d4bbc..4dba778c88 100644 --- a/src/parse/state/tag.ts +++ b/src/parse/state/tag.ts @@ -397,14 +397,14 @@ function readAttribute(parser: Parser, uniqueNames: Set) { function readAttributeValue(parser: Parser) { const quoteMark = parser.eat(`'`) ? `'` : parser.eat(`"`) ? `"` : null; - const regex = quoteMark === `'` - ? /'/ - : quoteMark === `"` ? /"/ : /[\s"'=<>\/`]/; - - const value = readSequence(parser, () => - regex.test(parser.template[parser.index]) + const regex = ( + quoteMark === `'` ? /'/ : + quoteMark === `"` ? /"/ : + /(\/>|[\s"'=<>`])/ ); + const value = readSequence(parser, () => !!parser.matchRegex(regex)); + if (quoteMark) parser.index += 1; return value; } diff --git a/test/parser/samples/attribute-containing-solidus/input.html b/test/parser/samples/attribute-containing-solidus/input.html new file mode 100644 index 0000000000..c8a8534c23 --- /dev/null +++ b/test/parser/samples/attribute-containing-solidus/input.html @@ -0,0 +1 @@ +Google diff --git a/test/parser/samples/attribute-containing-solidus/output.json b/test/parser/samples/attribute-containing-solidus/output.json new file mode 100644 index 0000000000..2927ae1d72 --- /dev/null +++ b/test/parser/samples/attribute-containing-solidus/output.json @@ -0,0 +1,41 @@ +{ + "html": { + "start": 0, + "end": 41, + "type": "Fragment", + "children": [ + { + "start": 0, + "end": 41, + "type": "Element", + "name": "a", + "attributes": [ + { + "start": 3, + "end": 30, + "type": "Attribute", + "name": "href", + "value": [ + { + "start": 8, + "end": 30, + "type": "Text", + "data": "https://www.google.com" + } + ] + } + ], + "children": [ + { + "start": 31, + "end": 37, + "type": "Text", + "data": "Google" + } + ] + } + ] + }, + "css": null, + "js": null +} \ No newline at end of file