html: Add missing 'width' and 'height' reflected IDL dimension attributes (#39606)

The HTML specification defines the 'width' and 'height' attributes as
dimension attributes for the embedded content and images (img, iframe,
embed, object, video, source, input with image type)
https://html.spec.whatwg.org/multipage/#dimension-attributes and for the
tables (table, col, colgroup, thead, tbody, tfoot, tr, td, th) even
these attributes are marked as obsolete.

And UA are expected to use these 'width' and 'height' attributes as
style presentational hints for the rendering.

The embedded content and images:
https://html.spec.whatwg.org/multipage/#dimRendering

The tables:
https://html.spec.whatwg.org/multipage/#tables-2:the-table-element-4

Added missing 'width' and/or 'height' reflected IDL attributes:
- conformant 'unsigned long' IDL attributes for the 'img, source, video'
(with new macro 'make_dimension_uint*)
- obsolete 'DOMString' IDL attributes for the 'td, th, col, colgroup'

Moved the `fn attribute_affects_presentational_hints()` from Element to
specific HTML and SVG elements with attributes which affects
presentational hints for rendering.

Testing: Improvements in the following tests
- html/dom/idlharness.https.html
- html/dom/reflection-embedded.html
- html/dom/reflection-tabular.html
-
html/rendering/replaced-elements/attributes-for-embedded-content-and-images/picture-aspect-ratio.html

Signed-off-by: Andrei Volykhin <andrei.volykhin@gmail.com>
This commit is contained in:
Andrei Volykhin
2025-10-09 21:03:47 +03:00
committed by GitHub
parent 7d811f1d2a
commit ef9f16027b
26 changed files with 297 additions and 1044 deletions

View File

@@ -5,6 +5,7 @@
use dom_struct::dom_struct;
use html5ever::{LocalName, Prefix, local_name};
use js::rust::HandleObject;
use style::attr::AttrValue;
use crate::dom::attr::Attr;
use crate::dom::bindings::codegen::Bindings::HTMLSourceElementBinding::HTMLSourceElementMethods;
@@ -18,7 +19,7 @@ use crate::dom::html::htmlelement::HTMLElement;
use crate::dom::html::htmlimageelement::HTMLImageElement;
use crate::dom::html::htmlmediaelement::HTMLMediaElement;
use crate::dom::html::htmlpictureelement::HTMLPictureElement;
use crate::dom::node::{BindContext, Node, UnbindContext};
use crate::dom::node::{BindContext, Node, NodeDamage, UnbindContext};
use crate::dom::virtualmethods::VirtualMethods;
use crate::script_runtime::CanGc;
@@ -58,11 +59,11 @@ impl HTMLSourceElement {
fn iterate_next_html_image_element_siblings(
next_siblings_iterator: impl Iterator<Item = Root<Dom<Node>>>,
can_gc: CanGc,
callback: impl Fn(&HTMLImageElement),
) {
for next_sibling in next_siblings_iterator {
if let Some(html_image_element_sibling) = next_sibling.downcast::<HTMLImageElement>() {
html_image_element_sibling.update_the_image_data(can_gc);
callback(html_image_element_sibling);
}
}
}
@@ -77,17 +78,36 @@ impl VirtualMethods for HTMLSourceElement {
self.super_type()
.unwrap()
.attribute_mutated(attr, mutation, can_gc);
match attr.local_name() {
&local_name!("srcset") |
&local_name!("sizes") |
&local_name!("media") |
&local_name!("type") => {
// <https://html.spec.whatwg.org/multipage/#reacting-to-dom-mutations>
// The element's parent is a picture element and a source element that is a previous
// sibling has its srcset, sizes, media, type attributes set, changed, or removed.
if let Some(parent) = self.upcast::<Node>().GetParentElement() {
if parent.is::<HTMLPictureElement>() {
let next_sibling_iterator = self.upcast::<Node>().following_siblings();
HTMLSourceElement::iterate_next_html_image_element_siblings(
next_sibling_iterator,
can_gc,
|image| image.update_the_image_data(can_gc),
);
}
}
},
&local_name!("width") | &local_name!("height") => {
// Note: Despite being explicitly stated in the specification that any width or
// height attributes changes (set, changed, removed) of the source element should be
// counted as relevant mutation for the sibling image element, these attributes
// affect only the style presentational hints of the image element.
if let Some(parent) = self.upcast::<Node>().GetParentElement() {
if parent.is::<HTMLPictureElement>() {
let next_sibling_iterator = self.upcast::<Node>().following_siblings();
HTMLSourceElement::iterate_next_html_image_element_siblings(
next_sibling_iterator,
|image| image.upcast::<Node>().dirty(NodeDamage::Other),
);
}
}
@@ -96,6 +116,18 @@ impl VirtualMethods for HTMLSourceElement {
}
}
fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
match name {
&local_name!("width") | &local_name!("height") => {
AttrValue::from_dimension(value.into())
},
_ => self
.super_type()
.unwrap()
.parse_plain_attribute(name, value),
}
}
/// <https://html.spec.whatwg.org/multipage/#the-source-element:html-element-insertion-steps>
fn bind_to_tree(&self, context: &BindContext, can_gc: CanGc) {
self.super_type().unwrap().bind_to_tree(context, can_gc);
@@ -118,7 +150,7 @@ impl VirtualMethods for HTMLSourceElement {
let next_sibling_iterator = self.upcast::<Node>().following_siblings();
HTMLSourceElement::iterate_next_html_image_element_siblings(
next_sibling_iterator,
can_gc,
|image| image.update_the_image_data(can_gc),
);
}
}
@@ -134,7 +166,7 @@ impl VirtualMethods for HTMLSourceElement {
let next_sibling_iterator = next_sibling.inclusively_following_siblings();
HTMLSourceElement::iterate_next_html_image_element_siblings(
next_sibling_iterator,
can_gc,
|image| image.update_the_image_data(can_gc),
);
}
}
@@ -171,4 +203,16 @@ impl HTMLSourceElementMethods<crate::DomTypeHolder> for HTMLSourceElement {
// https://html.spec.whatwg.org/multipage/#dom-source-media
make_setter!(SetMedia, "media");
// <https://html.spec.whatwg.org/multipage/#dom-source-width>
make_dimension_uint_getter!(Width, "width");
// <https://html.spec.whatwg.org/multipage/#dom-source-width>
make_dimension_uint_setter!(SetWidth, "width");
// <https://html.spec.whatwg.org/multipage/#dom-source-height>
make_dimension_uint_getter!(Height, "height");
// <https://html.spec.whatwg.org/multipage/#dom-source-height>
make_dimension_uint_setter!(SetHeight, "height");
}