Node.cpp 32 KB

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