mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Auto merge of #25572 - pshaughn:atomnames, r=jdm
Make name content attributes consistently atoms and put them in rare_data for fast access <!-- Please describe your changes on the following line: --> All codepaths setting the name content attribute now use an atom, which is also stored in rare_data for direct lookup by a get_name method. Paralleling the get_name method, I added a get_id method, which makes some internal id-lookup cases nicer. A new test tests for a name setter on every HTML element type. In addition to its overt and upstreamable purpose of checking IDL property reflection semantics, for us this test also hits some Servo assertions that make sure the name is an atom in every case. If the test doesn't crash, even a failed test case still has the attribute as an atom rather than some other type. The failed cases are for elements that we have unimplemented or completely stubbed; I added a few missing name IDL properties to otherwise implemented elements. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #25570 and make progress on #25057 <!-- Either: --> - [X] There are tests for these changes <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
This commit is contained in:
commit
9c135f0e95
32 changed files with 270 additions and 652 deletions
|
@ -1420,11 +1420,28 @@ impl Element {
|
|||
// https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
|
||||
pub fn get_attribute_by_name(&self, name: DOMString) -> Option<DomRoot<Attr>> {
|
||||
let name = &self.parsed_name(name);
|
||||
self.attrs
|
||||
let maybe_attribute = self
|
||||
.attrs
|
||||
.borrow()
|
||||
.iter()
|
||||
.find(|a| a.name() == name)
|
||||
.map(|js| DomRoot::from_ref(&**js))
|
||||
.map(|js| DomRoot::from_ref(&**js));
|
||||
|
||||
fn id_and_name_must_be_atoms(name: &LocalName, maybe_attr: &Option<DomRoot<Attr>>) -> bool {
|
||||
if *name == local_name!("id") || *name == local_name!("name") {
|
||||
match maybe_attr {
|
||||
None => true,
|
||||
Some(ref attr) => match *attr.value() {
|
||||
AttrValue::Atom(_) => true,
|
||||
_ => false,
|
||||
},
|
||||
}
|
||||
} else {
|
||||
true
|
||||
}
|
||||
}
|
||||
debug_assert!(id_and_name_must_be_atoms(name, &maybe_attribute));
|
||||
maybe_attribute
|
||||
}
|
||||
|
||||
pub fn set_attribute_from_parser(
|
||||
|
@ -1819,6 +1836,14 @@ impl Element {
|
|||
let other = other.upcast::<Element>();
|
||||
self.root_element() == other.root_element()
|
||||
}
|
||||
|
||||
pub fn get_id(&self) -> Option<Atom> {
|
||||
self.id_attribute.borrow().clone()
|
||||
}
|
||||
|
||||
pub fn get_name(&self) -> Option<Atom> {
|
||||
self.rare_data().as_ref()?.name_attribute.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl ElementMethods for Element {
|
||||
|
@ -1855,6 +1880,8 @@ impl ElementMethods for Element {
|
|||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#dom-element-id
|
||||
// This always returns a string; if you'd rather see None
|
||||
// on a null id, call get_id
|
||||
fn Id(&self) -> DOMString {
|
||||
self.get_string_attribute(&local_name!("id"))
|
||||
}
|
||||
|
@ -2802,6 +2829,20 @@ impl VirtualMethods for Element {
|
|||
}
|
||||
}
|
||||
},
|
||||
&local_name!("name") => {
|
||||
// Keep the name in rare data for fast access
|
||||
self.ensure_rare_data().name_attribute =
|
||||
mutation.new_value(attr).and_then(|value| {
|
||||
let value = value.as_atom();
|
||||
if value != &atom!("") {
|
||||
Some(value.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
});
|
||||
// TODO: notify the document about the name change
|
||||
// once it has a name_map (#25548)
|
||||
},
|
||||
_ => {
|
||||
// FIXME(emilio): This is pretty dubious, and should be done in
|
||||
// the relevant super-classes.
|
||||
|
@ -2820,6 +2861,7 @@ impl VirtualMethods for Element {
|
|||
fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
|
||||
match name {
|
||||
&local_name!("id") => AttrValue::from_atomic(value.into()),
|
||||
&local_name!("name") => AttrValue::from_atomic(value.into()),
|
||||
&local_name!("class") => AttrValue::from_serialized_tokenlist(value.into()),
|
||||
_ => self
|
||||
.super_type()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue