Skip to content

Commit 410d6ea

Browse files
committed
Fix: bugs about x-invalid-namespace
1 parent b6b536a commit 410d6ea

File tree

4 files changed

+2826
-10
lines changed

4 files changed

+2826
-10
lines changed

src/html/parser.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
import * as lodash from "lodash"
77
import {debug} from "../common/debug"
8-
import {ErrorCode, Namespace, NS, ParseError, Token, VAttribute, VDocumentFragment, VElement, VNode, VText} from "../ast"
8+
import {ErrorCode, HasLocation, Namespace, NS, ParseError, Token, VAttribute, VDocumentFragment, VElement, VNode, VText} from "../ast"
99
import {MATHML_ATTRIBUTE_NAME_MAP, SVG_ATTRIBUTE_NAME_MAP} from "./util/attribute-names"
1010
import {HTML_CAN_BE_LEFT_OPEN_TAGS, HTML_NON_FHRASING_TAGS, HTML_RAWTEXT_TAGS, HTML_RCDATA_TAGS, HTML_VOID_ELEMENT_TAGS, SVG_ELEMENT_NAME_MAP} from "./util/tag-names"
1111
import {Tokenizer, TokenType} from "./tokenizer"
@@ -132,7 +132,7 @@ export class Parser {
132132
* Report an invalid character error.
133133
* @param code The error code.
134134
*/
135-
private reportParseError(token: Token, code: ErrorCode): void {
135+
private reportParseError(token: HasLocation, code: ErrorCode): void {
136136
const error = ParseError.fromCode(code, token.range[0], token.loc.start.line, token.loc.start.column)
137137
this.errors.push(error)
138138

@@ -185,6 +185,18 @@ export class Parser {
185185
this.namespace = NS.HTML
186186
}
187187
}
188+
// Check namespace
189+
else if (node.type === "VAttribute" && node.directive === false) {
190+
const key = node.key.name
191+
const value = node.value && node.value.value
192+
193+
if (key === "xmlns" && value !== this.namespace) {
194+
this.reportParseError(node, "x-invalid-namespace")
195+
}
196+
if (key === "xmlns:xlink" && value !== NS.XLink) {
197+
this.reportParseError(node, "x-invalid-namespace")
198+
}
199+
}
188200
}
189201

190202
/**
@@ -483,14 +495,6 @@ export class Parser {
483495
throw new Error("unreachable")
484496
}
485497

486-
// Check namespace
487-
if (attribute.key.name === "xmlns" && token.value !== this.namespace) {
488-
this.reportParseError(token, "x-invalid-namespace")
489-
}
490-
if (attribute.key.name === "xmlns:xlink" && token.value !== NS.XLink) {
491-
this.reportParseError(token, "x-invalid-namespace")
492-
}
493-
494498
// Initialize the attribute value.
495499
attribute.range[1] = token.range[1]
496500
attribute.loc.end = token.loc.end

0 commit comments

Comments
 (0)