Node.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Demangle.h>
  8. #include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
  9. #include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
  10. #include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
  11. #include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
  12. #include <LibWeb/CSS/StyleValues/EdgeStyleValue.h>
  13. #include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
  14. #include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
  15. #include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
  16. #include <LibWeb/CSS/StyleValues/MathDepthStyleValue.h>
  17. #include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
  18. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  19. #include <LibWeb/CSS/StyleValues/RatioStyleValue.h>
  20. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  21. #include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
  22. #include <LibWeb/CSS/StyleValues/URLStyleValue.h>
  23. #include <LibWeb/DOM/Document.h>
  24. #include <LibWeb/Dump.h>
  25. #include <LibWeb/HTML/BrowsingContext.h>
  26. #include <LibWeb/HTML/HTMLHtmlElement.h>
  27. #include <LibWeb/Layout/BlockContainer.h>
  28. #include <LibWeb/Layout/FormattingContext.h>
  29. #include <LibWeb/Layout/Node.h>
  30. #include <LibWeb/Layout/TableWrapper.h>
  31. #include <LibWeb/Layout/TextNode.h>
  32. #include <LibWeb/Layout/Viewport.h>
  33. #include <LibWeb/Page/Page.h>
  34. #include <LibWeb/Platform/FontPlugin.h>
  35. namespace Web::Layout {
  36. Node::Node(DOM::Document& document, DOM::Node* node)
  37. : m_dom_node(node ? *node : document)
  38. , m_browsing_context(*document.browsing_context())
  39. , m_anonymous(node == nullptr)
  40. {
  41. if (node)
  42. node->set_layout_node({}, *this);
  43. }
  44. Node::~Node() = default;
  45. void Node::visit_edges(Cell::Visitor& visitor)
  46. {
  47. Base::visit_edges(visitor);
  48. visitor.visit(m_dom_node);
  49. visitor.visit(m_paintable);
  50. visitor.visit(m_pseudo_element_generator);
  51. visitor.visit(m_browsing_context);
  52. TreeNode::visit_edges(visitor);
  53. }
  54. // https://www.w3.org/TR/css-display-3/#out-of-flow
  55. bool Node::is_out_of_flow(FormattingContext const& formatting_context) const
  56. {
  57. // A layout node is out of flow if either:
  58. // 1. It is floated (which requires that floating is not inhibited).
  59. if (!formatting_context.inhibits_floating() && computed_values().float_() != CSS::Float::None)
  60. return true;
  61. // 2. It is "absolutely positioned".
  62. if (is_absolutely_positioned())
  63. return true;
  64. return false;
  65. }
  66. bool Node::can_contain_boxes_with_position_absolute() const
  67. {
  68. if (computed_values().position() != CSS::Positioning::Static)
  69. return true;
  70. if (is<Viewport>(*this))
  71. return true;
  72. // https://w3c.github.io/csswg-drafts/css-transforms-1/#propdef-transform
  73. // Any computed value other than none for the transform affects containing block and stacking context
  74. if (!computed_values().transformations().is_empty())
  75. return true;
  76. return false;
  77. }
  78. static Box const* nearest_ancestor_capable_of_forming_a_containing_block(Node const& node)
  79. {
  80. for (auto const* ancestor = node.parent(); ancestor; ancestor = ancestor->parent()) {
  81. if (ancestor->is_block_container()
  82. || ancestor->display().is_flex_inside()
  83. || ancestor->display().is_grid_inside()
  84. || ancestor->is_svg_svg_box()) {
  85. return verify_cast<Box>(ancestor);
  86. }
  87. }
  88. return nullptr;
  89. }
  90. Box const* Node::containing_block() const
  91. {
  92. if (is<TextNode>(*this))
  93. return nearest_ancestor_capable_of_forming_a_containing_block(*this);
  94. auto position = computed_values().position();
  95. // https://drafts.csswg.org/css-position-3/#absolute-cb
  96. if (position == CSS::Positioning::Absolute) {
  97. auto const* ancestor = parent();
  98. while (ancestor && !ancestor->can_contain_boxes_with_position_absolute())
  99. ancestor = ancestor->parent();
  100. while (ancestor && ancestor->is_anonymous())
  101. ancestor = nearest_ancestor_capable_of_forming_a_containing_block(*ancestor);
  102. return static_cast<Box const*>(ancestor);
  103. }
  104. if (position == CSS::Positioning::Fixed)
  105. return &root();
  106. return nearest_ancestor_capable_of_forming_a_containing_block(*this);
  107. }
  108. Box const* Node::non_anonymous_containing_block() const
  109. {
  110. auto nearest_ancestor_box = containing_block();
  111. VERIFY(nearest_ancestor_box);
  112. while (nearest_ancestor_box->is_anonymous()) {
  113. nearest_ancestor_box = nearest_ancestor_box->containing_block();
  114. VERIFY(nearest_ancestor_box);
  115. }
  116. return nearest_ancestor_box;
  117. }
  118. // https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context
  119. bool Node::establishes_stacking_context() const
  120. {
  121. // NOTE: While MDN is not authoritative, there isn't a single convenient location
  122. // in the CSS specifications where the rules for stacking contexts is described.
  123. // That's why the "spec link" here points to MDN.
  124. if (!has_style())
  125. return false;
  126. // We make a stacking context for the viewport. Painting and hit testing starts from here.
  127. if (is_viewport())
  128. return true;
  129. // Root element of the document (<html>).
  130. if (is_root_element())
  131. return true;
  132. auto position = computed_values().position();
  133. // Element with a position value absolute or relative and z-index value other than auto.
  134. if (position == CSS::Positioning::Absolute || position == CSS::Positioning::Relative) {
  135. if (computed_values().z_index().has_value()) {
  136. return true;
  137. }
  138. }
  139. // Element with a position value fixed or sticky.
  140. if (position == CSS::Positioning::Fixed || position == CSS::Positioning::Sticky)
  141. return true;
  142. if (!computed_values().transformations().is_empty())
  143. return true;
  144. // Element that is a child of a flex container, with z-index value other than auto.
  145. if (parent() && parent()->display().is_flex_inside() && computed_values().z_index().has_value())
  146. return true;
  147. // Element that is a child of a grid container, with z-index value other than auto.
  148. if (parent() && parent()->display().is_grid_inside() && computed_values().z_index().has_value())
  149. return true;
  150. // https://drafts.fxtf.org/filter-effects-2/#backdrop-filter-operation
  151. // A computed value of other than none results in the creation of both a stacking context [CSS21] and a Containing Block for absolute and fixed position descendants,
  152. // unless the element it applies to is a document root element in the current browsing context.
  153. // Spec Note: This rule works in the same way as for the filter property.
  154. if (!computed_values().backdrop_filter().is_none())
  155. return true;
  156. // Element with any of the following properties with value other than none:
  157. // - transform
  158. // - filter
  159. // - backdrop-filter
  160. // - perspective
  161. // - clip-path
  162. // - mask / mask-image / mask-border
  163. if (computed_values().mask().has_value())
  164. return true;
  165. return computed_values().opacity() < 1.0f;
  166. }
  167. HTML::BrowsingContext const& Node::browsing_context() const
  168. {
  169. return *m_browsing_context;
  170. }
  171. HTML::BrowsingContext& Node::browsing_context()
  172. {
  173. return *m_browsing_context;
  174. }
  175. JS::GCPtr<HTML::Navigable> Node::navigable() const
  176. {
  177. return document().navigable();
  178. }
  179. Viewport const& Node::root() const
  180. {
  181. VERIFY(document().layout_node());
  182. return *document().layout_node();
  183. }
  184. Viewport& Node::root()
  185. {
  186. VERIFY(document().layout_node());
  187. return *document().layout_node();
  188. }
  189. void Node::set_needs_display()
  190. {
  191. auto* containing_block = this->containing_block();
  192. if (!containing_block)
  193. return;
  194. if (!containing_block->paintable_box())
  195. return;
  196. if (!is<Painting::PaintableWithLines>(*containing_block->paintable_box()))
  197. return;
  198. static_cast<Painting::PaintableWithLines const&>(*containing_block->paintable_box()).for_each_fragment([&](auto& fragment) {
  199. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  200. if (navigable())
  201. navigable()->set_needs_display(fragment.absolute_rect());
  202. }
  203. return IterationDecision::Continue;
  204. });
  205. }
  206. CSSPixelPoint Node::box_type_agnostic_position() const
  207. {
  208. if (is<Box>(*this))
  209. return verify_cast<Box>(*this).paintable_box()->absolute_position();
  210. VERIFY(is_inline());
  211. CSSPixelPoint position;
  212. if (auto* block = containing_block(); block && block->paintable() && is<Painting::PaintableWithLines>(*block->paintable())) {
  213. static_cast<Painting::PaintableWithLines const&>(*block->paintable_box()).for_each_fragment([&](auto& fragment) {
  214. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  215. position = fragment.absolute_rect().location();
  216. return IterationDecision::Break;
  217. }
  218. return IterationDecision::Continue;
  219. });
  220. }
  221. return position;
  222. }
  223. bool Node::is_floating() const
  224. {
  225. if (!has_style())
  226. return false;
  227. // flex-items don't float.
  228. if (is_flex_item())
  229. return false;
  230. return computed_values().float_() != CSS::Float::None;
  231. }
  232. bool Node::is_positioned() const
  233. {
  234. return has_style() && computed_values().position() != CSS::Positioning::Static;
  235. }
  236. bool Node::is_absolutely_positioned() const
  237. {
  238. if (!has_style())
  239. return false;
  240. auto position = computed_values().position();
  241. return position == CSS::Positioning::Absolute || position == CSS::Positioning::Fixed;
  242. }
  243. bool Node::is_fixed_position() const
  244. {
  245. if (!has_style())
  246. return false;
  247. auto position = computed_values().position();
  248. return position == CSS::Positioning::Fixed;
  249. }
  250. NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> computed_style)
  251. : Node(document, node)
  252. {
  253. m_has_style = true;
  254. apply_style(*computed_style);
  255. }
  256. NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  257. : Node(document, node)
  258. , m_computed_values(move(computed_values))
  259. {
  260. m_has_style = true;
  261. }
  262. void NodeWithStyle::visit_edges(Visitor& visitor)
  263. {
  264. Base::visit_edges(visitor);
  265. for (auto& layer : m_computed_values.background_layers()) {
  266. if (layer.background_image && layer.background_image->is_image())
  267. layer.background_image->as_image().visit_edges(visitor);
  268. }
  269. if (m_list_style_image && m_list_style_image->is_image())
  270. m_list_style_image->as_image().visit_edges(visitor);
  271. }
  272. // https://www.w3.org/TR/css-values-4/#snap-a-length-as-a-border-width
  273. static CSSPixels snap_a_length_as_a_border_width(double device_pixels_per_css_pixel, CSSPixels length)
  274. {
  275. // 1. Assert: len is non-negative.
  276. VERIFY(length >= 0);
  277. // 2. If len is an integer number of device pixels, do nothing.
  278. auto device_pixels = length.to_double() * device_pixels_per_css_pixel;
  279. if (device_pixels == trunc(device_pixels))
  280. return length;
  281. // 3. If len is greater than zero, but less than 1 device pixel, round len up to 1 device pixel.
  282. if (device_pixels > 0 && device_pixels < 1)
  283. return CSSPixels::nearest_value_for(1 / device_pixels_per_css_pixel);
  284. // 4. If len is greater than 1 device pixel, round it down to the nearest integer number of device pixels.
  285. if (device_pixels > 1)
  286. return CSSPixels::nearest_value_for(floor(device_pixels) / device_pixels_per_css_pixel);
  287. return length;
  288. }
  289. void NodeWithStyle::apply_style(const CSS::StyleProperties& computed_style)
  290. {
  291. auto& computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
  292. // NOTE: color must be set first to ensure currentColor can be resolved in other properties (e.g. background-color).
  293. computed_values.set_color(computed_style.color_or_fallback(CSS::PropertyID::Color, *this, CSS::InitialValues::color()));
  294. // NOTE: We have to be careful that font-related properties get set in the right order.
  295. // m_font is used by Length::to_px() when resolving sizes against this layout node.
  296. // That's why it has to be set before everything else.
  297. computed_values.set_font_list(computed_style.computed_font_list());
  298. computed_values.set_font_size(computed_style.property(CSS::PropertyID::FontSize)->as_length().length().to_px(*this));
  299. computed_values.set_font_weight(round_to<int>(computed_style.property(CSS::PropertyID::FontWeight)->as_number().number()));
  300. computed_values.set_line_height(computed_style.line_height());
  301. computed_values.set_vertical_align(computed_style.vertical_align());
  302. {
  303. auto attachments = computed_style.property(CSS::PropertyID::BackgroundAttachment);
  304. auto clips = computed_style.property(CSS::PropertyID::BackgroundClip);
  305. auto images = computed_style.property(CSS::PropertyID::BackgroundImage);
  306. auto origins = computed_style.property(CSS::PropertyID::BackgroundOrigin);
  307. auto x_positions = computed_style.property(CSS::PropertyID::BackgroundPositionX);
  308. auto y_positions = computed_style.property(CSS::PropertyID::BackgroundPositionY);
  309. auto repeats = computed_style.property(CSS::PropertyID::BackgroundRepeat);
  310. auto sizes = computed_style.property(CSS::PropertyID::BackgroundSize);
  311. auto count_layers = [](auto maybe_style_value) -> size_t {
  312. if (maybe_style_value->is_value_list())
  313. return maybe_style_value->as_value_list().size();
  314. else
  315. return 1;
  316. };
  317. auto value_for_layer = [](auto& style_value, size_t layer_index) -> RefPtr<CSS::StyleValue const> {
  318. if (style_value->is_value_list())
  319. return style_value->as_value_list().value_at(layer_index, true);
  320. return style_value;
  321. };
  322. size_t layer_count = 1;
  323. layer_count = max(layer_count, count_layers(attachments));
  324. layer_count = max(layer_count, count_layers(clips));
  325. layer_count = max(layer_count, count_layers(images));
  326. layer_count = max(layer_count, count_layers(origins));
  327. layer_count = max(layer_count, count_layers(x_positions));
  328. layer_count = max(layer_count, count_layers(y_positions));
  329. layer_count = max(layer_count, count_layers(repeats));
  330. layer_count = max(layer_count, count_layers(sizes));
  331. Vector<CSS::BackgroundLayerData> layers;
  332. layers.ensure_capacity(layer_count);
  333. for (size_t layer_index = 0; layer_index < layer_count; layer_index++) {
  334. CSS::BackgroundLayerData layer;
  335. if (auto image_value = value_for_layer(images, layer_index); image_value) {
  336. if (image_value->is_abstract_image()) {
  337. layer.background_image = image_value->as_abstract_image();
  338. const_cast<CSS::AbstractImageStyleValue&>(*layer.background_image).load_any_resources(document());
  339. }
  340. }
  341. if (auto attachment_value = value_for_layer(attachments, layer_index); attachment_value && attachment_value->is_identifier()) {
  342. switch (attachment_value->to_identifier()) {
  343. case CSS::ValueID::Fixed:
  344. layer.attachment = CSS::BackgroundAttachment::Fixed;
  345. break;
  346. case CSS::ValueID::Local:
  347. layer.attachment = CSS::BackgroundAttachment::Local;
  348. break;
  349. case CSS::ValueID::Scroll:
  350. layer.attachment = CSS::BackgroundAttachment::Scroll;
  351. break;
  352. default:
  353. break;
  354. }
  355. }
  356. auto as_box = [](auto value_id) {
  357. switch (value_id) {
  358. case CSS::ValueID::BorderBox:
  359. return CSS::BackgroundBox::BorderBox;
  360. case CSS::ValueID::ContentBox:
  361. return CSS::BackgroundBox::ContentBox;
  362. case CSS::ValueID::PaddingBox:
  363. return CSS::BackgroundBox::PaddingBox;
  364. default:
  365. VERIFY_NOT_REACHED();
  366. }
  367. };
  368. if (auto origin_value = value_for_layer(origins, layer_index); origin_value && origin_value->is_identifier()) {
  369. layer.origin = as_box(origin_value->to_identifier());
  370. }
  371. if (auto clip_value = value_for_layer(clips, layer_index); clip_value && clip_value->is_identifier()) {
  372. layer.clip = as_box(clip_value->to_identifier());
  373. }
  374. if (auto position_value = value_for_layer(x_positions, layer_index); position_value && position_value->is_edge()) {
  375. auto& position = position_value->as_edge();
  376. layer.position_edge_x = position.edge();
  377. layer.position_offset_x = position.offset();
  378. }
  379. if (auto position_value = value_for_layer(y_positions, layer_index); position_value && position_value->is_edge()) {
  380. auto& position = position_value->as_edge();
  381. layer.position_edge_y = position.edge();
  382. layer.position_offset_y = position.offset();
  383. };
  384. if (auto size_value = value_for_layer(sizes, layer_index); size_value) {
  385. if (size_value->is_background_size()) {
  386. auto& size = size_value->as_background_size();
  387. layer.size_type = CSS::BackgroundSize::LengthPercentage;
  388. layer.size_x = size.size_x();
  389. layer.size_y = size.size_y();
  390. } else if (size_value->is_identifier()) {
  391. switch (size_value->to_identifier()) {
  392. case CSS::ValueID::Contain:
  393. layer.size_type = CSS::BackgroundSize::Contain;
  394. break;
  395. case CSS::ValueID::Cover:
  396. layer.size_type = CSS::BackgroundSize::Cover;
  397. break;
  398. default:
  399. break;
  400. }
  401. }
  402. }
  403. if (auto repeat_value = value_for_layer(repeats, layer_index); repeat_value && repeat_value->is_background_repeat()) {
  404. layer.repeat_x = repeat_value->as_background_repeat().repeat_x();
  405. layer.repeat_y = repeat_value->as_background_repeat().repeat_y();
  406. }
  407. layers.append(move(layer));
  408. }
  409. computed_values.set_background_layers(move(layers));
  410. }
  411. computed_values.set_background_color(computed_style.color_or_fallback(CSS::PropertyID::BackgroundColor, *this, CSS::InitialValues::background_color()));
  412. if (auto box_sizing = computed_style.box_sizing(); box_sizing.has_value())
  413. computed_values.set_box_sizing(box_sizing.release_value());
  414. if (auto maybe_font_variant = computed_style.font_variant(); maybe_font_variant.has_value())
  415. computed_values.set_font_variant(maybe_font_variant.release_value());
  416. // FIXME: BorderXRadius properties are now BorderRadiusStyleValues, so make use of that.
  417. auto border_bottom_left_radius = computed_style.property(CSS::PropertyID::BorderBottomLeftRadius);
  418. if (border_bottom_left_radius->is_border_radius()) {
  419. computed_values.set_border_bottom_left_radius(
  420. CSS::BorderRadiusData {
  421. border_bottom_left_radius->as_border_radius().horizontal_radius(),
  422. border_bottom_left_radius->as_border_radius().vertical_radius() });
  423. }
  424. auto border_bottom_right_radius = computed_style.property(CSS::PropertyID::BorderBottomRightRadius);
  425. if (border_bottom_right_radius->is_border_radius()) {
  426. computed_values.set_border_bottom_right_radius(
  427. CSS::BorderRadiusData {
  428. border_bottom_right_radius->as_border_radius().horizontal_radius(),
  429. border_bottom_right_radius->as_border_radius().vertical_radius() });
  430. }
  431. auto border_top_left_radius = computed_style.property(CSS::PropertyID::BorderTopLeftRadius);
  432. if (border_top_left_radius->is_border_radius()) {
  433. computed_values.set_border_top_left_radius(
  434. CSS::BorderRadiusData {
  435. border_top_left_radius->as_border_radius().horizontal_radius(),
  436. border_top_left_radius->as_border_radius().vertical_radius() });
  437. }
  438. auto border_top_right_radius = computed_style.property(CSS::PropertyID::BorderTopRightRadius);
  439. if (border_top_right_radius->is_border_radius()) {
  440. computed_values.set_border_top_right_radius(
  441. CSS::BorderRadiusData {
  442. border_top_right_radius->as_border_radius().horizontal_radius(),
  443. border_top_right_radius->as_border_radius().vertical_radius() });
  444. }
  445. computed_values.set_display(computed_style.display());
  446. auto flex_direction = computed_style.flex_direction();
  447. if (flex_direction.has_value())
  448. computed_values.set_flex_direction(flex_direction.value());
  449. auto flex_wrap = computed_style.flex_wrap();
  450. if (flex_wrap.has_value())
  451. computed_values.set_flex_wrap(flex_wrap.value());
  452. auto flex_basis = computed_style.flex_basis();
  453. if (flex_basis.has_value())
  454. computed_values.set_flex_basis(flex_basis.value());
  455. computed_values.set_flex_grow(computed_style.flex_grow());
  456. computed_values.set_flex_shrink(computed_style.flex_shrink());
  457. computed_values.set_order(computed_style.order());
  458. computed_values.set_clip(computed_style.clip());
  459. if (computed_style.backdrop_filter().has_filters()) {
  460. CSS::ResolvedBackdropFilter resolved_backdrop_filter;
  461. for (auto& filter : computed_style.backdrop_filter().filters()) {
  462. filter.visit(
  463. [&](CSS::Filter::Blur const& blur) {
  464. resolved_backdrop_filter.filters.append(CSS::ResolvedBackdropFilter::Blur {
  465. .radius = blur.resolved_radius(*this) });
  466. },
  467. [&](CSS::Filter::DropShadow const& drop_shadow) {
  468. // The default value for omitted values is missing length values set to 0
  469. // and the missing used color is taken from the color property.
  470. resolved_backdrop_filter.filters.append(CSS::ResolvedBackdropFilter::DropShadow {
  471. .offset_x = drop_shadow.offset_x.to_px(*this).to_double(),
  472. .offset_y = drop_shadow.offset_y.to_px(*this).to_double(),
  473. .radius = drop_shadow.radius.has_value() ? drop_shadow.radius->to_px(*this).to_double() : 0.0,
  474. .color = drop_shadow.color.has_value() ? *drop_shadow.color : this->computed_values().color() });
  475. },
  476. [&](CSS::Filter::Color const& color_operation) {
  477. resolved_backdrop_filter.filters.append(CSS::ResolvedBackdropFilter::ColorOperation {
  478. .operation = color_operation.operation,
  479. .amount = color_operation.resolved_amount() });
  480. },
  481. [&](CSS::Filter::HueRotate const& hue_rotate) {
  482. resolved_backdrop_filter.filters.append(CSS::ResolvedBackdropFilter::HueRotate { .angle_degrees = hue_rotate.angle_degrees() });
  483. });
  484. }
  485. computed_values.set_backdrop_filter(resolved_backdrop_filter);
  486. }
  487. auto justify_content = computed_style.justify_content();
  488. if (justify_content.has_value())
  489. computed_values.set_justify_content(justify_content.value());
  490. auto justify_items = computed_style.justify_items();
  491. if (justify_items.has_value())
  492. computed_values.set_justify_items(justify_items.value());
  493. auto justify_self = computed_style.justify_self();
  494. if (justify_self.has_value())
  495. computed_values.set_justify_self(justify_self.value());
  496. auto accent_color = computed_style.accent_color(*this);
  497. if (accent_color.has_value())
  498. computed_values.set_accent_color(accent_color.value());
  499. auto align_content = computed_style.align_content();
  500. if (align_content.has_value())
  501. computed_values.set_align_content(align_content.value());
  502. auto align_items = computed_style.align_items();
  503. if (align_items.has_value())
  504. computed_values.set_align_items(align_items.value());
  505. auto align_self = computed_style.align_self();
  506. if (align_self.has_value())
  507. computed_values.set_align_self(align_self.value());
  508. auto appearance = computed_style.appearance();
  509. if (appearance.has_value())
  510. computed_values.set_appearance(appearance.value());
  511. auto position = computed_style.position();
  512. if (position.has_value())
  513. computed_values.set_position(position.value());
  514. auto text_align = computed_style.text_align();
  515. if (text_align.has_value())
  516. computed_values.set_text_align(text_align.value());
  517. auto text_justify = computed_style.text_justify();
  518. if (text_align.has_value())
  519. computed_values.set_text_justify(text_justify.value());
  520. if (auto text_indent = computed_style.length_percentage(CSS::PropertyID::TextIndent); text_indent.has_value())
  521. computed_values.set_text_indent(text_indent.release_value());
  522. auto white_space = computed_style.white_space();
  523. if (white_space.has_value())
  524. computed_values.set_white_space(white_space.value());
  525. auto float_ = computed_style.float_();
  526. if (float_.has_value())
  527. computed_values.set_float(float_.value());
  528. computed_values.set_border_spacing_horizontal(computed_style.border_spacing_horizontal());
  529. computed_values.set_border_spacing_vertical(computed_style.border_spacing_vertical());
  530. auto caption_side = computed_style.caption_side();
  531. if (caption_side.has_value())
  532. computed_values.set_caption_side(caption_side.value());
  533. auto clear = computed_style.clear();
  534. if (clear.has_value())
  535. computed_values.set_clear(clear.value());
  536. auto overflow_x = computed_style.overflow_x();
  537. if (overflow_x.has_value())
  538. computed_values.set_overflow_x(overflow_x.value());
  539. auto overflow_y = computed_style.overflow_y();
  540. if (overflow_y.has_value())
  541. computed_values.set_overflow_y(overflow_y.value());
  542. auto cursor = computed_style.cursor();
  543. if (cursor.has_value())
  544. computed_values.set_cursor(cursor.value());
  545. auto image_rendering = computed_style.image_rendering();
  546. if (image_rendering.has_value())
  547. computed_values.set_image_rendering(image_rendering.value());
  548. auto pointer_events = computed_style.pointer_events();
  549. if (pointer_events.has_value())
  550. computed_values.set_pointer_events(pointer_events.value());
  551. computed_values.set_text_decoration_line(computed_style.text_decoration_line());
  552. auto text_decoration_style = computed_style.text_decoration_style();
  553. if (text_decoration_style.has_value())
  554. computed_values.set_text_decoration_style(text_decoration_style.value());
  555. auto text_transform = computed_style.text_transform();
  556. if (text_transform.has_value())
  557. computed_values.set_text_transform(text_transform.value());
  558. if (auto list_style_type = computed_style.list_style_type(); list_style_type.has_value())
  559. computed_values.set_list_style_type(list_style_type.value());
  560. auto list_style_image = computed_style.property(CSS::PropertyID::ListStyleImage);
  561. if (list_style_image->is_abstract_image()) {
  562. m_list_style_image = list_style_image->as_abstract_image();
  563. const_cast<CSS::AbstractImageStyleValue&>(*m_list_style_image).load_any_resources(document());
  564. }
  565. if (auto list_style_position = computed_style.list_style_position(); list_style_position.has_value())
  566. computed_values.set_list_style_position(list_style_position.value());
  567. // FIXME: The default text decoration color value is `currentcolor`, but since we can't resolve that easily,
  568. // we just manually grab the value from `color`. This makes it dependent on `color` being
  569. // specified first, so it's far from ideal.
  570. computed_values.set_text_decoration_color(computed_style.color_or_fallback(CSS::PropertyID::TextDecorationColor, *this, computed_values.color()));
  571. if (auto maybe_text_decoration_thickness = computed_style.length_percentage(CSS::PropertyID::TextDecorationThickness); maybe_text_decoration_thickness.has_value())
  572. computed_values.set_text_decoration_thickness(maybe_text_decoration_thickness.release_value());
  573. computed_values.set_text_shadow(computed_style.text_shadow(*this));
  574. computed_values.set_z_index(computed_style.z_index());
  575. computed_values.set_opacity(computed_style.opacity());
  576. if (auto maybe_visibility = computed_style.visibility(); maybe_visibility.has_value())
  577. computed_values.set_visibility(maybe_visibility.release_value());
  578. computed_values.set_width(computed_style.size_value(CSS::PropertyID::Width));
  579. computed_values.set_min_width(computed_style.size_value(CSS::PropertyID::MinWidth));
  580. computed_values.set_max_width(computed_style.size_value(CSS::PropertyID::MaxWidth));
  581. computed_values.set_height(computed_style.size_value(CSS::PropertyID::Height));
  582. computed_values.set_min_height(computed_style.size_value(CSS::PropertyID::MinHeight));
  583. computed_values.set_max_height(computed_style.size_value(CSS::PropertyID::MaxHeight));
  584. computed_values.set_inset(computed_style.length_box(CSS::PropertyID::Left, CSS::PropertyID::Top, CSS::PropertyID::Right, CSS::PropertyID::Bottom, CSS::Length::make_auto()));
  585. computed_values.set_margin(computed_style.length_box(CSS::PropertyID::MarginLeft, CSS::PropertyID::MarginTop, CSS::PropertyID::MarginRight, CSS::PropertyID::MarginBottom, CSS::Length::make_px(0)));
  586. computed_values.set_padding(computed_style.length_box(CSS::PropertyID::PaddingLeft, CSS::PropertyID::PaddingTop, CSS::PropertyID::PaddingRight, CSS::PropertyID::PaddingBottom, CSS::Length::make_px(0)));
  587. computed_values.set_box_shadow(computed_style.box_shadow(*this));
  588. computed_values.set_transformations(computed_style.transformations());
  589. computed_values.set_transform_origin(computed_style.transform_origin());
  590. auto transition_delay_property = computed_style.property(CSS::PropertyID::TransitionDelay);
  591. if (transition_delay_property->is_time()) {
  592. auto& transition_delay = transition_delay_property->as_time();
  593. computed_values.set_transition_delay(transition_delay.time());
  594. } else if (transition_delay_property->is_calculated()) {
  595. auto& transition_delay = transition_delay_property->as_calculated();
  596. computed_values.set_transition_delay(transition_delay.resolve_time().value());
  597. }
  598. auto do_border_style = [&](CSS::BorderData& border, CSS::PropertyID width_property, CSS::PropertyID color_property, CSS::PropertyID style_property) {
  599. // FIXME: The default border color value is `currentcolor`, but since we can't resolve that easily,
  600. // we just manually grab the value from `color`. This makes it dependent on `color` being
  601. // specified first, so it's far from ideal.
  602. border.color = computed_style.color_or_fallback(color_property, *this, computed_values.color());
  603. border.line_style = computed_style.line_style(style_property).value_or(CSS::LineStyle::None);
  604. // https://w3c.github.io/csswg-drafts/css-backgrounds/#border-style
  605. // none
  606. // No border. Color and width are ignored (i.e., the border has width 0). Note this means that the initial value of border-image-width will also resolve to zero.
  607. // hidden
  608. // Same as none, but has different behavior in the border conflict resolution rules for border-collapsed tables [CSS2].
  609. if (border.line_style == CSS::LineStyle::None || border.line_style == CSS::LineStyle::Hidden) {
  610. border.width = 0;
  611. } else {
  612. auto resolve_border_width = [&]() -> CSSPixels {
  613. auto value = computed_style.property(width_property);
  614. if (value->is_calculated())
  615. return value->as_calculated().resolve_length(*this)->to_px(*this);
  616. if (value->is_length())
  617. return value->as_length().length().to_px(*this);
  618. if (value->is_identifier()) {
  619. // https://www.w3.org/TR/css-backgrounds-3/#valdef-line-width-thin
  620. switch (value->to_identifier()) {
  621. case CSS::ValueID::Thin:
  622. return 1;
  623. case CSS::ValueID::Medium:
  624. return 3;
  625. case CSS::ValueID::Thick:
  626. return 5;
  627. default:
  628. VERIFY_NOT_REACHED();
  629. }
  630. }
  631. VERIFY_NOT_REACHED();
  632. };
  633. border.width = snap_a_length_as_a_border_width(document().page().client().device_pixels_per_css_pixel(), resolve_border_width());
  634. }
  635. };
  636. do_border_style(computed_values.border_left(), CSS::PropertyID::BorderLeftWidth, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftStyle);
  637. do_border_style(computed_values.border_top(), CSS::PropertyID::BorderTopWidth, CSS::PropertyID::BorderTopColor, CSS::PropertyID::BorderTopStyle);
  638. do_border_style(computed_values.border_right(), CSS::PropertyID::BorderRightWidth, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightStyle);
  639. do_border_style(computed_values.border_bottom(), CSS::PropertyID::BorderBottomWidth, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomStyle);
  640. if (auto outline_color = computed_style.property(CSS::PropertyID::OutlineColor); outline_color->has_color())
  641. computed_values.set_outline_color(outline_color->to_color(*this));
  642. if (auto outline_offset = computed_style.property(CSS::PropertyID::OutlineOffset); outline_offset->is_length())
  643. computed_values.set_outline_offset(outline_offset->as_length().length());
  644. if (auto outline_style = computed_style.outline_style(); outline_style.has_value())
  645. computed_values.set_outline_style(outline_style.value());
  646. if (auto outline_width = computed_style.property(CSS::PropertyID::OutlineWidth); outline_width->is_length())
  647. computed_values.set_outline_width(outline_width->as_length().length());
  648. // FIXME: Stop generating the content twice. (First time is in TreeBuilder.)
  649. computed_values.set_content(computed_style.content(initial_quote_nesting_level()).content_data);
  650. computed_values.set_grid_auto_columns(computed_style.grid_auto_columns());
  651. computed_values.set_grid_auto_rows(computed_style.grid_auto_rows());
  652. computed_values.set_grid_template_columns(computed_style.grid_template_columns());
  653. computed_values.set_grid_template_rows(computed_style.grid_template_rows());
  654. computed_values.set_grid_column_end(computed_style.grid_column_end());
  655. computed_values.set_grid_column_start(computed_style.grid_column_start());
  656. computed_values.set_grid_row_end(computed_style.grid_row_end());
  657. computed_values.set_grid_row_start(computed_style.grid_row_start());
  658. computed_values.set_grid_template_areas(computed_style.grid_template_areas());
  659. computed_values.set_grid_auto_flow(computed_style.grid_auto_flow());
  660. auto fill = computed_style.property(CSS::PropertyID::Fill);
  661. if (fill->has_color())
  662. computed_values.set_fill(fill->to_color(*this));
  663. else if (fill->is_url())
  664. computed_values.set_fill(fill->as_url().url());
  665. auto stroke = computed_style.property(CSS::PropertyID::Stroke);
  666. if (stroke->has_color())
  667. computed_values.set_stroke(stroke->to_color(*this));
  668. else if (stroke->is_url())
  669. computed_values.set_stroke(stroke->as_url().url());
  670. if (auto stop_color = computed_style.property(CSS::PropertyID::StopColor); stop_color->has_color())
  671. computed_values.set_stop_color(stop_color->to_color(*this));
  672. auto stroke_width = computed_style.property(CSS::PropertyID::StrokeWidth);
  673. // FIXME: Converting to pixels isn't really correct - values should be in "user units"
  674. // https://svgwg.org/svg2-draft/coords.html#TermUserUnits
  675. if (stroke_width->is_number())
  676. computed_values.set_stroke_width(CSS::Length::make_px(CSSPixels::nearest_value_for(stroke_width->as_number().number())));
  677. else if (stroke_width->is_length())
  678. computed_values.set_stroke_width(stroke_width->as_length().length());
  679. else if (stroke_width->is_percentage())
  680. computed_values.set_stroke_width(CSS::LengthPercentage { stroke_width->as_percentage().percentage() });
  681. if (auto mask_type = computed_style.mask_type(); mask_type.has_value())
  682. computed_values.set_mask_type(*mask_type);
  683. if (auto mask = computed_style.property(CSS::PropertyID::Mask); mask->is_url())
  684. computed_values.set_mask(mask->as_url().url());
  685. if (auto fill_rule = computed_style.fill_rule(); fill_rule.has_value())
  686. computed_values.set_fill_rule(*fill_rule);
  687. computed_values.set_fill_opacity(computed_style.fill_opacity());
  688. computed_values.set_stroke_opacity(computed_style.stroke_opacity());
  689. computed_values.set_stop_opacity(computed_style.stop_opacity());
  690. if (auto text_anchor = computed_style.text_anchor(); text_anchor.has_value())
  691. computed_values.set_text_anchor(*text_anchor);
  692. if (auto column_count = computed_style.property(CSS::PropertyID::ColumnCount); column_count->is_integer())
  693. computed_values.set_column_count(CSS::ColumnCount::make_integer(column_count->as_integer().integer()));
  694. computed_values.set_column_gap(computed_style.size_value(CSS::PropertyID::ColumnGap));
  695. computed_values.set_row_gap(computed_style.size_value(CSS::PropertyID::RowGap));
  696. if (auto border_collapse = computed_style.border_collapse(); border_collapse.has_value())
  697. computed_values.set_border_collapse(border_collapse.value());
  698. if (auto table_layout = computed_style.table_layout(); table_layout.has_value())
  699. computed_values.set_table_layout(table_layout.value());
  700. auto aspect_ratio = computed_style.property(CSS::PropertyID::AspectRatio);
  701. if (aspect_ratio->is_value_list()) {
  702. auto& values_list = aspect_ratio->as_value_list().values();
  703. if (values_list.size() == 2
  704. && values_list[0]->is_identifier() && values_list[0]->as_identifier().id() == CSS::ValueID::Auto
  705. && values_list[1]->is_ratio()) {
  706. computed_values.set_aspect_ratio({ true, values_list[1]->as_ratio().ratio() });
  707. }
  708. } else if (aspect_ratio->is_identifier() && aspect_ratio->as_identifier().id() == CSS::ValueID::Auto) {
  709. computed_values.set_aspect_ratio({ true, {} });
  710. } else if (aspect_ratio->is_ratio()) {
  711. computed_values.set_aspect_ratio({ false, aspect_ratio->as_ratio().ratio() });
  712. }
  713. auto math_shift_value = computed_style.property(CSS::PropertyID::MathShift);
  714. if (auto math_shift = value_id_to_math_shift(math_shift_value->to_identifier()); math_shift.has_value())
  715. computed_values.set_math_shift(math_shift.value());
  716. auto math_style_value = computed_style.property(CSS::PropertyID::MathStyle);
  717. if (auto math_style = value_id_to_math_style(math_style_value->to_identifier()); math_style.has_value())
  718. computed_values.set_math_style(math_style.value());
  719. computed_values.set_math_depth(computed_style.math_depth());
  720. computed_values.set_quotes(computed_style.quotes());
  721. propagate_style_to_anonymous_wrappers();
  722. }
  723. void NodeWithStyle::propagate_style_to_anonymous_wrappers()
  724. {
  725. // Update the style of any anonymous wrappers that inherit from this node.
  726. // FIXME: This is pretty hackish. It would be nicer if they shared the inherited style
  727. // data structure somehow, so this wasn't necessary.
  728. // If this is a `display:table` box with an anonymous wrapper parent,
  729. // the parent inherits style from *this* node, not the other way around.
  730. if (display().is_table_inside() && is<TableWrapper>(parent())) {
  731. auto& table_wrapper = *static_cast<TableWrapper*>(parent());
  732. static_cast<CSS::MutableComputedValues&>(static_cast<CSS::ComputedValues&>(const_cast<CSS::ImmutableComputedValues&>(table_wrapper.computed_values()))).inherit_from(m_computed_values);
  733. transfer_table_box_computed_values_to_wrapper_computed_values(table_wrapper.m_computed_values);
  734. }
  735. // Propagate style to all anonymous children (except table wrappers!)
  736. for_each_child_of_type<NodeWithStyle>([&](NodeWithStyle& child) {
  737. if (child.is_anonymous() && !is<TableWrapper>(child)) {
  738. auto& child_computed_values = static_cast<CSS::MutableComputedValues&>(static_cast<CSS::ComputedValues&>(const_cast<CSS::ImmutableComputedValues&>(child.computed_values())));
  739. child_computed_values.inherit_from(m_computed_values);
  740. }
  741. });
  742. }
  743. bool Node::is_root_element() const
  744. {
  745. if (is_anonymous())
  746. return false;
  747. return is<HTML::HTMLHtmlElement>(*dom_node());
  748. }
  749. ByteString Node::debug_description() const
  750. {
  751. StringBuilder builder;
  752. builder.append(class_name());
  753. if (dom_node()) {
  754. builder.appendff("<{}>", dom_node()->node_name());
  755. if (dom_node()->is_element()) {
  756. auto& element = static_cast<DOM::Element const&>(*dom_node());
  757. if (element.id().has_value())
  758. builder.appendff("#{}", element.id().value());
  759. for (auto const& class_name : element.class_names())
  760. builder.appendff(".{}", class_name);
  761. }
  762. } else {
  763. builder.append("(anonymous)"sv);
  764. }
  765. return builder.to_byte_string();
  766. }
  767. CSS::Display Node::display() const
  768. {
  769. if (!has_style()) {
  770. // NOTE: No style means this is dumb text content.
  771. return CSS::Display(CSS::DisplayOutside::Inline, CSS::DisplayInside::Flow);
  772. }
  773. return computed_values().display();
  774. }
  775. bool Node::is_inline() const
  776. {
  777. return display().is_inline_outside();
  778. }
  779. bool Node::is_inline_block() const
  780. {
  781. auto display = this->display();
  782. return display.is_inline_outside() && display.is_flow_root_inside();
  783. }
  784. bool Node::is_inline_table() const
  785. {
  786. auto display = this->display();
  787. return display.is_inline_outside() && display.is_table_inside();
  788. }
  789. JS::NonnullGCPtr<NodeWithStyle> NodeWithStyle::create_anonymous_wrapper() const
  790. {
  791. auto wrapper = heap().allocate_without_realm<BlockContainer>(const_cast<DOM::Document&>(document()), nullptr, m_computed_values.clone_inherited_values());
  792. static_cast<CSS::MutableComputedValues&>(wrapper->m_computed_values).set_display(CSS::Display(CSS::DisplayOutside::Block, CSS::DisplayInside::Flow));
  793. return *wrapper;
  794. }
  795. void NodeWithStyle::reset_table_box_computed_values_used_by_wrapper_to_init_values()
  796. {
  797. VERIFY(this->display().is_table_inside());
  798. CSS::MutableComputedValues& mutable_computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
  799. mutable_computed_values.set_position(CSS::InitialValues::position());
  800. mutable_computed_values.set_float(CSS::InitialValues::float_());
  801. mutable_computed_values.set_clear(CSS::InitialValues::clear());
  802. mutable_computed_values.set_inset(CSS::InitialValues::inset());
  803. mutable_computed_values.set_margin(CSS::InitialValues::margin());
  804. }
  805. void NodeWithStyle::transfer_table_box_computed_values_to_wrapper_computed_values(CSS::ComputedValues& wrapper_computed_values)
  806. {
  807. // The computed values of properties 'position', 'float', 'margin-*', 'top', 'right', 'bottom', and 'left' on the table element are used on the table wrapper box and not the table box;
  808. // all other values of non-inheritable properties are used on the table box and not the table wrapper box.
  809. // (Where the table element's values are not used on the table and table wrapper boxes, the initial values are used instead.)
  810. auto& mutable_wrapper_computed_values = static_cast<CSS::MutableComputedValues&>(wrapper_computed_values);
  811. if (display().is_inline_outside())
  812. mutable_wrapper_computed_values.set_display(CSS::Display::from_short(CSS::Display::Short::InlineBlock));
  813. else
  814. mutable_wrapper_computed_values.set_display(CSS::Display::from_short(CSS::Display::Short::FlowRoot));
  815. mutable_wrapper_computed_values.set_position(computed_values().position());
  816. mutable_wrapper_computed_values.set_inset(computed_values().inset());
  817. mutable_wrapper_computed_values.set_float(computed_values().float_());
  818. mutable_wrapper_computed_values.set_clear(computed_values().clear());
  819. mutable_wrapper_computed_values.set_margin(computed_values().margin());
  820. reset_table_box_computed_values_used_by_wrapper_to_init_values();
  821. }
  822. void Node::set_paintable(JS::GCPtr<Painting::Paintable> paintable)
  823. {
  824. m_paintable = move(paintable);
  825. }
  826. JS::GCPtr<Painting::Paintable> Node::create_paintable() const
  827. {
  828. return nullptr;
  829. }
  830. bool Node::is_anonymous() const
  831. {
  832. return m_anonymous;
  833. }
  834. DOM::Node const* Node::dom_node() const
  835. {
  836. if (m_anonymous)
  837. return nullptr;
  838. return m_dom_node.ptr();
  839. }
  840. DOM::Node* Node::dom_node()
  841. {
  842. if (m_anonymous)
  843. return nullptr;
  844. return m_dom_node.ptr();
  845. }
  846. DOM::Element const* Node::pseudo_element_generator() const
  847. {
  848. VERIFY(m_generated_for != GeneratedFor::NotGenerated);
  849. return m_pseudo_element_generator.ptr();
  850. }
  851. DOM::Element* Node::pseudo_element_generator()
  852. {
  853. VERIFY(m_generated_for != GeneratedFor::NotGenerated);
  854. return m_pseudo_element_generator.ptr();
  855. }
  856. DOM::Document& Node::document()
  857. {
  858. return m_dom_node->document();
  859. }
  860. DOM::Document const& Node::document() const
  861. {
  862. return m_dom_node->document();
  863. }
  864. }