Node.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Demangle.h>
  7. #include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
  8. #include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
  9. #include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
  10. #include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
  11. #include <LibWeb/CSS/StyleValues/EdgeStyleValue.h>
  12. #include <LibWeb/CSS/StyleValues/NumericStyleValue.h>
  13. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  14. #include <LibWeb/CSS/StyleValues/StyleValueList.h>
  15. #include <LibWeb/CSS/StyleValues/URLStyleValue.h>
  16. #include <LibWeb/DOM/Document.h>
  17. #include <LibWeb/Dump.h>
  18. #include <LibWeb/HTML/BrowsingContext.h>
  19. #include <LibWeb/HTML/HTMLHtmlElement.h>
  20. #include <LibWeb/Layout/BlockContainer.h>
  21. #include <LibWeb/Layout/FormattingContext.h>
  22. #include <LibWeb/Layout/Node.h>
  23. #include <LibWeb/Layout/TableBox.h>
  24. #include <LibWeb/Layout/TextNode.h>
  25. #include <LibWeb/Layout/Viewport.h>
  26. #include <LibWeb/Platform/FontPlugin.h>
  27. namespace Web::Layout {
  28. Node::Node(DOM::Document& document, DOM::Node* node)
  29. : m_dom_node(node ? *node : document)
  30. , m_browsing_context(*document.browsing_context())
  31. , m_anonymous(node == nullptr)
  32. {
  33. if (node)
  34. node->set_layout_node({}, *this);
  35. }
  36. Node::~Node() = default;
  37. void Node::visit_edges(Cell::Visitor& visitor)
  38. {
  39. Base::visit_edges(visitor);
  40. visitor.visit(m_dom_node);
  41. visitor.visit(m_paintable);
  42. visitor.visit(m_browsing_context);
  43. TreeNode::visit_edges(visitor);
  44. }
  45. // https://www.w3.org/TR/css-display-3/#out-of-flow
  46. bool Node::is_out_of_flow(FormattingContext const& formatting_context) const
  47. {
  48. // A layout node is out of flow if either:
  49. // 1. It is floated (which requires that floating is not inhibited).
  50. if (!formatting_context.inhibits_floating() && computed_values().float_() != CSS::Float::None)
  51. return true;
  52. // 2. It is "absolutely positioned".
  53. if (is_absolutely_positioned())
  54. return true;
  55. return false;
  56. }
  57. bool Node::can_contain_boxes_with_position_absolute() const
  58. {
  59. if (computed_values().position() != CSS::Position::Static)
  60. return true;
  61. if (is<Viewport>(*this))
  62. return true;
  63. // https://w3c.github.io/csswg-drafts/css-transforms-1/#propdef-transform
  64. // Any computed value other than none for the transform affects containing block and stacking context
  65. if (!computed_values().transformations().is_empty())
  66. return true;
  67. return false;
  68. }
  69. static Box const* nearest_ancestor_capable_of_forming_a_containing_block(Node const& node)
  70. {
  71. for (auto const* ancestor = node.parent(); ancestor; ancestor = ancestor->parent()) {
  72. if (ancestor->is_block_container()
  73. || ancestor->display().is_flex_inside()
  74. || ancestor->display().is_grid_inside()) {
  75. return verify_cast<Box>(ancestor);
  76. }
  77. }
  78. return nullptr;
  79. }
  80. Box const* Node::containing_block() const
  81. {
  82. if (is<TextNode>(*this))
  83. return nearest_ancestor_capable_of_forming_a_containing_block(*this);
  84. auto position = computed_values().position();
  85. // https://drafts.csswg.org/css-position-3/#absolute-cb
  86. if (position == CSS::Position::Absolute) {
  87. auto const* ancestor = parent();
  88. while (ancestor && !ancestor->can_contain_boxes_with_position_absolute())
  89. ancestor = ancestor->parent();
  90. while (ancestor && ancestor->is_anonymous())
  91. ancestor = nearest_ancestor_capable_of_forming_a_containing_block(*ancestor);
  92. return static_cast<Box const*>(ancestor);
  93. }
  94. if (position == CSS::Position::Fixed)
  95. return &root();
  96. return nearest_ancestor_capable_of_forming_a_containing_block(*this);
  97. }
  98. Box const* Node::non_anyonymous_containing_block() const
  99. {
  100. auto nearest_ancestor_box = containing_block();
  101. VERIFY(nearest_ancestor_box);
  102. while (nearest_ancestor_box->is_anonymous()) {
  103. nearest_ancestor_box = nearest_ancestor_box->containing_block();
  104. VERIFY(nearest_ancestor_box);
  105. }
  106. return nearest_ancestor_box;
  107. }
  108. // https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context
  109. bool Node::establishes_stacking_context() const
  110. {
  111. // NOTE: While MDN is not authoritative, there isn't a single convenient location
  112. // in the CSS specifications where the rules for stacking contexts is described.
  113. // That's why the "spec link" here points to MDN.
  114. if (!has_style())
  115. return false;
  116. // We make a stacking context for the viewport. Painting and hit testing starts from here.
  117. if (is_viewport())
  118. return true;
  119. // Root element of the document (<html>).
  120. if (is_root_element())
  121. return true;
  122. auto position = computed_values().position();
  123. // Element with a position value absolute or relative and z-index value other than auto.
  124. if (position == CSS::Position::Absolute || position == CSS::Position::Relative) {
  125. if (computed_values().z_index().has_value()) {
  126. return true;
  127. }
  128. }
  129. // Element with a position value fixed or sticky.
  130. if (position == CSS::Position::Fixed || position == CSS::Position::Sticky)
  131. return true;
  132. if (!computed_values().transformations().is_empty())
  133. return true;
  134. // Element that is a child of a flex container, with z-index value other than auto.
  135. if (parent() && parent()->display().is_flex_inside() && computed_values().z_index().has_value())
  136. return true;
  137. // Element that is a child of a grid container, with z-index value other than auto.
  138. if (parent() && parent()->display().is_grid_inside() && computed_values().z_index().has_value())
  139. return true;
  140. // https://drafts.fxtf.org/filter-effects-2/#backdrop-filter-operation
  141. // 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,
  142. // unless the element it applies to is a document root element in the current browsing context.
  143. // Spec Note: This rule works in the same way as for the filter property.
  144. if (!computed_values().backdrop_filter().is_none())
  145. return true;
  146. return computed_values().opacity() < 1.0f;
  147. }
  148. HTML::BrowsingContext const& Node::browsing_context() const
  149. {
  150. return *m_browsing_context;
  151. }
  152. HTML::BrowsingContext& Node::browsing_context()
  153. {
  154. return *m_browsing_context;
  155. }
  156. Viewport const& Node::root() const
  157. {
  158. VERIFY(document().layout_node());
  159. return *document().layout_node();
  160. }
  161. Viewport& Node::root()
  162. {
  163. VERIFY(document().layout_node());
  164. return *document().layout_node();
  165. }
  166. void Node::set_needs_display()
  167. {
  168. auto* containing_block = this->containing_block();
  169. if (!containing_block)
  170. return;
  171. if (!containing_block->paintable_box())
  172. return;
  173. if (!is<Painting::PaintableWithLines>(*containing_block->paintable_box()))
  174. return;
  175. static_cast<Painting::PaintableWithLines const&>(*containing_block->paintable_box()).for_each_fragment([&](auto& fragment) {
  176. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  177. browsing_context().set_needs_display(fragment.absolute_rect());
  178. }
  179. return IterationDecision::Continue;
  180. });
  181. }
  182. CSSPixelPoint Node::box_type_agnostic_position() const
  183. {
  184. if (is<Box>(*this))
  185. return verify_cast<Box>(*this).paintable_box()->absolute_position();
  186. VERIFY(is_inline());
  187. CSSPixelPoint position;
  188. if (auto* block = containing_block()) {
  189. if (is<Painting::PaintableWithLines>(*block)) {
  190. static_cast<Painting::PaintableWithLines const&>(*block->paintable_box()).for_each_fragment([&](auto& fragment) {
  191. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  192. position = fragment.absolute_rect().location();
  193. return IterationDecision::Break;
  194. }
  195. return IterationDecision::Continue;
  196. });
  197. }
  198. }
  199. return position;
  200. }
  201. bool Node::is_floating() const
  202. {
  203. if (!has_style())
  204. return false;
  205. // flex-items don't float.
  206. if (is_flex_item())
  207. return false;
  208. return computed_values().float_() != CSS::Float::None;
  209. }
  210. bool Node::is_positioned() const
  211. {
  212. return has_style() && computed_values().position() != CSS::Position::Static;
  213. }
  214. bool Node::is_absolutely_positioned() const
  215. {
  216. if (!has_style())
  217. return false;
  218. auto position = computed_values().position();
  219. return position == CSS::Position::Absolute || position == CSS::Position::Fixed;
  220. }
  221. bool Node::is_fixed_position() const
  222. {
  223. if (!has_style())
  224. return false;
  225. auto position = computed_values().position();
  226. return position == CSS::Position::Fixed;
  227. }
  228. NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> computed_style)
  229. : Node(document, node)
  230. {
  231. m_has_style = true;
  232. apply_style(*computed_style);
  233. }
  234. NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  235. : Node(document, node)
  236. , m_computed_values(move(computed_values))
  237. {
  238. m_has_style = true;
  239. m_font = Platform::FontPlugin::the().default_font();
  240. }
  241. void NodeWithStyle::apply_style(const CSS::StyleProperties& computed_style)
  242. {
  243. auto& computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
  244. // NOTE: color must be set first to ensure currentColor can be resolved in other properties (e.g. background-color).
  245. computed_values.set_color(computed_style.color_or_fallback(CSS::PropertyID::Color, *this, CSS::InitialValues::color()));
  246. // NOTE: We have to be careful that font-related properties get set in the right order.
  247. // m_font is used by Length::to_px() when resolving sizes against this layout node.
  248. // That's why it has to be set before everything else.
  249. m_font = computed_style.computed_font();
  250. computed_values.set_font_size(computed_style.property(CSS::PropertyID::FontSize)->to_length().to_px(*this).value());
  251. computed_values.set_font_weight(computed_style.property(CSS::PropertyID::FontWeight)->as_numeric().integer());
  252. m_line_height = computed_style.line_height(*this);
  253. computed_values.set_vertical_align(computed_style.vertical_align());
  254. {
  255. auto attachments = computed_style.property(CSS::PropertyID::BackgroundAttachment);
  256. auto clips = computed_style.property(CSS::PropertyID::BackgroundClip);
  257. auto images = computed_style.property(CSS::PropertyID::BackgroundImage);
  258. auto origins = computed_style.property(CSS::PropertyID::BackgroundOrigin);
  259. auto x_positions = computed_style.property(CSS::PropertyID::BackgroundPositionX);
  260. auto y_positions = computed_style.property(CSS::PropertyID::BackgroundPositionY);
  261. auto repeats = computed_style.property(CSS::PropertyID::BackgroundRepeat);
  262. auto sizes = computed_style.property(CSS::PropertyID::BackgroundSize);
  263. auto count_layers = [](auto maybe_style_value) -> size_t {
  264. if (maybe_style_value->is_value_list())
  265. return maybe_style_value->as_value_list().size();
  266. else
  267. return 1;
  268. };
  269. auto value_for_layer = [](auto& style_value, size_t layer_index) -> RefPtr<CSS::StyleValue const> {
  270. if (style_value->is_value_list())
  271. return style_value->as_value_list().value_at(layer_index, true);
  272. return style_value;
  273. };
  274. size_t layer_count = 1;
  275. layer_count = max(layer_count, count_layers(attachments));
  276. layer_count = max(layer_count, count_layers(clips));
  277. layer_count = max(layer_count, count_layers(images));
  278. layer_count = max(layer_count, count_layers(origins));
  279. layer_count = max(layer_count, count_layers(x_positions));
  280. layer_count = max(layer_count, count_layers(y_positions));
  281. layer_count = max(layer_count, count_layers(repeats));
  282. layer_count = max(layer_count, count_layers(sizes));
  283. Vector<CSS::BackgroundLayerData> layers;
  284. layers.ensure_capacity(layer_count);
  285. for (size_t layer_index = 0; layer_index < layer_count; layer_index++) {
  286. CSS::BackgroundLayerData layer;
  287. if (auto image_value = value_for_layer(images, layer_index); image_value) {
  288. if (image_value->is_abstract_image()) {
  289. layer.background_image = image_value->as_abstract_image();
  290. const_cast<CSS::AbstractImageStyleValue&>(*layer.background_image).load_any_resources(document());
  291. }
  292. }
  293. if (auto attachment_value = value_for_layer(attachments, layer_index); attachment_value && attachment_value->is_identifier()) {
  294. switch (attachment_value->to_identifier()) {
  295. case CSS::ValueID::Fixed:
  296. layer.attachment = CSS::BackgroundAttachment::Fixed;
  297. break;
  298. case CSS::ValueID::Local:
  299. layer.attachment = CSS::BackgroundAttachment::Local;
  300. break;
  301. case CSS::ValueID::Scroll:
  302. layer.attachment = CSS::BackgroundAttachment::Scroll;
  303. break;
  304. default:
  305. break;
  306. }
  307. }
  308. auto as_box = [](auto value_id) {
  309. switch (value_id) {
  310. case CSS::ValueID::BorderBox:
  311. return CSS::BackgroundBox::BorderBox;
  312. case CSS::ValueID::ContentBox:
  313. return CSS::BackgroundBox::ContentBox;
  314. case CSS::ValueID::PaddingBox:
  315. return CSS::BackgroundBox::PaddingBox;
  316. default:
  317. VERIFY_NOT_REACHED();
  318. }
  319. };
  320. if (auto origin_value = value_for_layer(origins, layer_index); origin_value && origin_value->is_identifier()) {
  321. layer.origin = as_box(origin_value->to_identifier());
  322. }
  323. if (auto clip_value = value_for_layer(clips, layer_index); clip_value && clip_value->is_identifier()) {
  324. layer.clip = as_box(clip_value->to_identifier());
  325. }
  326. if (auto position_value = value_for_layer(x_positions, layer_index); position_value && position_value->is_edge()) {
  327. auto& position = position_value->as_edge();
  328. layer.position_edge_x = position.edge();
  329. layer.position_offset_x = position.offset();
  330. }
  331. if (auto position_value = value_for_layer(y_positions, layer_index); position_value && position_value->is_edge()) {
  332. auto& position = position_value->as_edge();
  333. layer.position_edge_y = position.edge();
  334. layer.position_offset_y = position.offset();
  335. };
  336. if (auto size_value = value_for_layer(sizes, layer_index); size_value) {
  337. if (size_value->is_background_size()) {
  338. auto& size = size_value->as_background_size();
  339. layer.size_type = CSS::BackgroundSize::LengthPercentage;
  340. layer.size_x = size.size_x();
  341. layer.size_y = size.size_y();
  342. } else if (size_value->is_identifier()) {
  343. switch (size_value->to_identifier()) {
  344. case CSS::ValueID::Contain:
  345. layer.size_type = CSS::BackgroundSize::Contain;
  346. break;
  347. case CSS::ValueID::Cover:
  348. layer.size_type = CSS::BackgroundSize::Cover;
  349. break;
  350. default:
  351. break;
  352. }
  353. }
  354. }
  355. if (auto repeat_value = value_for_layer(repeats, layer_index); repeat_value && repeat_value->is_background_repeat()) {
  356. layer.repeat_x = repeat_value->as_background_repeat().repeat_x();
  357. layer.repeat_y = repeat_value->as_background_repeat().repeat_y();
  358. }
  359. layers.append(move(layer));
  360. }
  361. computed_values.set_background_layers(move(layers));
  362. }
  363. computed_values.set_background_color(computed_style.color_or_fallback(CSS::PropertyID::BackgroundColor, *this, CSS::InitialValues::background_color()));
  364. if (auto box_sizing = computed_style.box_sizing(); box_sizing.has_value())
  365. computed_values.set_box_sizing(box_sizing.release_value());
  366. if (auto maybe_font_variant = computed_style.font_variant(); maybe_font_variant.has_value())
  367. computed_values.set_font_variant(maybe_font_variant.release_value());
  368. // FIXME: BorderXRadius properties are now BorderRadiusStyleValues, so make use of that.
  369. auto border_bottom_left_radius = computed_style.property(CSS::PropertyID::BorderBottomLeftRadius);
  370. if (border_bottom_left_radius->is_border_radius()) {
  371. computed_values.set_border_bottom_left_radius(
  372. CSS::BorderRadiusData {
  373. border_bottom_left_radius->as_border_radius().horizontal_radius(),
  374. border_bottom_left_radius->as_border_radius().vertical_radius() });
  375. }
  376. auto border_bottom_right_radius = computed_style.property(CSS::PropertyID::BorderBottomRightRadius);
  377. if (border_bottom_right_radius->is_border_radius()) {
  378. computed_values.set_border_bottom_right_radius(
  379. CSS::BorderRadiusData {
  380. border_bottom_right_radius->as_border_radius().horizontal_radius(),
  381. border_bottom_right_radius->as_border_radius().vertical_radius() });
  382. }
  383. auto border_top_left_radius = computed_style.property(CSS::PropertyID::BorderTopLeftRadius);
  384. if (border_top_left_radius->is_border_radius()) {
  385. computed_values.set_border_top_left_radius(
  386. CSS::BorderRadiusData {
  387. border_top_left_radius->as_border_radius().horizontal_radius(),
  388. border_top_left_radius->as_border_radius().vertical_radius() });
  389. }
  390. auto border_top_right_radius = computed_style.property(CSS::PropertyID::BorderTopRightRadius);
  391. if (border_top_right_radius->is_border_radius()) {
  392. computed_values.set_border_top_right_radius(
  393. CSS::BorderRadiusData {
  394. border_top_right_radius->as_border_radius().horizontal_radius(),
  395. border_top_right_radius->as_border_radius().vertical_radius() });
  396. }
  397. computed_values.set_display(computed_style.display());
  398. auto flex_direction = computed_style.flex_direction();
  399. if (flex_direction.has_value())
  400. computed_values.set_flex_direction(flex_direction.value());
  401. auto flex_wrap = computed_style.flex_wrap();
  402. if (flex_wrap.has_value())
  403. computed_values.set_flex_wrap(flex_wrap.value());
  404. auto flex_basis = computed_style.flex_basis();
  405. if (flex_basis.has_value())
  406. computed_values.set_flex_basis(flex_basis.value());
  407. computed_values.set_flex_grow(computed_style.flex_grow());
  408. computed_values.set_flex_shrink(computed_style.flex_shrink());
  409. computed_values.set_order(computed_style.order());
  410. computed_values.set_clip(computed_style.clip());
  411. computed_values.set_backdrop_filter(computed_style.backdrop_filter());
  412. auto justify_content = computed_style.justify_content();
  413. if (justify_content.has_value())
  414. computed_values.set_justify_content(justify_content.value());
  415. auto accent_color = computed_style.accent_color(*this);
  416. if (accent_color.has_value())
  417. computed_values.set_accent_color(accent_color.value());
  418. auto align_content = computed_style.align_content();
  419. if (align_content.has_value())
  420. computed_values.set_align_content(align_content.value());
  421. auto align_items = computed_style.align_items();
  422. if (align_items.has_value())
  423. computed_values.set_align_items(align_items.value());
  424. auto align_self = computed_style.align_self();
  425. if (align_self.has_value())
  426. computed_values.set_align_self(align_self.value());
  427. auto appearance = computed_style.appearance();
  428. if (appearance.has_value())
  429. computed_values.set_appearance(appearance.value());
  430. auto position = computed_style.position();
  431. if (position.has_value())
  432. computed_values.set_position(position.value());
  433. auto text_align = computed_style.text_align();
  434. if (text_align.has_value())
  435. computed_values.set_text_align(text_align.value());
  436. auto text_justify = computed_style.text_justify();
  437. if (text_align.has_value())
  438. computed_values.set_text_justify(text_justify.value());
  439. if (auto text_indent = computed_style.length_percentage(CSS::PropertyID::TextIndent); text_indent.has_value())
  440. computed_values.set_text_indent(text_indent.release_value());
  441. auto white_space = computed_style.white_space();
  442. if (white_space.has_value())
  443. computed_values.set_white_space(white_space.value());
  444. auto float_ = computed_style.float_();
  445. if (float_.has_value())
  446. computed_values.set_float(float_.value());
  447. auto clear = computed_style.clear();
  448. if (clear.has_value())
  449. computed_values.set_clear(clear.value());
  450. auto overflow_x = computed_style.overflow_x();
  451. if (overflow_x.has_value())
  452. computed_values.set_overflow_x(overflow_x.value());
  453. auto overflow_y = computed_style.overflow_y();
  454. if (overflow_y.has_value())
  455. computed_values.set_overflow_y(overflow_y.value());
  456. auto cursor = computed_style.cursor();
  457. if (cursor.has_value())
  458. computed_values.set_cursor(cursor.value());
  459. auto image_rendering = computed_style.image_rendering();
  460. if (image_rendering.has_value())
  461. computed_values.set_image_rendering(image_rendering.value());
  462. auto pointer_events = computed_style.pointer_events();
  463. if (pointer_events.has_value())
  464. computed_values.set_pointer_events(pointer_events.value());
  465. computed_values.set_text_decoration_line(computed_style.text_decoration_line());
  466. auto text_decoration_style = computed_style.text_decoration_style();
  467. if (text_decoration_style.has_value())
  468. computed_values.set_text_decoration_style(text_decoration_style.value());
  469. auto text_transform = computed_style.text_transform();
  470. if (text_transform.has_value())
  471. computed_values.set_text_transform(text_transform.value());
  472. if (auto list_style_type = computed_style.list_style_type(); list_style_type.has_value())
  473. computed_values.set_list_style_type(list_style_type.value());
  474. auto list_style_image = computed_style.property(CSS::PropertyID::ListStyleImage);
  475. if (list_style_image->is_abstract_image()) {
  476. m_list_style_image = list_style_image->as_abstract_image();
  477. const_cast<CSS::AbstractImageStyleValue&>(*m_list_style_image).load_any_resources(document());
  478. }
  479. // FIXME: The default text decoration color value is `currentcolor`, but since we can't resolve that easily,
  480. // we just manually grab the value from `color`. This makes it dependent on `color` being
  481. // specified first, so it's far from ideal.
  482. computed_values.set_text_decoration_color(computed_style.color_or_fallback(CSS::PropertyID::TextDecorationColor, *this, computed_values.color()));
  483. if (auto maybe_text_decoration_thickness = computed_style.length_percentage(CSS::PropertyID::TextDecorationThickness); maybe_text_decoration_thickness.has_value())
  484. computed_values.set_text_decoration_thickness(maybe_text_decoration_thickness.release_value());
  485. computed_values.set_text_shadow(computed_style.text_shadow());
  486. computed_values.set_z_index(computed_style.z_index());
  487. computed_values.set_opacity(computed_style.opacity());
  488. if (auto maybe_visibility = computed_style.visibility(); maybe_visibility.has_value())
  489. computed_values.set_visibility(maybe_visibility.release_value());
  490. m_visible = computed_values.opacity() != 0 && computed_values.visibility() == CSS::Visibility::Visible;
  491. computed_values.set_width(computed_style.size_value(CSS::PropertyID::Width));
  492. computed_values.set_min_width(computed_style.size_value(CSS::PropertyID::MinWidth));
  493. computed_values.set_max_width(computed_style.size_value(CSS::PropertyID::MaxWidth));
  494. computed_values.set_height(computed_style.size_value(CSS::PropertyID::Height));
  495. computed_values.set_min_height(computed_style.size_value(CSS::PropertyID::MinHeight));
  496. computed_values.set_max_height(computed_style.size_value(CSS::PropertyID::MaxHeight));
  497. computed_values.set_inset(computed_style.length_box(CSS::PropertyID::Left, CSS::PropertyID::Top, CSS::PropertyID::Right, CSS::PropertyID::Bottom, CSS::Length::make_auto()));
  498. 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)));
  499. 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)));
  500. computed_values.set_box_shadow(computed_style.box_shadow());
  501. computed_values.set_transformations(computed_style.transformations());
  502. computed_values.set_transform_origin(computed_style.transform_origin());
  503. auto do_border_style = [&](CSS::BorderData& border, CSS::PropertyID width_property, CSS::PropertyID color_property, CSS::PropertyID style_property) {
  504. // FIXME: The default border color value is `currentcolor`, but since we can't resolve that easily,
  505. // we just manually grab the value from `color`. This makes it dependent on `color` being
  506. // specified first, so it's far from ideal.
  507. border.color = computed_style.color_or_fallback(color_property, *this, computed_values.color());
  508. border.line_style = computed_style.line_style(style_property).value_or(CSS::LineStyle::None);
  509. // https://w3c.github.io/csswg-drafts/css-backgrounds/#border-style
  510. // none
  511. // 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.
  512. // hidden
  513. // Same as none, but has different behavior in the border conflict resolution rules for border-collapsed tables [CSS2].
  514. if (border.line_style == CSS::LineStyle::None || border.line_style == CSS::LineStyle::Hidden) {
  515. border.width = 0;
  516. } else {
  517. auto resolve_border_width = [&]() -> double {
  518. auto value = computed_style.property(width_property);
  519. if (value->is_calculated())
  520. return value->as_calculated().resolve_length(*this)->to_px(*this).value();
  521. if (value->has_length())
  522. return value->to_length().to_px(*this).value();
  523. if (value->is_identifier()) {
  524. // https://www.w3.org/TR/css-backgrounds-3/#valdef-line-width-thin
  525. switch (value->to_identifier()) {
  526. case CSS::ValueID::Thin:
  527. return 1.0;
  528. case CSS::ValueID::Medium:
  529. return 3.0;
  530. case CSS::ValueID::Thick:
  531. return 5.0;
  532. default:
  533. VERIFY_NOT_REACHED();
  534. }
  535. }
  536. VERIFY_NOT_REACHED();
  537. };
  538. border.width = resolve_border_width();
  539. }
  540. };
  541. do_border_style(computed_values.border_left(), CSS::PropertyID::BorderLeftWidth, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftStyle);
  542. do_border_style(computed_values.border_top(), CSS::PropertyID::BorderTopWidth, CSS::PropertyID::BorderTopColor, CSS::PropertyID::BorderTopStyle);
  543. do_border_style(computed_values.border_right(), CSS::PropertyID::BorderRightWidth, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightStyle);
  544. do_border_style(computed_values.border_bottom(), CSS::PropertyID::BorderBottomWidth, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomStyle);
  545. computed_values.set_content(computed_style.content());
  546. computed_values.set_grid_auto_columns(computed_style.grid_auto_columns());
  547. computed_values.set_grid_auto_rows(computed_style.grid_auto_rows());
  548. computed_values.set_grid_template_columns(computed_style.grid_template_columns());
  549. computed_values.set_grid_template_rows(computed_style.grid_template_rows());
  550. computed_values.set_grid_column_end(computed_style.grid_column_end());
  551. computed_values.set_grid_column_start(computed_style.grid_column_start());
  552. computed_values.set_grid_row_end(computed_style.grid_row_end());
  553. computed_values.set_grid_row_start(computed_style.grid_row_start());
  554. computed_values.set_grid_template_areas(computed_style.grid_template_areas());
  555. auto fill = computed_style.property(CSS::PropertyID::Fill);
  556. if (fill->has_color())
  557. computed_values.set_fill(fill->to_color(*this));
  558. else if (fill->is_url())
  559. computed_values.set_fill(fill->as_url().url());
  560. // TODO: Allow url()s for strokes
  561. if (auto stroke = computed_style.property(CSS::PropertyID::Stroke); stroke->has_color())
  562. computed_values.set_stroke(stroke->to_color(*this));
  563. if (auto stop_color = computed_style.property(CSS::PropertyID::StopColor); stop_color->has_color())
  564. computed_values.set_stop_color(stop_color->to_color(*this));
  565. auto stroke_width = computed_style.property(CSS::PropertyID::StrokeWidth);
  566. // FIXME: Converting to pixels isn't really correct - values should be in "user units"
  567. // https://svgwg.org/svg2-draft/coords.html#TermUserUnits
  568. if (stroke_width->is_numeric())
  569. computed_values.set_stroke_width(CSS::Length::make_px(stroke_width->as_numeric().number()));
  570. else if (stroke_width->is_length())
  571. computed_values.set_stroke_width(stroke_width->to_length());
  572. else if (stroke_width->is_percentage())
  573. computed_values.set_stroke_width(CSS::LengthPercentage { stroke_width->as_percentage().percentage() });
  574. computed_values.set_fill_opacity(computed_style.fill_opacity());
  575. computed_values.set_stroke_opacity(computed_style.stroke_opacity());
  576. computed_values.set_stop_opacity(computed_style.stop_opacity());
  577. computed_values.set_column_gap(computed_style.size_value(CSS::PropertyID::ColumnGap));
  578. computed_values.set_row_gap(computed_style.size_value(CSS::PropertyID::RowGap));
  579. if (auto border_collapse = computed_style.border_collapse(); border_collapse.has_value())
  580. computed_values.set_border_collapse(border_collapse.value());
  581. }
  582. bool Node::is_root_element() const
  583. {
  584. if (is_anonymous())
  585. return false;
  586. return is<HTML::HTMLHtmlElement>(*dom_node());
  587. }
  588. DeprecatedString Node::debug_description() const
  589. {
  590. StringBuilder builder;
  591. builder.append(class_name());
  592. if (dom_node()) {
  593. builder.appendff("<{}>", dom_node()->node_name());
  594. if (dom_node()->is_element()) {
  595. auto& element = static_cast<DOM::Element const&>(*dom_node());
  596. if (auto id = element.get_attribute(HTML::AttributeNames::id); !id.is_null())
  597. builder.appendff("#{}", id);
  598. for (auto const& class_name : element.class_names())
  599. builder.appendff(".{}", class_name);
  600. }
  601. } else {
  602. builder.append("(anonymous)"sv);
  603. }
  604. return builder.to_deprecated_string();
  605. }
  606. CSS::Display Node::display() const
  607. {
  608. if (!has_style()) {
  609. // NOTE: No style means this is dumb text content.
  610. return CSS::Display(CSS::Display::Outside::Inline, CSS::Display::Inside::Flow);
  611. }
  612. return computed_values().display();
  613. }
  614. bool Node::is_inline() const
  615. {
  616. return display().is_inline_outside();
  617. }
  618. bool Node::is_inline_block() const
  619. {
  620. auto display = this->display();
  621. return display.is_inline_outside() && display.is_flow_root_inside();
  622. }
  623. bool Node::is_inline_table() const
  624. {
  625. auto display = this->display();
  626. return display.is_inline_outside() && display.is_table_inside();
  627. }
  628. JS::NonnullGCPtr<NodeWithStyle> NodeWithStyle::create_anonymous_wrapper() const
  629. {
  630. auto wrapper = heap().allocate_without_realm<BlockContainer>(const_cast<DOM::Document&>(document()), nullptr, m_computed_values.clone_inherited_values());
  631. static_cast<CSS::MutableComputedValues&>(wrapper->m_computed_values).set_display(CSS::Display(CSS::Display::Outside::Block, CSS::Display::Inside::Flow));
  632. wrapper->m_font = m_font;
  633. wrapper->m_line_height = m_line_height;
  634. return *wrapper;
  635. }
  636. void NodeWithStyle::reset_table_box_computed_values_used_by_wrapper_to_init_values()
  637. {
  638. VERIFY(is<TableBox>(*this));
  639. CSS::MutableComputedValues& mutable_computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
  640. mutable_computed_values.set_position(CSS::Position::Static);
  641. mutable_computed_values.set_float(CSS::Float::None);
  642. mutable_computed_values.set_clear(CSS::Clear::None);
  643. mutable_computed_values.set_inset({ CSS::Length::make_auto(), CSS::Length::make_auto(), CSS::Length::make_auto(), CSS::Length::make_auto() });
  644. mutable_computed_values.set_margin({ CSS::Length::make_px(0), CSS::Length::make_px(0), CSS::Length::make_px(0), CSS::Length::make_px(0) });
  645. }
  646. void Node::set_paintable(JS::GCPtr<Painting::Paintable> paintable)
  647. {
  648. m_paintable = move(paintable);
  649. }
  650. JS::GCPtr<Painting::Paintable> Node::create_paintable() const
  651. {
  652. return nullptr;
  653. }
  654. bool Node::is_anonymous() const
  655. {
  656. return m_anonymous;
  657. }
  658. DOM::Node const* Node::dom_node() const
  659. {
  660. if (m_anonymous)
  661. return nullptr;
  662. return m_dom_node.ptr();
  663. }
  664. DOM::Node* Node::dom_node()
  665. {
  666. if (m_anonymous)
  667. return nullptr;
  668. return m_dom_node.ptr();
  669. }
  670. DOM::Document& Node::document()
  671. {
  672. return m_dom_node->document();
  673. }
  674. DOM::Document const& Node::document() const
  675. {
  676. return m_dom_node->document();
  677. }
  678. }