script: Clean up attribute access a little bit in Element (#43064)

- Use modern Rust conveniences such as `unwrap_or_default`
 - Unabbreviate `attr`
- Unify the lowercase ASCII name assertion and make it a debug assertion
 - Use `unreachable!` instead of panic
- Expose two attribute getters that follow the behavior of two
specification concepts and what we expect internally in Servo:
- One that takes a namespace, but does not require lowercase attribute
names. ([specification
concept](https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace>))
- One that does not take a namespace, but does require lowercase
attribute names. ([specification
concept](https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name))
Testing: This should not really change behavior so should be covered by
existing tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson
2026-03-06 21:02:37 +01:00
committed by GitHub
parent 291e9e57cb
commit 401d327b96
23 changed files with 134 additions and 164 deletions

View File

@@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use html5ever::{LocalName, Prefix, local_name, ns};
use html5ever::{LocalName, Prefix, local_name};
use js::rust::HandleObject;
use servo_url::ServoUrl;
@@ -68,9 +68,7 @@ impl HTMLBaseElement {
let document = self.owner_document();
// Step 2. Let urlRecord be the result of parsing the value of element's href content attribute
// with document's fallback base URL, and document's character encoding. (Thus, the base element isn't affected by itself.)
let attr = self
.upcast::<Element>()
.get_attribute(&ns!(), &local_name!("href"));
let attr = self.upcast::<Element>().get_attribute(&local_name!("href"));
let Some(href_value) = attr.as_ref().map(|attr| attr.value()) else {
unreachable!("Must always have a href set when setting frozen base URL");
};
@@ -117,9 +115,7 @@ impl HTMLBaseElementMethods<crate::DomTypeHolder> for HTMLBaseElement {
let document = self.owner_document();
// Step 2. Let url be the value of the href attribute of this element, if it has one, and the empty string otherwise.
let attr = self
.upcast::<Element>()
.get_attribute(&ns!(), &local_name!("href"));
let attr = self.upcast::<Element>().get_attribute(&local_name!("href"));
let value = attr.as_ref().map(|attr| attr.value());
let url = value.as_ref().map_or("", |value| &**value);