Skip to content
Open
Changes from 2 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
16 changes: 12 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,14 @@ function text(node, state) {
state.parser.tokenizer.state = 0
}

const isWhitespace = /^[ \t\n\f\r]+$/.test(node.value)
const tokenType = isWhitespace

Copilot AI Mar 24, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The /^[ \t\n\f\r]+$/ regex literal is evaluated each time text() runs, creating a new RegExp object per text node. Consider hoisting it to module scope (or reusing a shared constant) to avoid unnecessary allocations in large documents.

Copilot uses AI. Check for mistakes.
? Token.TokenType.WHITESPACE_CHARACTER
: Token.TokenType.CHARACTER

/** @type {Token.CharacterToken} */
const token = {
type: Token.TokenType.CHARACTER,
type: tokenType,
chars: node.value,

Copilot AI Mar 24, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a regression test for this new whitespace-token behavior (e.g., a <table> with \n / formatting-only text nodes between table elements) to ensure raw() no longer causes parse5 foster parenting to move those text nodes outside the table.

Copilot uses AI. Check for mistakes.
location: createParse5Location(node)
}
Expand Down Expand Up @@ -505,7 +510,10 @@ function startTag(node, state) {
resetTokenizer(state, pointStart(node))

const current = state.parser.openElements.current
let ns = 'namespaceURI' in current ? current.namespaceURI : webNamespaces.html
let ns =
current && 'namespaceURI' in current
? current.namespaceURI
: webNamespaces.html

if (ns === webNamespaces.html && tagName === 'svg') {
ns = webNamespaces.svg
Expand Down Expand Up @@ -628,8 +636,8 @@ function documentMode(node) {
const head = node.type === 'root' ? node.children[0] : node
return Boolean(
head &&
(head.type === 'doctype' ||
(head.type === 'element' && head.tagName.toLowerCase() === 'html'))
(head.type === 'doctype' ||
(head.type === 'element' && head.tagName.toLowerCase() === 'html'))
)
}

Expand Down
Loading