Skip to content

fix: document body/head shortcut getters when documentElement is body/head - #362

Open
johnhldiver-beep wants to merge 1 commit into
WebReflection:mainfrom
johnhldiver-beep:fix/283-document-body-shortcut
Open

johnhldiver-beep wants to merge 1 commit into
WebReflection:mainfrom
johnhldiver-beep:fix/283-document-body-shortcut

Conversation

@johnhldiver-beep

Copy link
Copy Markdown

Fixes #283.

document.body loses its children: parseHTML('<body><div class="whatever"></div></body>') makes the <body> element itself the documentElement, and the shortcut getters in esm/html/document.js then misfire: head prepends a new empty <head> inside the real body, and body creates a new empty <body> after it. Result: two <body> elements, document.body is the fake empty one, and merely accessing it mutates the real body.

Fix (root cause, one concern): the head/body getters now return documentElement directly when it already is HEAD/BODY, before any create/prepend/after logic. Both mirrors updated identically. title untouched (not affected by this root cause). No existing tests modified.

Tests: regression tests in test/html/document.js for the body case and the symmetric head case. New tests failed before the fix and pass after; test/html/document.js and the full node test/index.js suite both exit 0 with no failures. Repros verified in both ESM and CJS: body === document.body true, exactly 1 body, children still just DIV (no mutation), bare-<head> doc head identity true, and a normal full document (head/body/title) unaffected. Honest limits: this suite prints no numeric assertion totals, so counts are reported as exit codes + failure-grep counts; eslint/c8 were not run (devDeps deliberately skipped for disk).

Notes: prior PR #108 (2022, "Fix document.body not including content from DOMParser parsed ") was closed unmerged because the maintainer incorporated a related fix for #106 directly — and his stated guidance there ("The body getter is likely the place to look for a change") is exactly the approach taken here. Residual, pre-existing, out of scope: calling document.head directly on a bare-<body> document still prepends an empty <head> inside the body (the old ensure-a-head fallback); the issue's repro is about body, and changing head-on-bare-body semantics would be a separate behavior decision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] document shortcut element properties lose their children

1 participant