Node.cpp 41 KB

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