diff --git a/src/node.spec.ts b/src/node.spec.ts index f350655e..670f02ec 100644 --- a/src/node.spec.ts +++ b/src/node.spec.ts @@ -72,6 +72,7 @@ describe("Nodes", () => { class Doctype extends node.Node { type = ElementType.Doctype; nodeType = Number.NaN; + nodeName = "doctype"; } const element = new Doctype(); expect(() => element.cloneNode()).toThrow( @@ -106,6 +107,22 @@ describe("Nodes", () => { expect(parent).toHaveProperty("x-mode", "no-quirks"); }); + + it("should expose DOM nodeName aliases", () => { + const text = new node.Text("text"); + const comment = new node.Comment("comment"); + const cdata = new node.CDATA([text]); + const document = new node.Document([cdata]); + const directive = new node.ProcessingInstruction("xml", "?xml"); + const element = new node.Element("div", {}); + + expect(text.nodeName).toBe("#text"); + expect(comment.nodeName).toBe("#comment"); + expect(cdata.nodeName).toBe("#cdata-section"); + expect(document.nodeName).toBe("#document"); + expect(directive.nodeName).toBe("xml"); + expect(element.nodeName).toBe("div"); + }); }); type Options = DomHandlerOptions & ParserOptions; diff --git a/src/node.ts b/src/node.ts index ccefb9c1..30473f23 100644 --- a/src/node.ts +++ b/src/node.ts @@ -80,6 +80,12 @@ export abstract class Node { */ abstract readonly nodeType: number; + /** + * [DOM spec](https://dom.spec.whatwg.org/#dom-node-nodename)-compatible + * alias. + */ + abstract readonly nodeName: string; + // Read-write aliases for properties /** @@ -164,6 +170,10 @@ export class Text extends DataNode { get nodeType(): 3 { return 3; } + + get nodeName(): "#text" { + return "#text"; + } } /** @@ -175,6 +185,10 @@ export class Comment extends DataNode { get nodeType(): 8 { return 8; } + + get nodeName(): "#comment" { + return "#comment"; + } } /** @@ -193,6 +207,10 @@ export class ProcessingInstruction extends DataNode { return 1; } + get nodeName(): string { + return this.name; + } + /** If this is a doctype, the document type name (parse5 only). */ "x-name"?: string; /** If this is a doctype, the document type public identifier (parse5 only). */ @@ -250,6 +268,10 @@ export class CDATA extends NodeWithChildren { get nodeType(): 4 { return 4; } + + get nodeName(): "#cdata-section" { + return "#cdata-section"; + } } /** @@ -262,6 +284,10 @@ export class Document extends NodeWithChildren { return 9; } + get nodeName(): "#document" { + return "#document"; + } + /** [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks) (parse5 only). */ declare "x-mode"?: "no-quirks" | "quirks" | "limited-quirks"; } @@ -313,6 +339,10 @@ export class Element extends NodeWithChildren { return 1; } + get nodeName(): string { + return this.name; + } + /** * `parse5` source code location info, with start & end tags. *