Node.cpp 51 KB

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