1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 11:07:35 +00:00

LibWeb+WebContent: Forbid access to underlying type of CSSPixels

Although DistinctNumeric, which is supposed to abstract the underlying
type, was used to represent CSSPixels, we have a whole bunch of places
in the layout code that assume CSSPixels::value() returns a
floating-point type. This assumption makes it difficult to replace the
underlying type in CSSPixels with a non-floating type.

To make it easier to transition CSSPixels to fixed-point math, one step
we can take is to prevent access to the underlying type using value()
and instead use explicit conversions with the to_float(), to_double(),
and to_int() methods.
This commit is contained in:
Aliaksandr Kalenik 2023-06-12 21:37:35 +03:00 committed by Andreas Kling
parent 5a54c686a7
commit 147c3b3d97
43 changed files with 340 additions and 220 deletions

View file

@ -164,7 +164,7 @@ int HTMLElement::offset_top() const
return 0;
auto position = layout_node()->box_type_agnostic_position();
auto parent_position = parent_element()->layout_node()->box_type_agnostic_position();
return position.y().value() - parent_position.y().value();
return position.y().to_int() - parent_position.y().to_int();
}
// https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetleft
@ -177,7 +177,7 @@ int HTMLElement::offset_left() const
return 0;
auto position = layout_node()->box_type_agnostic_position();
auto parent_position = parent_element()->layout_node()->box_type_agnostic_position();
return position.x().value() - parent_position.x().value();
return position.x().to_int() - parent_position.x().to_int();
}
// https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetwidth
@ -193,7 +193,7 @@ int HTMLElement::offset_width() const
// 2. Return the width of the axis-aligned bounding box of the border boxes of all fragments generated by the elements principal box,
// ignoring any transforms that apply to the element and its ancestors.
// FIXME: Account for inline boxes.
return paintable_box()->border_box_width().value();
return paintable_box()->border_box_width().to_int();
}
// https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetheight
@ -209,7 +209,7 @@ int HTMLElement::offset_height() const
// 2. Return the height of the axis-aligned bounding box of the border boxes of all fragments generated by the elements principal box,
// ignoring any transforms that apply to the element and its ancestors.
// FIXME: Account for inline boxes.
return paintable_box()->border_box_height().value();
return paintable_box()->border_box_height().to_int();
}
// https://html.spec.whatwg.org/multipage/links.html#cannot-navigate

View file

@ -152,7 +152,7 @@ unsigned HTMLImageElement::width() const
// Return the rendered width of the image, in CSS pixels, if the image is being rendered.
if (auto* paintable_box = this->paintable_box())
return paintable_box->content_width().value();
return paintable_box->content_width().to_int();
// NOTE: This step seems to not be in the spec, but all browsers do it.
auto width_attr = get_attribute(HTML::AttributeNames::width);
@ -180,7 +180,7 @@ unsigned HTMLImageElement::height() const
// Return the rendered height of the image, in CSS pixels, if the image is being rendered.
if (auto* paintable_box = this->paintable_box())
return paintable_box->content_height().value();
return paintable_box->content_height().to_int();
// NOTE: This step seems to not be in the spec, but all browsers do it.
auto height_attr = get_attribute(HTML::AttributeNames::height);

View file

@ -400,7 +400,7 @@ void SourceSet::normalize_source_densities()
auto& width_descriptor = image_source.descriptor.get<ImageSource::WidthDescriptorValue>();
if (source_size.is_absolute()) {
image_source.descriptor = ImageSource::PixelDensityDescriptorValue {
.value = (width_descriptor.value / source_size.absolute_length_to_px()).value()
.value = (width_descriptor.value / source_size.absolute_length_to_px()).to_double()
};
} else {
dbgln("FIXME: Handle relative sizes: {}", source_size);

View file

@ -476,12 +476,12 @@ Optional<CSS::MediaFeatureValue> Window::query_media_feature(CSS::MediaFeatureID
// FIXME: device-aspect-ratio
case CSS::MediaFeatureID::DeviceHeight:
if (auto* page = this->page()) {
return CSS::MediaFeatureValue(CSS::Length::make_px(page->web_exposed_screen_area().height().value()));
return CSS::MediaFeatureValue(CSS::Length::make_px(page->web_exposed_screen_area().height().to_double()));
}
return CSS::MediaFeatureValue(0);
case CSS::MediaFeatureID::DeviceWidth:
if (auto* page = this->page()) {
return CSS::MediaFeatureValue(CSS::Length::make_px(page->web_exposed_screen_area().width().value()));
return CSS::MediaFeatureValue(CSS::Length::make_px(page->web_exposed_screen_area().width().to_double()));
}
return CSS::MediaFeatureValue(0);
case CSS::MediaFeatureID::DisplayMode:
@ -1032,7 +1032,7 @@ i32 Window::inner_width() const
// The innerWidth attribute must return the viewport width including the size of a rendered scroll bar (if any),
// or zero if there is no viewport.
if (auto const* browsing_context = associated_document().browsing_context())
return browsing_context->viewport_rect().width().value();
return browsing_context->viewport_rect().width().to_int();
return 0;
}
@ -1042,7 +1042,7 @@ i32 Window::inner_height() const
// The innerHeight attribute must return the viewport height including the size of a rendered scroll bar (if any),
// or zero if there is no viewport.
if (auto const* browsing_context = associated_document().browsing_context())
return browsing_context->viewport_rect().height().value();
return browsing_context->viewport_rect().height().to_int();
return 0;
}
@ -1052,7 +1052,7 @@ double Window::scroll_x() const
// The scrollX attribute must return the x-coordinate, relative to the initial containing block origin,
// of the left of the viewport, or zero if there is no viewport.
if (auto* page = this->page())
return page->top_level_browsing_context().viewport_scroll_offset().x().value();
return page->top_level_browsing_context().viewport_scroll_offset().x().to_double();
return 0;
}
@ -1062,7 +1062,7 @@ double Window::scroll_y() const
// The scrollY attribute must return the y-coordinate, relative to the initial containing block origin,
// of the top of the viewport, or zero if there is no viewport.
if (auto* page = this->page())
return page->top_level_browsing_context().viewport_scroll_offset().y().value();
return page->top_level_browsing_context().viewport_scroll_offset().y().to_double();
return 0;
}