Skip to content

Commit 3813f0e

Browse files
committed
Chore: small refactoring
1 parent f6f42bb commit 3813f0e

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

src/html/parser.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,12 @@ export class Parser {
303303

304304
/**
305305
* Detect the namespace of the new element.
306-
* @param name The value of a HTMLTagOpen token.
306+
* @param token The StartTag token to detect.
307307
* @returns The namespace of the new element.
308308
*/
309-
private detectNamespace(name: string): Namespace {
309+
//eslint-disable-next-line complexity, require-jsdoc
310+
private detectNamespace(token: StartTag): Namespace {
311+
const name = token.name
310312
let ns = this.namespace
311313

312314
if (ns === NS.MathML || ns === NS.SVG) {
@@ -339,6 +341,15 @@ export class Parser {
339341
}
340342
}
341343

344+
if (name === "template") {
345+
const xmlns = token.attributes.find(a => a.key.name === "xmlns")
346+
const value = xmlns && xmlns.value && xmlns.value.value
347+
348+
if (value === NS.HTML || value === NS.MathML || value === NS.SVG) {
349+
return value
350+
}
351+
}
352+
342353
return ns
343354
}
344355

@@ -410,22 +421,7 @@ export class Parser {
410421
this.closeCurrentElementIfNecessary(token.name)
411422

412423
const parent = this.currentNode
413-
let namespace = this.detectNamespace(token.name)
414-
if (token.name === "template") {
415-
for (const attribute of token.attributes) {
416-
if (attribute.key.name !== "xmlns") {
417-
continue
418-
}
419-
const value = attribute.value && attribute.value.value
420-
if (
421-
value === NS.HTML ||
422-
value === NS.MathML ||
423-
value === NS.SVG
424-
) {
425-
namespace = value
426-
}
427-
}
428-
}
424+
const namespace = this.detectNamespace(token)
429425
const element: VElement = {
430426
type: "VElement",
431427
range: [token.range[0], token.range[1]],

0 commit comments

Comments
 (0)