Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -164,6 +170,10 @@ export class Text extends DataNode {
get nodeType(): 3 {
return 3;
}

get nodeName(): "#text" {
return "#text";
}
}

/**
Expand All @@ -175,6 +185,10 @@ export class Comment extends DataNode {
get nodeType(): 8 {
return 8;
}

get nodeName(): "#comment" {
return "#comment";
}
}

/**
Expand All @@ -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). */
Expand Down Expand Up @@ -250,6 +268,10 @@ export class CDATA extends NodeWithChildren {
get nodeType(): 4 {
return 4;
}

get nodeName(): "#cdata-section" {
return "#cdata-section";
}
}

/**
Expand All @@ -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";
}
Expand Down Expand Up @@ -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.
*
Expand Down