Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parse5): use string types for node names #631

Merged
merged 1 commit into from Aug 4, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 12 additions & 22 deletions packages/parse5/lib/tree-adapters/default.ts
Expand Up @@ -2,17 +2,9 @@ import { DOCUMENT_MODE, type NS } from '../common/html.js';
import type { Attribute, Location, ElementLocation } from '../common/token.js';
import type { TreeAdapter, TreeAdapterTypeMap } from './interface.js';

export enum NodeType {
Document = '#document',
DocumentFragment = '#document-fragment',
Comment = '#comment',
Text = '#text',
DocumentType = '#documentType',
}

export interface Document {
/** The name of the node. */
nodeName: NodeType.Document;
nodeName: '#document';
/**
* Document mode.
*
Expand All @@ -26,7 +18,7 @@ export interface Document {

export interface DocumentFragment {
/** The name of the node. */
nodeName: NodeType.DocumentFragment;
nodeName: '#document-fragment';
/** The node's children. */
childNodes: ChildNode[];
/** Comment source code location info. Available if location info is enabled. */
Expand All @@ -52,7 +44,7 @@ export interface Element {

export interface CommentNode {
/** The name of the node. */
nodeName: NodeType.Comment;
nodeName: '#comment';
/** Parent node. */
parentNode: ParentNode | null;
/** Comment text. */
Expand All @@ -62,7 +54,7 @@ export interface CommentNode {
}

export interface TextNode {
nodeName: NodeType.Text;
nodeName: '#text';
/** Parent node. */
parentNode: ParentNode | null;
/** Text content. */
Expand All @@ -80,7 +72,7 @@ export interface Template extends Element {

export interface DocumentType {
/** The name of the node. */
nodeName: NodeType.DocumentType;
nodeName: '#documentType';
/** Parent node. */
parentNode: ParentNode | null;
/** Document type name. */
Expand Down Expand Up @@ -112,7 +104,7 @@ export type DefaultTreeAdapterMap = TreeAdapterTypeMap<

function createTextNode(value: string): TextNode {
return {
nodeName: NodeType.Text,
nodeName: '#text',
value,
parentNode: null,
};
Expand All @@ -122,15 +114,15 @@ export const defaultTreeAdapter: TreeAdapter<DefaultTreeAdapterMap> = {
//Node construction
createDocument(): Document {
return {
nodeName: NodeType.Document,
nodeName: '#document',
mode: DOCUMENT_MODE.NO_QUIRKS,
childNodes: [],
};
},

createDocumentFragment(): DocumentFragment {
return {
nodeName: NodeType.DocumentFragment,
nodeName: '#document-fragment',
childNodes: [],
};
},
Expand All @@ -148,7 +140,7 @@ export const defaultTreeAdapter: TreeAdapter<DefaultTreeAdapterMap> = {

createCommentNode(data: string): CommentNode {
return {
nodeName: NodeType.Comment,
nodeName: '#comment',
data,
parentNode: null,
};
Expand Down Expand Up @@ -176,17 +168,15 @@ export const defaultTreeAdapter: TreeAdapter<DefaultTreeAdapterMap> = {
},

setDocumentType(document: Document, name: string, publicId: string, systemId: string): void {
const doctypeNode = document.childNodes.find(
(node): node is DocumentType => node.nodeName === NodeType.DocumentType
);
const doctypeNode = document.childNodes.find((node): node is DocumentType => node.nodeName === '#documentType');

if (doctypeNode) {
doctypeNode.name = name;
doctypeNode.publicId = publicId;
doctypeNode.systemId = systemId;
} else {
const node: DocumentType = {
nodeName: NodeType.DocumentType,
nodeName: '#documentType',
name,
publicId,
systemId,
Expand Down Expand Up @@ -302,7 +292,7 @@ export const defaultTreeAdapter: TreeAdapter<DefaultTreeAdapterMap> = {
},

isDocumentTypeNode(node: Node): node is DocumentType {
return node.nodeName === NodeType.DocumentType;
return node.nodeName === '#documentType';
},

isElementNode(node: Node): node is Element {
Expand Down