Node.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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::Position::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::Position::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::Position::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::Position::Absolute || position == CSS::Position::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::Position::Fixed || position == CSS::Position::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::Position::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::Position::Absolute || position == CSS::Position::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::Position::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. m_font = Platform::FontPlugin::the().default_font();
  262. }
  263. void NodeWithStyle::visit_edges(Visitor& visitor)
  264. {
  265. Base::visit_edges(visitor);
  266. for (auto& layer : m_computed_values.background_layers()) {
  267. if (layer.background_image && layer.background_image->is_image())
  268. layer.background_image->as_image().visit_edges(visitor);
  269. }
  270. if (m_list_style_image && m_list_style_image->is_image())
  271. m_list_style_image->as_image().visit_edges(visitor);
  272. }
  273. // https://www.w3.org/TR/css-values-4/#snap-a-length-as-a-border-width
  274. static CSSPixels snap_a_length_as_a_border_width(double device_pixels_per_css_pixel, CSSPixels length)
  275. {
  276. // 1. Assert: len is non-negative.
  277. VERIFY(length >= 0);
  278. // 2. If len is an integer number of device pixels, do nothing.
  279. auto device_pixels = length.to_double() * device_pixels_per_css_pixel;
  280. if (device_pixels == trunc(device_pixels))
  281. return length;
  282. // 3. If len is greater than zero, but less than 1 device pixel, round len up to 1 device pixel.
  283. if (device_pixels > 0 && device_pixels < 1)
  284. return CSSPixels::nearest_value_for(1 / device_pixels_per_css_pixel);
  285. // 4. If len is greater than 1 device pixel, round it down to the nearest integer number of device pixels.
  286. if (device_pixels > 1)
  287. return CSSPixels::nearest_value_for(floor(device_pixels) / device_pixels_per_css_pixel);
  288. return length;
  289. }
  290. void NodeWithStyle::apply_style(const CSS::StyleProperties& computed_style)
  291. {
  292. auto& computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
  293. // NOTE: color must be set first to ensure currentColor can be resolved in other properties (e.g. background-color).
  294. computed_values.set_color(computed_style.color_or_fallback(CSS::PropertyID::Color, *this, CSS::InitialValues::color()));
  295. // NOTE: We have to be careful that font-related properties get set in the right order.
  296. // m_font is used by Length::to_px() when resolving sizes against this layout node.
  297. // That's why it has to be set before everything else.
  298. m_font = computed_style.computed_font();
  299. computed_values.set_font_size(computed_style.property(CSS::PropertyID::FontSize)->as_length().length().to_px(*this));
  300. computed_values.set_font_weight(round_to<int>(computed_style.property(CSS::PropertyID::FontWeight)->as_number().number()));
  301. m_line_height = computed_style.line_height(*this);
  302. computed_values.set_vertical_align(computed_style.vertical_align());
  303. {
  304. auto attachments = computed_style.property(CSS::PropertyID::BackgroundAttachment);
  305. auto clips = computed_style.property(CSS::PropertyID::BackgroundClip);
  306. auto images = computed_style.property(CSS::PropertyID::BackgroundImage);
  307. auto origins = computed_style.property(CSS::PropertyID::BackgroundOrigin);
  308. auto x_positions = computed_style.property(CSS::PropertyID::BackgroundPositionX);
  309. auto y_positions = computed_style.property(CSS::PropertyID::BackgroundPositionY);
  310. auto repeats = computed_style.property(CSS::PropertyID::BackgroundRepeat);
  311. auto sizes = computed_style.property(CSS::PropertyID::BackgroundSize);
  312. auto count_layers = [](auto maybe_style_value) -> size_t {
  313. if (maybe_style_value->is_value_list())
  314. return maybe_style_value->as_value_list().size();
  315. else
  316. return 1;
  317. };
  318. auto value_for_layer = [](auto& style_value, size_t layer_index) -> RefPtr<CSS::StyleValue const> {
  319. if (style_value->is_value_list())
  320. return style_value->as_value_list().value_at(layer_index, true);
  321. return style_value;
  322. };
  323. size_t layer_count = 1;
  324. layer_count = max(layer_count, count_layers(attachments));
  325. layer_count = max(layer_count, count_layers(clips));
  326. layer_count = max(layer_count, count_layers(images));
  327. layer_count = max(layer_count, count_layers(origins));
  328. layer_count = max(layer_count, count_layers(x_positions));
  329. layer_count = max(layer_count, count_layers(y_positions));
  330. layer_count = max(layer_count, count_layers(repeats));
  331. layer_count = max(layer_count, count_layers(sizes));
  332. Vector<CSS::BackgroundLayerData> layers;
  333. layers.ensure_capacity(layer_count);
  334. for (size_t layer_index = 0; layer_index < layer_count; layer_index++) {
  335. CSS::BackgroundLayerData layer;
  336. if (auto image_value = value_for_layer(images, layer_index); image_value) {
  337. if (image_value->is_abstract_image()) {
  338. layer.background_image = image_value->as_abstract_image();
  339. const_cast<CSS::AbstractImageStyleValue&>(*layer.background_image).load_any_resources(document());
  340. }
  341. }
  342. if (auto attachment_value = value_for_layer(attachments, layer_index); attachment_value && attachment_value->is_identifier()) {
  343. switch (attachment_value->to_identifier()) {
  344. case CSS::ValueID::Fixed:
  345. layer.attachment = CSS::BackgroundAttachment::Fixed;
  346. break;
  347. case CSS::ValueID::Local:
  348. layer.attachment = CSS::BackgroundAttachment::Local;
  349. break;
  350. case CSS::ValueID::Scroll:
  351. layer.attachment = CSS::BackgroundAttachment::Scroll;
  352. break;
  353. default:
  354. break;
  355. }
  356. }
  357. auto as_box = [](auto value_id) {
  358. switch (value_id) {
  359. case CSS::ValueID::BorderBox:
  360. return CSS::BackgroundBox::BorderBox;
  361. case CSS::ValueID::ContentBox:
  362. return CSS::BackgroundBox::ContentBox;
  363. case CSS::ValueID::PaddingBox:
  364. return CSS::BackgroundBox::PaddingBox;
  365. default:
  366. VERIFY_NOT_REACHED();
  367. }
  368. };
  369. if (auto origin_value = value_for_layer(origins, layer_index); origin_value && origin_value->is_identifier()) {
  370. layer.origin = as_box(origin_value->to_identifier());
  371. }
  372. if (auto clip_value = value_for_layer(clips, layer_index); clip_value && clip_value->is_identifier()) {
  373. layer.clip = as_box(clip_value->to_identifier());
  374. }
  375. if (auto position_value = value_for_layer(x_positions, layer_index); position_value && position_value->is_edge()) {
  376. auto& position = position_value->as_edge();
  377. layer.position_edge_x = position.edge();
  378. layer.position_offset_x = position.offset();
  379. }
  380. if (auto position_value = value_for_layer(y_positions, layer_index); position_value && position_value->is_edge()) {
  381. auto& position = position_value->as_edge();
  382. layer.position_edge_y = position.edge();
  383. layer.position_offset_y = position.offset();
  384. };
  385. if (auto size_value = value_for_layer(sizes, layer_index); size_value) {
  386. if (size_value->is_background_size()) {
  387. auto& size = size_value->as_background_size();
  388. layer.size_type = CSS::BackgroundSize::LengthPercentage;
  389. layer.size_x = size.size_x();
  390. layer.size_y = size.size_y();
  391. } else if (size_value->is_identifier()) {
  392. switch (size_value->to_identifier()) {
  393. case CSS::ValueID::Contain:
  394. layer.size_type = CSS::BackgroundSize::Contain;
  395. break;
  396. case CSS::ValueID::Cover:
  397. layer.size_type = CSS::BackgroundSize::Cover;
  398. break;
  399. default:
  400. break;
  401. }
  402. }
  403. }
  404. if (auto repeat_value = value_for_layer(repeats, layer_index); repeat_value && repeat_value->is_background_repeat()) {
  405. layer.repeat_x = repeat_value->as_background_repeat().repeat_x();
  406. layer.repeat_y = repeat_value->as_background_repeat().repeat_y();
  407. }
  408. layers.append(move(layer));
  409. }
  410. computed_values.set_background_layers(move(layers));
  411. }
  412. computed_values.set_background_color(computed_style.color_or_fallback(CSS::PropertyID::BackgroundColor, *this, CSS::InitialValues::background_color()));
  413. if (auto box_sizing = computed_style.box_sizing(); box_sizing.has_value())
  414. computed_values.set_box_sizing(box_sizing.release_value());
  415. if (auto maybe_font_variant = computed_style.font_variant(); maybe_font_variant.has_value())
  416. computed_values.set_font_variant(maybe_font_variant.release_value());
  417. // FIXME: BorderXRadius properties are now BorderRadiusStyleValues, so make use of that.
  418. auto border_bottom_left_radius = computed_style.property(CSS::PropertyID::BorderBottomLeftRadius);
  419. if (border_bottom_left_radius->is_border_radius()) {
  420. computed_values.set_border_bottom_left_radius(
  421. CSS::BorderRadiusData {
  422. border_bottom_left_radius->as_border_radius().horizontal_radius(),
  423. border_bottom_left_radius->as_border_radius().vertical_radius() });
  424. }
  425. auto border_bottom_right_radius = computed_style.property(CSS::PropertyID::BorderBottomRightRadius);
  426. if (border_bottom_right_radius->is_border_radius()) {
  427. computed_values.set_border_bottom_right_radius(
  428. CSS::BorderRadiusData {
  429. border_bottom_right_radius->as_border_radius().horizontal_radius(),
  430. border_bottom_right_radius->as_border_radius().vertical_radius() });
  431. }
  432. auto border_top_left_radius = computed_style.property(CSS::PropertyID::BorderTopLeftRadius);
  433. if (border_top_left_radius->is_border_radius()) {
  434. computed_values.set_border_top_left_radius(
  435. CSS::BorderRadiusData {
  436. border_top_left_radius->as_border_radius().horizontal_radius(),
  437. border_top_left_radius->as_border_radius().vertical_radius() });
  438. }
  439. auto border_top_right_radius = computed_style.property(CSS::PropertyID::BorderTopRightRadius);
  440. if (border_top_right_radius->is_border_radius()) {
  441. computed_values.set_border_top_right_radius(
  442. CSS::BorderRadiusData {
  443. border_top_right_radius->as_border_radius().horizontal_radius(),
  444. border_top_right_radius->as_border_radius().vertical_radius() });
  445. }
  446. computed_values.set_display(computed_style.display());
  447. auto flex_direction = computed_style.flex_direction();
  448. if (flex_direction.has_value())
  449. computed_values.set_flex_direction(flex_direction.value());
  450. auto flex_wrap = computed_style.flex_wrap();
  451. if (flex_wrap.has_value())
  452. computed_values.set_flex_wrap(flex_wrap.value());
  453. auto flex_basis = computed_style.flex_basis();
  454. if (flex_basis.has_value())
  455. computed_values.set_flex_basis(flex_basis.value());
  456. computed_values.set_flex_grow(computed_style.flex_grow());
  457. computed_values.set_flex_shrink(computed_style.flex_shrink());
  458. computed_values.set_order(computed_style.order());
  459. computed_values.set_clip(computed_style.clip());
  460. computed_values.set_backdrop_filter(computed_style.backdrop_filter());
  461. auto justify_content = computed_style.justify_content();
  462. if (justify_content.has_value())
  463. computed_values.set_justify_content(justify_content.value());
  464. auto justify_items = computed_style.justify_items();
  465. if (justify_items.has_value())
  466. computed_values.set_justify_items(justify_items.value());
  467. auto justify_self = computed_style.justify_self();
  468. if (justify_self.has_value())
  469. computed_values.set_justify_self(justify_self.value());
  470. auto accent_color = computed_style.accent_color(*this);
  471. if (accent_color.has_value())
  472. computed_values.set_accent_color(accent_color.value());
  473. auto align_content = computed_style.align_content();
  474. if (align_content.has_value())
  475. computed_values.set_align_content(align_content.value());
  476. auto align_items = computed_style.align_items();
  477. if (align_items.has_value())
  478. computed_values.set_align_items(align_items.value());
  479. auto align_self = computed_style.align_self();
  480. if (align_self.has_value())
  481. computed_values.set_align_self(align_self.value());
  482. auto appearance = computed_style.appearance();
  483. if (appearance.has_value())
  484. computed_values.set_appearance(appearance.value());
  485. auto position = computed_style.position();
  486. if (position.has_value())
  487. computed_values.set_position(position.value());
  488. auto text_align = computed_style.text_align();
  489. if (text_align.has_value())
  490. computed_values.set_text_align(text_align.value());
  491. auto text_justify = computed_style.text_justify();
  492. if (text_align.has_value())
  493. computed_values.set_text_justify(text_justify.value());
  494. if (auto text_indent = computed_style.length_percentage(CSS::PropertyID::TextIndent); text_indent.has_value())
  495. computed_values.set_text_indent(text_indent.release_value());
  496. auto white_space = computed_style.white_space();
  497. if (white_space.has_value())
  498. computed_values.set_white_space(white_space.value());
  499. auto float_ = computed_style.float_();
  500. if (float_.has_value())
  501. computed_values.set_float(float_.value());
  502. computed_values.set_border_spacing_horizontal(computed_style.border_spacing_horizontal());
  503. computed_values.set_border_spacing_vertical(computed_style.border_spacing_vertical());
  504. auto caption_side = computed_style.caption_side();
  505. if (caption_side.has_value())
  506. computed_values.set_caption_side(caption_side.value());
  507. auto clear = computed_style.clear();
  508. if (clear.has_value())
  509. computed_values.set_clear(clear.value());
  510. auto overflow_x = computed_style.overflow_x();
  511. if (overflow_x.has_value())
  512. computed_values.set_overflow_x(overflow_x.value());
  513. auto overflow_y = computed_style.overflow_y();
  514. if (overflow_y.has_value())
  515. computed_values.set_overflow_y(overflow_y.value());
  516. auto cursor = computed_style.cursor();
  517. if (cursor.has_value())
  518. computed_values.set_cursor(cursor.value());
  519. auto image_rendering = computed_style.image_rendering();
  520. if (image_rendering.has_value())
  521. computed_values.set_image_rendering(image_rendering.value());
  522. auto pointer_events = computed_style.pointer_events();
  523. if (pointer_events.has_value())
  524. computed_values.set_pointer_events(pointer_events.value());
  525. computed_values.set_text_decoration_line(computed_style.text_decoration_line());
  526. auto text_decoration_style = computed_style.text_decoration_style();
  527. if (text_decoration_style.has_value())
  528. computed_values.set_text_decoration_style(text_decoration_style.value());
  529. auto text_transform = computed_style.text_transform();
  530. if (text_transform.has_value())
  531. computed_values.set_text_transform(text_transform.value());
  532. if (auto list_style_type = computed_style.list_style_type(); list_style_type.has_value())
  533. computed_values.set_list_style_type(list_style_type.value());
  534. auto list_style_image = computed_style.property(CSS::PropertyID::ListStyleImage);
  535. if (list_style_image->is_abstract_image()) {
  536. m_list_style_image = list_style_image->as_abstract_image();
  537. const_cast<CSS::AbstractImageStyleValue&>(*m_list_style_image).load_any_resources(document());
  538. }
  539. if (auto list_style_position = computed_style.list_style_position(); list_style_position.has_value())
  540. computed_values.set_list_style_position(list_style_position.value());
  541. // FIXME: The default text decoration color value is `currentcolor`, but since we can't resolve that easily,
  542. // we just manually grab the value from `color`. This makes it dependent on `color` being
  543. // specified first, so it's far from ideal.
  544. computed_values.set_text_decoration_color(computed_style.color_or_fallback(CSS::PropertyID::TextDecorationColor, *this, computed_values.color()));
  545. if (auto maybe_text_decoration_thickness = computed_style.length_percentage(CSS::PropertyID::TextDecorationThickness); maybe_text_decoration_thickness.has_value())
  546. computed_values.set_text_decoration_thickness(maybe_text_decoration_thickness.release_value());
  547. computed_values.set_text_shadow(computed_style.text_shadow(*this));
  548. computed_values.set_z_index(computed_style.z_index());
  549. computed_values.set_opacity(computed_style.opacity());
  550. if (auto maybe_visibility = computed_style.visibility(); maybe_visibility.has_value())
  551. computed_values.set_visibility(maybe_visibility.release_value());
  552. computed_values.set_width(computed_style.size_value(CSS::PropertyID::Width));
  553. computed_values.set_min_width(computed_style.size_value(CSS::PropertyID::MinWidth));
  554. computed_values.set_max_width(computed_style.size_value(CSS::PropertyID::MaxWidth));
  555. computed_values.set_height(computed_style.size_value(CSS::PropertyID::Height));
  556. computed_values.set_min_height(computed_style.size_value(CSS::PropertyID::MinHeight));
  557. computed_values.set_max_height(computed_style.size_value(CSS::PropertyID::MaxHeight));
  558. computed_values.set_inset(computed_style.length_box(CSS::PropertyID::Left, CSS::PropertyID::Top, CSS::PropertyID::Right, CSS::PropertyID::Bottom, CSS::Length::make_auto()));
  559. 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)));
  560. 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)));
  561. computed_values.set_box_shadow(computed_style.box_shadow(*this));
  562. computed_values.set_transformations(computed_style.transformations());
  563. computed_values.set_transform_origin(computed_style.transform_origin());
  564. auto transition_delay_property = computed_style.property(CSS::PropertyID::TransitionDelay);
  565. if (transition_delay_property->is_time()) {
  566. auto& transition_delay = transition_delay_property->as_time();
  567. computed_values.set_transition_delay(transition_delay.time());
  568. } else if (transition_delay_property->is_calculated()) {
  569. auto& transition_delay = transition_delay_property->as_calculated();
  570. computed_values.set_transition_delay(transition_delay.resolve_time().value());
  571. }
  572. auto do_border_style = [&](CSS::BorderData& border, CSS::PropertyID width_property, CSS::PropertyID color_property, CSS::PropertyID style_property) {
  573. // FIXME: The default border color value is `currentcolor`, but since we can't resolve that easily,
  574. // we just manually grab the value from `color`. This makes it dependent on `color` being
  575. // specified first, so it's far from ideal.
  576. border.color = computed_style.color_or_fallback(color_property, *this, computed_values.color());
  577. border.line_style = computed_style.line_style(style_property).value_or(CSS::LineStyle::None);
  578. // https://w3c.github.io/csswg-drafts/css-backgrounds/#border-style
  579. // none
  580. // 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.
  581. // hidden
  582. // Same as none, but has different behavior in the border conflict resolution rules for border-collapsed tables [CSS2].
  583. if (border.line_style == CSS::LineStyle::None || border.line_style == CSS::LineStyle::Hidden) {
  584. border.width = 0;
  585. } else {
  586. auto resolve_border_width = [&]() -> CSSPixels {
  587. auto value = computed_style.property(width_property);
  588. if (value->is_calculated())
  589. return value->as_calculated().resolve_length(*this)->to_px(*this);
  590. if (value->is_length())
  591. return value->as_length().length().to_px(*this);
  592. if (value->is_identifier()) {
  593. // https://www.w3.org/TR/css-backgrounds-3/#valdef-line-width-thin
  594. switch (value->to_identifier()) {
  595. case CSS::ValueID::Thin:
  596. return 1;
  597. case CSS::ValueID::Medium:
  598. return 3;
  599. case CSS::ValueID::Thick:
  600. return 5;
  601. default:
  602. VERIFY_NOT_REACHED();
  603. }
  604. }
  605. VERIFY_NOT_REACHED();
  606. };
  607. border.width = snap_a_length_as_a_border_width(document().page()->client().device_pixels_per_css_pixel(), resolve_border_width());
  608. }
  609. };
  610. do_border_style(computed_values.border_left(), CSS::PropertyID::BorderLeftWidth, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftStyle);
  611. do_border_style(computed_values.border_top(), CSS::PropertyID::BorderTopWidth, CSS::PropertyID::BorderTopColor, CSS::PropertyID::BorderTopStyle);
  612. do_border_style(computed_values.border_right(), CSS::PropertyID::BorderRightWidth, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightStyle);
  613. do_border_style(computed_values.border_bottom(), CSS::PropertyID::BorderBottomWidth, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomStyle);
  614. if (auto outline_color = computed_style.property(CSS::PropertyID::OutlineColor); outline_color->has_color())
  615. computed_values.set_outline_color(outline_color->to_color(*this));
  616. if (auto outline_offset = computed_style.property(CSS::PropertyID::OutlineOffset); outline_offset->is_length())
  617. computed_values.set_outline_offset(outline_offset->as_length().length());
  618. if (auto outline_style = computed_style.outline_style(); outline_style.has_value())
  619. computed_values.set_outline_style(outline_style.value());
  620. if (auto outline_width = computed_style.property(CSS::PropertyID::OutlineWidth); outline_width->is_length())
  621. computed_values.set_outline_width(outline_width->as_length().length());
  622. // FIXME: Stop generating the content twice. (First time is in TreeBuilder.)
  623. computed_values.set_content(computed_style.content(initial_quote_nesting_level()).content_data);
  624. computed_values.set_grid_auto_columns(computed_style.grid_auto_columns());
  625. computed_values.set_grid_auto_rows(computed_style.grid_auto_rows());
  626. computed_values.set_grid_template_columns(computed_style.grid_template_columns());
  627. computed_values.set_grid_template_rows(computed_style.grid_template_rows());
  628. computed_values.set_grid_column_end(computed_style.grid_column_end());
  629. computed_values.set_grid_column_start(computed_style.grid_column_start());
  630. computed_values.set_grid_row_end(computed_style.grid_row_end());
  631. computed_values.set_grid_row_start(computed_style.grid_row_start());
  632. computed_values.set_grid_template_areas(computed_style.grid_template_areas());
  633. computed_values.set_grid_auto_flow(computed_style.grid_auto_flow());
  634. auto fill = computed_style.property(CSS::PropertyID::Fill);
  635. if (fill->has_color())
  636. computed_values.set_fill(fill->to_color(*this));
  637. else if (fill->is_url())
  638. computed_values.set_fill(fill->as_url().url());
  639. auto stroke = computed_style.property(CSS::PropertyID::Stroke);
  640. if (stroke->has_color())
  641. computed_values.set_stroke(stroke->to_color(*this));
  642. else if (stroke->is_url())
  643. computed_values.set_stroke(stroke->as_url().url());
  644. if (auto stop_color = computed_style.property(CSS::PropertyID::StopColor); stop_color->has_color())
  645. computed_values.set_stop_color(stop_color->to_color(*this));
  646. auto stroke_width = computed_style.property(CSS::PropertyID::StrokeWidth);
  647. // FIXME: Converting to pixels isn't really correct - values should be in "user units"
  648. // https://svgwg.org/svg2-draft/coords.html#TermUserUnits
  649. if (stroke_width->is_number())
  650. computed_values.set_stroke_width(CSS::Length::make_px(CSSPixels::nearest_value_for(stroke_width->as_number().number())));
  651. else if (stroke_width->is_length())
  652. computed_values.set_stroke_width(stroke_width->as_length().length());
  653. else if (stroke_width->is_percentage())
  654. computed_values.set_stroke_width(CSS::LengthPercentage { stroke_width->as_percentage().percentage() });
  655. if (auto mask = computed_style.property(CSS::PropertyID::Mask); mask->is_url())
  656. computed_values.set_mask(mask->as_url().url());
  657. if (auto fill_rule = computed_style.fill_rule(); fill_rule.has_value())
  658. computed_values.set_fill_rule(*fill_rule);
  659. computed_values.set_fill_opacity(computed_style.fill_opacity());
  660. computed_values.set_stroke_opacity(computed_style.stroke_opacity());
  661. computed_values.set_stop_opacity(computed_style.stop_opacity());
  662. if (auto text_anchor = computed_style.text_anchor(); text_anchor.has_value())
  663. computed_values.set_text_anchor(*text_anchor);
  664. if (auto column_count = computed_style.property(CSS::PropertyID::ColumnCount); column_count->is_integer())
  665. computed_values.set_column_count(CSS::ColumnCount::make_integer(column_count->as_integer().integer()));
  666. computed_values.set_column_gap(computed_style.size_value(CSS::PropertyID::ColumnGap));
  667. computed_values.set_row_gap(computed_style.size_value(CSS::PropertyID::RowGap));
  668. if (auto border_collapse = computed_style.border_collapse(); border_collapse.has_value())
  669. computed_values.set_border_collapse(border_collapse.value());
  670. if (auto table_layout = computed_style.table_layout(); table_layout.has_value())
  671. computed_values.set_table_layout(table_layout.value());
  672. auto aspect_ratio = computed_style.property(CSS::PropertyID::AspectRatio);
  673. if (aspect_ratio->is_value_list()) {
  674. auto& values_list = aspect_ratio->as_value_list().values();
  675. if (values_list.size() == 2
  676. && values_list[0]->is_identifier() && values_list[0]->as_identifier().id() == CSS::ValueID::Auto
  677. && values_list[1]->is_ratio()) {
  678. computed_values.set_aspect_ratio({ true, values_list[1]->as_ratio().ratio() });
  679. }
  680. } else if (aspect_ratio->is_identifier() && aspect_ratio->as_identifier().id() == CSS::ValueID::Auto) {
  681. computed_values.set_aspect_ratio({ true, {} });
  682. } else if (aspect_ratio->is_ratio()) {
  683. computed_values.set_aspect_ratio({ false, aspect_ratio->as_ratio().ratio() });
  684. }
  685. if (display().is_table_inside() && is<TableWrapper>(parent())) {
  686. auto& wrapper_computed_values = static_cast<TableWrapper*>(parent())->m_computed_values;
  687. transfer_table_box_computed_values_to_wrapper_computed_values(wrapper_computed_values);
  688. }
  689. auto math_shift_value = computed_style.property(CSS::PropertyID::MathShift);
  690. if (auto math_shift = value_id_to_math_shift(math_shift_value->to_identifier()); math_shift.has_value())
  691. computed_values.set_math_shift(math_shift.value());
  692. auto math_style_value = computed_style.property(CSS::PropertyID::MathStyle);
  693. if (auto math_style = value_id_to_math_style(math_style_value->to_identifier()); math_style.has_value())
  694. computed_values.set_math_style(math_style.value());
  695. computed_values.set_math_depth(computed_style.math_depth());
  696. computed_values.set_quotes(computed_style.quotes());
  697. // Update any anonymous children that inherit from this node.
  698. // FIXME: This is pretty hackish. It would be nicer if they shared the inherited style
  699. // data structure somehow, so this wasn't necessary.
  700. for_each_child([&](auto& child) {
  701. if (child.is_anonymous()) {
  702. auto& child_computed_values = static_cast<CSS::MutableComputedValues&>(static_cast<CSS::ComputedValues&>(const_cast<CSS::ImmutableComputedValues&>(child.computed_values())));
  703. child_computed_values.inherit_from(computed_values);
  704. }
  705. });
  706. }
  707. bool Node::is_root_element() const
  708. {
  709. if (is_anonymous())
  710. return false;
  711. return is<HTML::HTMLHtmlElement>(*dom_node());
  712. }
  713. DeprecatedString Node::debug_description() const
  714. {
  715. StringBuilder builder;
  716. builder.append(class_name());
  717. if (dom_node()) {
  718. builder.appendff("<{}>", dom_node()->node_name());
  719. if (dom_node()->is_element()) {
  720. auto& element = static_cast<DOM::Element const&>(*dom_node());
  721. if (auto id = element.get_attribute(HTML::AttributeNames::id); id.has_value())
  722. builder.appendff("#{}", id.value());
  723. for (auto const& class_name : element.class_names())
  724. builder.appendff(".{}", class_name);
  725. }
  726. } else {
  727. builder.append("(anonymous)"sv);
  728. }
  729. return builder.to_deprecated_string();
  730. }
  731. CSS::Display Node::display() const
  732. {
  733. if (!has_style()) {
  734. // NOTE: No style means this is dumb text content.
  735. return CSS::Display(CSS::DisplayOutside::Inline, CSS::DisplayInside::Flow);
  736. }
  737. return computed_values().display();
  738. }
  739. bool Node::is_inline() const
  740. {
  741. return display().is_inline_outside();
  742. }
  743. bool Node::is_inline_block() const
  744. {
  745. auto display = this->display();
  746. return display.is_inline_outside() && display.is_flow_root_inside();
  747. }
  748. bool Node::is_inline_table() const
  749. {
  750. auto display = this->display();
  751. return display.is_inline_outside() && display.is_table_inside();
  752. }
  753. JS::NonnullGCPtr<NodeWithStyle> NodeWithStyle::create_anonymous_wrapper() const
  754. {
  755. auto wrapper = heap().allocate_without_realm<BlockContainer>(const_cast<DOM::Document&>(document()), nullptr, m_computed_values.clone_inherited_values());
  756. static_cast<CSS::MutableComputedValues&>(wrapper->m_computed_values).set_display(CSS::Display(CSS::DisplayOutside::Block, CSS::DisplayInside::Flow));
  757. wrapper->m_font = m_font;
  758. wrapper->m_line_height = m_line_height;
  759. return *wrapper;
  760. }
  761. void NodeWithStyle::reset_table_box_computed_values_used_by_wrapper_to_init_values()
  762. {
  763. VERIFY(this->display().is_table_inside());
  764. CSS::MutableComputedValues& mutable_computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
  765. mutable_computed_values.set_position(CSS::InitialValues::position());
  766. mutable_computed_values.set_float(CSS::InitialValues::float_());
  767. mutable_computed_values.set_clear(CSS::InitialValues::clear());
  768. mutable_computed_values.set_inset(CSS::InitialValues::inset());
  769. mutable_computed_values.set_margin(CSS::InitialValues::margin());
  770. }
  771. void NodeWithStyle::transfer_table_box_computed_values_to_wrapper_computed_values(CSS::ComputedValues& wrapper_computed_values)
  772. {
  773. // 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;
  774. // all other values of non-inheritable properties are used on the table box and not the table wrapper box.
  775. // (Where the table element's values are not used on the table and table wrapper boxes, the initial values are used instead.)
  776. auto& mutable_wrapper_computed_values = static_cast<CSS::MutableComputedValues&>(wrapper_computed_values);
  777. if (display().is_inline_outside())
  778. mutable_wrapper_computed_values.set_display(CSS::Display::from_short(CSS::Display::Short::InlineBlock));
  779. else
  780. mutable_wrapper_computed_values.set_display(CSS::Display::from_short(CSS::Display::Short::FlowRoot));
  781. mutable_wrapper_computed_values.set_position(computed_values().position());
  782. mutable_wrapper_computed_values.set_inset(computed_values().inset());
  783. mutable_wrapper_computed_values.set_float(computed_values().float_());
  784. mutable_wrapper_computed_values.set_clear(computed_values().clear());
  785. mutable_wrapper_computed_values.set_margin(computed_values().margin());
  786. reset_table_box_computed_values_used_by_wrapper_to_init_values();
  787. }
  788. void Node::set_paintable(JS::GCPtr<Painting::Paintable> paintable)
  789. {
  790. m_paintable = move(paintable);
  791. }
  792. JS::GCPtr<Painting::Paintable> Node::create_paintable() const
  793. {
  794. return nullptr;
  795. }
  796. bool Node::is_anonymous() const
  797. {
  798. return m_anonymous;
  799. }
  800. DOM::Node const* Node::dom_node() const
  801. {
  802. if (m_anonymous)
  803. return nullptr;
  804. return m_dom_node.ptr();
  805. }
  806. DOM::Node* Node::dom_node()
  807. {
  808. if (m_anonymous)
  809. return nullptr;
  810. return m_dom_node.ptr();
  811. }
  812. DOM::Element const* Node::pseudo_element_generator() const
  813. {
  814. VERIFY(m_generated_for != GeneratedFor::NotGenerated);
  815. return m_pseudo_element_generator.ptr();
  816. }
  817. DOM::Element* Node::pseudo_element_generator()
  818. {
  819. VERIFY(m_generated_for != GeneratedFor::NotGenerated);
  820. return m_pseudo_element_generator.ptr();
  821. }
  822. DOM::Document& Node::document()
  823. {
  824. return m_dom_node->document();
  825. }
  826. DOM::Document const& Node::document() const
  827. {
  828. return m_dom_node->document();
  829. }
  830. }