Node.cpp 36 KB

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