Repository navigation
Conversation
Agent-Logs-Url: https://github.com/HtmlUnit/htmlunit/sessions/fba3cddd-48d3-46d8-891c-65f275e769a6 Co-authored-by: rbri <[email protected]>
Copilot
AI
changed the title
[WIP] Implement Document.parseHTMLUnsafe static method
Add May 22, 2026
Document.parseHTMLUnsafe / HTMLDocument.parseHTMLUnsafe and full DOM parsing coverage
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



This change adds support for the standards-defined static
Document.parseHTMLUnsafe(html)API (also exposed viaHTMLDocument) to parse full HTML documents without sanitization. It aligns HtmlUnit with modern browser behavior distinct fromDOMParser.parseFromString(..., 'text/html')by exposing the constructor-level static entrypoint.API surface: static
parseHTMLUnsafeonDocument@JsxStaticFunctiononDocument:parseHTMLUnsafe(Context, VarScope, Scriptable, Object[], Function)undefined→""JavaScriptEngine.toString(...)DOMParser.parseHtmlDocument(...).API parity: static
parseHTMLUnsafeonHTMLDocument@JsxStaticFunctiononHTMLDocumentdelegating toDocument.parseHTMLUnsafe(...).New conformance-style test suite
ParseHTMLUnsafeTestwith targeted coverage for:DocumentandHTMLDocument[object HTMLDocument])title,getElementById,querySelector)<script>element present, inline handler attribute preserved)defaultView === null, parsed doc not equal to pagedocument)<!DOCTYPE html>...) and typical element/attribute preservation (img[src])Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
central.sonatype.com/usr/lib/jvm/temurin-17-jdk-amd64/bin/java /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --enable-native-access=ALL-UNNAMED -classpath /usr/share/apache-maven-3.9.16/boot/plexus-classworlds-2.11.0.jar -Dclassworlds.conf=/usr/share/apache-maven-3.9.16/bin/m2.conf -Dmaven.home=/usr/share/apache-maven-3.9.16 -Dlibrary.jansi.path=/usr/share/apache-maven-3.9.16/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/htmlunit/htmlunit org.codehaus.plexus.classworlds.launcher.Launcher -Dtest=org.htmlunit.javascript.host.dom.DOMParserTest test(dns block)/usr/lib/jvm/temurin-17-jdk-amd64/bin/java /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --enable-native-access=ALL-UNNAMED -classpath /usr/share/apache-maven-3.9.16/boot/plexus-classworlds-2.11.0.jar -Dclassworlds.conf=/usr/share/apache-maven-3.9.16/bin/m2.conf -Dmaven.home=/usr/share/apache-maven-3.9.16 -Dlibrary.jansi.path=/usr/share/apache-maven-3.9.16/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/htmlunit/htmlunit org.codehaus.plexus.classworlds.launcher.Launcher -Dtest=org.htmlunit.javascript.host.dom.ParseHTMLUnsafeTest test(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
Goal
Implement
Document.parseHTMLUnsafe(html)as a static method on theDocumentclass (and also onHTMLDocument) in HtmlUnit, and add a comprehensive test suite inDOMParserTest.java(or a newParseHTMLUnsafeTest.java).Background / Spec
Document.parseHTMLUnsafe(string)is defined in the HTML Living Standard §8.4 and DOM Parsing spec. It is a static method onDocumentthat:Documentobject (anHTMLDocument)DOMParser.parseFromString(html, 'text/html'), this is a static method called directly on theDocumentconstructor:Document.parseHTMLUnsafe("<html>...</html>")Existing code to understand first
Before implementing, study these files carefully:
src/main/java/org/htmlunit/javascript/host/dom/DOMParser.java— this already implementsparseFromStringfortext/html, usingStringWebResponse+HtmlPage+HTMLParser. The newparseHTMLUnsafeshould reuse the sameparseHtmlDocumentstatic helper.src/main/java/org/htmlunit/javascript/host/dom/Document.java— this is where the new@JsxStaticFunctionmust be added.src/main/java/org/htmlunit/javascript/host/html/HTMLDocument.java— check ifparseHTMLUnsafealso needs to be placed here (browsers expose it on bothDocumentandHTMLDocument).src/main/java/org/htmlunit/javascript/host/URL.java— reference for@JsxStaticFunctionpattern.src/main/java/org/htmlunit/javascript/host/media/MediaSource.java— another simple@JsxStaticFunctionexample.src/test/java/org/htmlunit/javascript/host/dom/DOMParserTest.java— existing test patterns to follow.Implementation
1. Add
parseHTMLUnsafetoDocument.javaIn
src/main/java/org/htmlunit/javascript/host/dom/Document.java, add a static method:2. Required imports
Add whatever imports are needed in
Document.java:org.htmlunit.StringWebResponseorg.htmlunit.WebResponseorg.htmlunit.javascript.host.dom.DOMParser(if not already imported)org.htmlunit.corejs.javascript.Contextorg.htmlunit.corejs.javascript.Functionorg.htmlunit.corejs.javascript.Scriptableorg.htmlunit.corejs.javascript.VarScopeTest suite
Create
src/test/java/org/htmlunit/javascript/host/dom/ParseHTMLUnsafeTest.javaextendingWebDriverTestCasewith the following tests. Follow the exact style ofDOMParserTest.java(inline HTML string,LOG_TITLE_FUNCTION,loadPageVerifyTitle2).Tests to ...
This pull request was created from Copilot chat.