Skip to content

Commit

Permalink
fix(tokenizer): Allow XML tags to start with any character (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Apr 8, 2021
1 parent ee68790 commit 0b94ab5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,18 @@ export default class Tokenizer {
this.sectionStart = this._index;
}
}
/**
* HTML only allows ASCII alpha characters (a-z and A-Z) at the beginning of a tag name.
*
* XML allows a lot more characters here (@see https://www.w3.org/TR/REC-xml/#NT-NameStartChar).
* We allow anything that wouldn't end the tag.
*/
private isTagStartChar(c: string) {
return (
isASCIIAlpha(c) ||
(this.xmlMode && !whitespace(c) && c !== "/" && c !== ">")
);
}
private stateBeforeTagName(c: string) {
if (c === "/") {
this._state = State.BeforeClosingTagName;
Expand All @@ -342,7 +354,7 @@ export default class Tokenizer {
} else if (c === "?") {
this._state = State.InProcessingInstruction;
this.sectionStart = this._index + 1;
} else if (!isASCIIAlpha(c)) {
} else if (!this.isTagStartChar(c)) {
this._state = State.Text;
} else {
this._state =
Expand Down Expand Up @@ -378,7 +390,7 @@ export default class Tokenizer {
this._state = State.Text;
this._index--;
}
} else if (!isASCIIAlpha(c)) {
} else if (!this.isTagStartChar(c)) {
this._state = State.InSpecialComment;
this.sectionStart = this._index;
} else {
Expand Down
33 changes: 33 additions & 0 deletions src/__fixtures__/Events/40-xml_tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "XML tags",
"options": {
"parser": { "xmlMode": true }
},
"html": "<:foo><_bar>",
"expected": [
{
"data": [":foo"],
"event": "opentagname"
},
{
"data": [":foo", {}],
"event": "opentag"
},
{
"data": ["_bar"],
"event": "opentagname"
},
{
"data": ["_bar", {}],
"event": "opentag"
},
{
"data": ["_bar"],
"event": "closetag"
},
{
"data": [":foo"],
"event": "closetag"
}
]
}

0 comments on commit 0b94ab5

Please sign in to comment.