Skip to content

script: Update name validation for attribute, element, and doctype #37747

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

Merged
merged 24 commits into from
Jul 11, 2025

Conversation

minghuaw
Copy link
Contributor

@minghuaw minghuaw commented Jun 27, 2025

A recent update in the spec (whatwg/dom#1079) introduced new rules for name validation of attribute, element, and doctype. This PR implements the new name validation rules in components/script/dom/bindings/domname.rs. The old XML name validation rules are not fully removed because there remains a few usage of it in ProcessingInstructions and xpath.

Testing: Covered by WPT tests
Fixes: #37746

@yezhizhen
Copy link
Contributor

yezhizhen commented Jun 28, 2025

You need to git commit -s to sign off, for each commits to pass DCO.

@servo-wpt-sync
Copy link
Collaborator

🛠 These changes could not be applied onto the latest upstream WPT. Servo's copy of the Web Platform Tests may be out of sync.

@minghuaw minghuaw closed this Jun 30, 2025
@minghuaw minghuaw force-pushed the fix/wpt/dom/nodes/Document-createAttribute branch from 0b3d4c8 to cbb0407 Compare June 30, 2025 01:30
@minghuaw minghuaw reopened this Jun 30, 2025
@minghuaw
Copy link
Contributor Author

minghuaw commented Jun 30, 2025

Below is a summary of how the current draft performs on a few related WPT test files (the live version)

WPT Test File Status (Passed/Total)
wpt/dom/nodes/Document-createAttribute.html 36/36
wpt/dom/nodes/Document-createElement.html 147/147
wpt/dom/nodes/Document-createElementNS.html 575/596
wpt/dom/nodes/name-validation.html 5/5
wpt/dom/nodes/DOMImplementation-createDocumentType.html 82/82

The test cases that are still failing in wpt/dom/nodes/Document-createElementNS.html seem to be caused by confusion and/or bug in the testing implementation (see web-platform-tests/wpt#53464).

Would this draft be considered good enough for review or is it better to wait until it passes all wpt/dom/nodes/Document-createElementNS.html test cases?

@minghuaw minghuaw marked this pull request as ready for review June 30, 2025 06:48
@minghuaw minghuaw force-pushed the fix/wpt/dom/nodes/Document-createAttribute branch from d52c0fe to e6fa526 Compare June 30, 2025 08:06
@minghuaw minghuaw changed the title fix: Update name validation for attribute, element, and doctype script: Update name validation for attribute, element, and doctype Jun 30, 2025
@minghuaw minghuaw force-pushed the fix/wpt/dom/nodes/Document-createAttribute branch from e6fa526 to 34c2623 Compare July 2, 2025 00:56
@@ -120,9 +123,7 @@ use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::bindings::trace::{HashMapTracedValues, NoTrace};
#[cfg(feature = "webgpu")]
use crate::dom::bindings::weakref::WeakRef;
use crate::dom::bindings::xmlname::{
matches_name_production, namespace_from_domstring, validate_and_extract,
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these methods still being used?

If not, let's remove them. Otherwise we need to somehow unify them with the new methods introduced by this PR.

The main consideration is to avoid maintaining multiple mechanisms for the same thing.

Copy link
Contributor Author

@minghuaw minghuaw Jul 2, 2025

Choose a reason for hiding this comment

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

matches_name_production is still being used in two places that still requires XML naming rules (CreateProcessingInstruction and set_custom_attribute, which do require XML naming after the spec update see whatwg/dom#1079 and https://html.spec.whatwg.org/multipage/dom.html#custom-data-attribute).

namespace_from_domstring is just a convenient conversion that's used in many places, but we could also move it into the name_validation mod.

There remains one usage of xmlname::validate_and_extract during evaluation of xpath, which I assume that XML naming rules is used. The relevant docs are not well maintained (see https://dom.spec.whatwg.org/#xpath)

Copy link
Contributor

Choose a reason for hiding this comment

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

It appears that after this change, usage of xmlname will be the special case. We need to decide the fate of the exposed functions there:

  1. matches_name_production is well-scoped by XML and should remain there
  2. namespace_from_domstring seems still widely used and should be moved to the new file
  3. validate_and_extract and validate_and_extract_qualified_name no longer implement the spec, and in the best case should be removed. Could you list out links to all the the renaming usages and let's decide?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

3. `validate_and_extract` and `validate_and_extract_qualified_name` no longer implement the spec, and in the best case should be removed. Could you list out links to all the the renaming usages and let's decide?

This is the only remaining usage of the xmlname::validate_and_extract script::xpath::eval

if let Ok((ns, prefix, local)) = validate_and_extract(namespace, &qname_as_str) {
We could probably move validate_and_extract and validate_and_extract_qualified_name into this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved validate_and_extract and validate_and_extract_qualified_name from script/dom/bindings/xmlname.rs to script/xpath/eval.rs and restricted their visibility to private. See commit 73f22a5

@minghuaw minghuaw force-pushed the fix/wpt/dom/nodes/Document-createAttribute branch from 34c2623 to e609017 Compare July 2, 2025 07:40
@@ -120,9 +123,7 @@ use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::bindings::trace::{HashMapTracedValues, NoTrace};
#[cfg(feature = "webgpu")]
use crate::dom::bindings::weakref::WeakRef;
use crate::dom::bindings::xmlname::{
matches_name_production, namespace_from_domstring, validate_and_extract,
Copy link
Contributor

Choose a reason for hiding this comment

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

It appears that after this change, usage of xmlname will be the special case. We need to decide the fate of the exposed functions there:

  1. matches_name_production is well-scoped by XML and should remain there
  2. namespace_from_domstring seems still widely used and should be moved to the new file
  3. validate_and_extract and validate_and_extract_qualified_name no longer implement the spec, and in the best case should be removed. Could you list out links to all the the renaming usages and let's decide?

@minghuaw minghuaw force-pushed the fix/wpt/dom/nodes/Document-createAttribute branch 4 times, most recently from 710e2e7 to 935e866 Compare July 3, 2025 09:52
Copy link
Contributor

@xiaochengh xiaochengh left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@xiaochengh xiaochengh enabled auto-merge July 4, 2025 08:08
auto-merge was automatically disabled July 4, 2025 08:53

Head branch was pushed to by a user without write access

@minghuaw minghuaw force-pushed the fix/wpt/dom/nodes/Document-createAttribute branch from 935e866 to 558bd15 Compare July 4, 2025 08:53
@yezhizhen
Copy link
Contributor

Remember to update PR message as well, which would be the commit message in main.

@xiaochengh
Copy link
Contributor

Please let me know when this is ready for merging, thanks

@minghuaw
Copy link
Contributor Author

minghuaw commented Jul 4, 2025

Remember to update PR message as well, which would be the commit message in main.

@yezhizhen Do you mind taking a look at the updated PR message? Just trying to see if it's explanatory enough for someone who didn't review the changes? Thanks

@yezhizhen
Copy link
Contributor

@minghuaw It is very concise and understandable to me.

@minghuaw
Copy link
Contributor Author

Here's the updated WPT test result: https://github.com/minghuaw/servo/actions/runs/16188707259/job/45702433544.

@xiaochengh This is ready to get merged

@servo-wpt-sync
Copy link
Collaborator

🛠 These changes could not be applied onto the latest upstream WPT. Servo's copy of the Web Platform Tests may be out of sync.

@jdm jdm added this pull request to the merge queue Jul 11, 2025
minghuaw and others added 24 commits July 11, 2025 10:50
Signed-off-by: minghuaw <[email protected]>
Signed-off-by: minghuaw <[email protected]>
Signed-off-by: Minghua Wu <[email protected]>
Signed-off-by: Minghua Wu <[email protected]>
Signed-off-by: minghuaw <[email protected]>
Signed-off-by: minghuaw <[email protected]>
@minghuaw minghuaw force-pushed the fix/wpt/dom/nodes/Document-createAttribute branch from d6d9d53 to ebef119 Compare July 11, 2025 02:51
Merged via the queue into servo:main with commit 5b507dc Jul 11, 2025
2 checks passed
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.

Update validation of element, attribute, and doctype names
5 participants