Node.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Demangle.h>
  7. #include <LibGfx/Font/FontDatabase.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/Dump.h>
  10. #include <LibWeb/HTML/BrowsingContext.h>
  11. #include <LibWeb/HTML/HTMLHtmlElement.h>
  12. #include <LibWeb/Layout/BlockContainer.h>
  13. #include <LibWeb/Layout/FormattingContext.h>
  14. #include <LibWeb/Layout/InitialContainingBlock.h>
  15. #include <LibWeb/Layout/Node.h>
  16. #include <LibWeb/Layout/TextNode.h>
  17. #include <typeinfo>
  18. namespace Web::Layout {
  19. Node::Node(DOM::Document& document, DOM::Node* node)
  20. : m_document(document)
  21. , m_dom_node(node)
  22. {
  23. m_serial_id = m_document->next_layout_node_serial_id({});
  24. if (m_dom_node)
  25. m_dom_node->set_layout_node({}, this);
  26. }
  27. Node::~Node()
  28. {
  29. if (m_dom_node && m_dom_node->layout_node() == this)
  30. m_dom_node->set_layout_node({}, nullptr);
  31. }
  32. // https://www.w3.org/TR/css-display-3/#out-of-flow
  33. bool Node::is_out_of_flow(FormattingContext const& formatting_context) const
  34. {
  35. // A layout node is out of flow if either:
  36. // 1. It is floated (which requires that floating is not inhibited).
  37. if (!formatting_context.inhibits_floating() && computed_values().float_() != CSS::Float::None)
  38. return true;
  39. // 2. It is "absolutely positioned".
  40. if (is_absolutely_positioned())
  41. return true;
  42. return false;
  43. }
  44. bool Node::can_contain_boxes_with_position_absolute() const
  45. {
  46. return computed_values().position() != CSS::Position::Static || is<InitialContainingBlock>(*this);
  47. }
  48. BlockContainer const* Node::containing_block() const
  49. {
  50. if (is<TextNode>(*this))
  51. return first_ancestor_of_type<BlockContainer>();
  52. auto position = computed_values().position();
  53. if (position == CSS::Position::Absolute) {
  54. auto* ancestor = parent();
  55. while (ancestor && !ancestor->can_contain_boxes_with_position_absolute())
  56. ancestor = ancestor->parent();
  57. while (ancestor && (!is<BlockContainer>(*ancestor) || ancestor->is_anonymous()))
  58. ancestor = ancestor->containing_block();
  59. return static_cast<BlockContainer const*>(ancestor);
  60. }
  61. if (position == CSS::Position::Fixed)
  62. return &root();
  63. return first_ancestor_of_type<BlockContainer>();
  64. }
  65. bool Node::establishes_stacking_context() const
  66. {
  67. if (!has_style())
  68. return false;
  69. if (dom_node() == &document().root())
  70. return true;
  71. auto position = computed_values().position();
  72. if (position == CSS::Position::Absolute || position == CSS::Position::Relative || position == CSS::Position::Fixed || position == CSS::Position::Sticky)
  73. return true;
  74. if (!computed_values().transformations().is_empty())
  75. return true;
  76. return computed_values().opacity() < 1.0f;
  77. }
  78. HTML::BrowsingContext const& Node::browsing_context() const
  79. {
  80. VERIFY(document().browsing_context());
  81. return *document().browsing_context();
  82. }
  83. HTML::BrowsingContext& Node::browsing_context()
  84. {
  85. VERIFY(document().browsing_context());
  86. return *document().browsing_context();
  87. }
  88. InitialContainingBlock const& Node::root() const
  89. {
  90. VERIFY(document().layout_node());
  91. return *document().layout_node();
  92. }
  93. InitialContainingBlock& Node::root()
  94. {
  95. VERIFY(document().layout_node());
  96. return *document().layout_node();
  97. }
  98. void Node::set_needs_display()
  99. {
  100. if (auto* block = containing_block()) {
  101. block->paint_box()->for_each_fragment([&](auto& fragment) {
  102. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  103. browsing_context().set_needs_display(enclosing_int_rect(fragment.absolute_rect()));
  104. }
  105. return IterationDecision::Continue;
  106. });
  107. }
  108. }
  109. Gfx::FloatPoint Node::box_type_agnostic_position() const
  110. {
  111. if (is<Box>(*this))
  112. return verify_cast<Box>(*this).paint_box()->absolute_position();
  113. VERIFY(is_inline());
  114. Gfx::FloatPoint position;
  115. if (auto* block = containing_block()) {
  116. block->paint_box()->for_each_fragment([&](auto& fragment) {
  117. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  118. position = fragment.absolute_rect().location();
  119. return IterationDecision::Break;
  120. }
  121. return IterationDecision::Continue;
  122. });
  123. }
  124. return position;
  125. }
  126. bool Node::is_floating() const
  127. {
  128. if (!has_style())
  129. return false;
  130. // flex-items don't float.
  131. if (is_flex_item())
  132. return false;
  133. return computed_values().float_() != CSS::Float::None;
  134. }
  135. bool Node::is_positioned() const
  136. {
  137. return has_style() && computed_values().position() != CSS::Position::Static;
  138. }
  139. bool Node::is_absolutely_positioned() const
  140. {
  141. if (!has_style())
  142. return false;
  143. auto position = computed_values().position();
  144. return position == CSS::Position::Absolute || position == CSS::Position::Fixed;
  145. }
  146. bool Node::is_fixed_position() const
  147. {
  148. if (!has_style())
  149. return false;
  150. auto position = computed_values().position();
  151. return position == CSS::Position::Fixed;
  152. }
  153. NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> computed_style)
  154. : Node(document, node)
  155. {
  156. m_has_style = true;
  157. apply_style(*computed_style);
  158. }
  159. NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  160. : Node(document, node)
  161. , m_computed_values(move(computed_values))
  162. {
  163. m_has_style = true;
  164. m_font = Gfx::FontDatabase::default_font();
  165. }
  166. void NodeWithStyle::did_insert_into_layout_tree(CSS::StyleProperties const&)
  167. {
  168. }
  169. void NodeWithStyle::apply_style(const CSS::StyleProperties& computed_style)
  170. {
  171. auto& computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
  172. // NOTE: We have to be careful that font-related properties get set in the right order.
  173. // m_font is used by Length::to_px() when resolving sizes against this layout node.
  174. // That's why it has to be set before everything else.
  175. m_font = computed_style.computed_font();
  176. computed_values.set_font_size(computed_style.property(CSS::PropertyID::FontSize)->to_length().to_px(*this));
  177. computed_values.set_font_weight(computed_style.property(CSS::PropertyID::FontWeight)->to_integer());
  178. m_line_height = computed_style.line_height(*this);
  179. computed_values.set_vertical_align(computed_style.vertical_align());
  180. {
  181. auto attachments = computed_style.property(CSS::PropertyID::BackgroundAttachment);
  182. auto clips = computed_style.property(CSS::PropertyID::BackgroundClip);
  183. auto images = computed_style.property(CSS::PropertyID::BackgroundImage);
  184. auto origins = computed_style.property(CSS::PropertyID::BackgroundOrigin);
  185. auto positions = computed_style.property(CSS::PropertyID::BackgroundPosition);
  186. auto repeats = computed_style.property(CSS::PropertyID::BackgroundRepeat);
  187. auto sizes = computed_style.property(CSS::PropertyID::BackgroundSize);
  188. auto count_layers = [](auto maybe_style_value) -> size_t {
  189. if (maybe_style_value->is_value_list())
  190. return maybe_style_value->as_value_list().size();
  191. else
  192. return 1;
  193. };
  194. auto value_for_layer = [](auto& style_value, size_t layer_index) -> RefPtr<CSS::StyleValue> {
  195. if (style_value->is_value_list())
  196. return style_value->as_value_list().value_at(layer_index, true);
  197. return style_value;
  198. };
  199. size_t layer_count = 1;
  200. layer_count = max(layer_count, count_layers(attachments));
  201. layer_count = max(layer_count, count_layers(clips));
  202. layer_count = max(layer_count, count_layers(images));
  203. layer_count = max(layer_count, count_layers(origins));
  204. layer_count = max(layer_count, count_layers(positions));
  205. layer_count = max(layer_count, count_layers(repeats));
  206. layer_count = max(layer_count, count_layers(sizes));
  207. Vector<CSS::BackgroundLayerData> layers;
  208. layers.ensure_capacity(layer_count);
  209. for (size_t layer_index = 0; layer_index < layer_count; layer_index++) {
  210. CSS::BackgroundLayerData layer;
  211. if (auto image_value = value_for_layer(images, layer_index); image_value) {
  212. if (image_value->is_abstract_image()) {
  213. layer.background_image = image_value->as_abstract_image();
  214. layer.background_image->load_any_resources(document());
  215. }
  216. }
  217. if (auto attachment_value = value_for_layer(attachments, layer_index); attachment_value && attachment_value->has_identifier()) {
  218. switch (attachment_value->to_identifier()) {
  219. case CSS::ValueID::Fixed:
  220. layer.attachment = CSS::BackgroundAttachment::Fixed;
  221. break;
  222. case CSS::ValueID::Local:
  223. layer.attachment = CSS::BackgroundAttachment::Local;
  224. break;
  225. case CSS::ValueID::Scroll:
  226. layer.attachment = CSS::BackgroundAttachment::Scroll;
  227. break;
  228. default:
  229. break;
  230. }
  231. }
  232. auto as_box = [](auto value_id) {
  233. switch (value_id) {
  234. case CSS::ValueID::BorderBox:
  235. return CSS::BackgroundBox::BorderBox;
  236. case CSS::ValueID::ContentBox:
  237. return CSS::BackgroundBox::ContentBox;
  238. case CSS::ValueID::PaddingBox:
  239. return CSS::BackgroundBox::PaddingBox;
  240. default:
  241. VERIFY_NOT_REACHED();
  242. }
  243. };
  244. if (auto origin_value = value_for_layer(origins, layer_index); origin_value && origin_value->has_identifier()) {
  245. layer.origin = as_box(origin_value->to_identifier());
  246. }
  247. if (auto clip_value = value_for_layer(clips, layer_index); clip_value && clip_value->has_identifier()) {
  248. layer.clip = as_box(clip_value->to_identifier());
  249. }
  250. if (auto position_value = value_for_layer(positions, layer_index); position_value && position_value->is_position()) {
  251. auto& position = position_value->as_position();
  252. layer.position_edge_x = position.edge_x();
  253. layer.position_edge_y = position.edge_y();
  254. layer.position_offset_x = position.offset_x();
  255. layer.position_offset_y = position.offset_y();
  256. }
  257. if (auto size_value = value_for_layer(sizes, layer_index); size_value) {
  258. if (size_value->is_background_size()) {
  259. auto& size = size_value->as_background_size();
  260. layer.size_type = CSS::BackgroundSize::LengthPercentage;
  261. layer.size_x = size.size_x();
  262. layer.size_y = size.size_y();
  263. } else if (size_value->has_identifier()) {
  264. switch (size_value->to_identifier()) {
  265. case CSS::ValueID::Contain:
  266. layer.size_type = CSS::BackgroundSize::Contain;
  267. break;
  268. case CSS::ValueID::Cover:
  269. layer.size_type = CSS::BackgroundSize::Cover;
  270. break;
  271. default:
  272. break;
  273. }
  274. }
  275. }
  276. if (auto repeat_value = value_for_layer(repeats, layer_index); repeat_value && repeat_value->is_background_repeat()) {
  277. layer.repeat_x = repeat_value->as_background_repeat().repeat_x();
  278. layer.repeat_y = repeat_value->as_background_repeat().repeat_y();
  279. }
  280. layers.append(move(layer));
  281. }
  282. computed_values.set_background_layers(move(layers));
  283. }
  284. computed_values.set_background_color(computed_style.color_or_fallback(CSS::PropertyID::BackgroundColor, *this, CSS::InitialValues::background_color()));
  285. if (auto box_sizing = computed_style.box_sizing(); box_sizing.has_value())
  286. computed_values.set_box_sizing(box_sizing.release_value());
  287. if (auto maybe_font_variant = computed_style.font_variant(); maybe_font_variant.has_value())
  288. computed_values.set_font_variant(maybe_font_variant.release_value());
  289. // FIXME: BorderXRadius properties are now BorderRadiusStyleValues, so make use of that.
  290. auto border_bottom_left_radius = computed_style.property(CSS::PropertyID::BorderBottomLeftRadius);
  291. if (border_bottom_left_radius->is_border_radius()) {
  292. computed_values.set_border_bottom_left_radius(
  293. CSS::BorderRadiusData {
  294. border_bottom_left_radius->as_border_radius().horizontal_radius(),
  295. border_bottom_left_radius->as_border_radius().vertical_radius() });
  296. }
  297. auto border_bottom_right_radius = computed_style.property(CSS::PropertyID::BorderBottomRightRadius);
  298. if (border_bottom_right_radius->is_border_radius()) {
  299. computed_values.set_border_bottom_right_radius(
  300. CSS::BorderRadiusData {
  301. border_bottom_right_radius->as_border_radius().horizontal_radius(),
  302. border_bottom_right_radius->as_border_radius().vertical_radius() });
  303. }
  304. auto border_top_left_radius = computed_style.property(CSS::PropertyID::BorderTopLeftRadius);
  305. if (border_top_left_radius->is_border_radius()) {
  306. computed_values.set_border_top_left_radius(
  307. CSS::BorderRadiusData {
  308. border_top_left_radius->as_border_radius().horizontal_radius(),
  309. border_top_left_radius->as_border_radius().vertical_radius() });
  310. }
  311. auto border_top_right_radius = computed_style.property(CSS::PropertyID::BorderTopRightRadius);
  312. if (border_top_right_radius->is_border_radius()) {
  313. computed_values.set_border_top_right_radius(
  314. CSS::BorderRadiusData {
  315. border_top_right_radius->as_border_radius().horizontal_radius(),
  316. border_top_right_radius->as_border_radius().vertical_radius() });
  317. }
  318. computed_values.set_display(computed_style.display());
  319. auto flex_direction = computed_style.flex_direction();
  320. if (flex_direction.has_value())
  321. computed_values.set_flex_direction(flex_direction.value());
  322. auto flex_wrap = computed_style.flex_wrap();
  323. if (flex_wrap.has_value())
  324. computed_values.set_flex_wrap(flex_wrap.value());
  325. auto flex_basis = computed_style.flex_basis();
  326. if (flex_basis.has_value())
  327. computed_values.set_flex_basis(flex_basis.value());
  328. computed_values.set_flex_grow(computed_style.flex_grow());
  329. computed_values.set_flex_shrink(computed_style.flex_shrink());
  330. computed_values.set_order(computed_style.order());
  331. computed_values.set_clip(computed_style.clip());
  332. auto justify_content = computed_style.justify_content();
  333. if (justify_content.has_value())
  334. computed_values.set_justify_content(justify_content.value());
  335. auto align_items = computed_style.align_items();
  336. if (align_items.has_value())
  337. computed_values.set_align_items(align_items.value());
  338. auto align_self = computed_style.align_self();
  339. if (align_self.has_value())
  340. computed_values.set_align_self(align_self.value());
  341. auto appearance = computed_style.appearance();
  342. if (appearance.has_value())
  343. computed_values.set_appearance(appearance.value());
  344. auto position = computed_style.position();
  345. if (position.has_value())
  346. computed_values.set_position(position.value());
  347. auto text_align = computed_style.text_align();
  348. if (text_align.has_value())
  349. computed_values.set_text_align(text_align.value());
  350. auto text_justify = computed_style.text_justify();
  351. if (text_align.has_value())
  352. computed_values.set_text_justify(text_justify.value());
  353. auto white_space = computed_style.white_space();
  354. if (white_space.has_value())
  355. computed_values.set_white_space(white_space.value());
  356. auto float_ = computed_style.float_();
  357. if (float_.has_value())
  358. computed_values.set_float(float_.value());
  359. auto clear = computed_style.clear();
  360. if (clear.has_value())
  361. computed_values.set_clear(clear.value());
  362. auto overflow_x = computed_style.overflow_x();
  363. if (overflow_x.has_value())
  364. computed_values.set_overflow_x(overflow_x.value());
  365. auto overflow_y = computed_style.overflow_y();
  366. if (overflow_y.has_value())
  367. computed_values.set_overflow_y(overflow_y.value());
  368. auto cursor = computed_style.cursor();
  369. if (cursor.has_value())
  370. computed_values.set_cursor(cursor.value());
  371. auto image_rendering = computed_style.image_rendering();
  372. if (image_rendering.has_value())
  373. computed_values.set_image_rendering(image_rendering.value());
  374. auto pointer_events = computed_style.pointer_events();
  375. if (pointer_events.has_value())
  376. computed_values.set_pointer_events(pointer_events.value());
  377. computed_values.set_text_decoration_line(computed_style.text_decoration_line());
  378. auto text_decoration_style = computed_style.text_decoration_style();
  379. if (text_decoration_style.has_value())
  380. computed_values.set_text_decoration_style(text_decoration_style.value());
  381. auto text_transform = computed_style.text_transform();
  382. if (text_transform.has_value())
  383. computed_values.set_text_transform(text_transform.value());
  384. if (auto list_style_type = computed_style.list_style_type(); list_style_type.has_value())
  385. computed_values.set_list_style_type(list_style_type.value());
  386. auto list_style_image = computed_style.property(CSS::PropertyID::ListStyleImage);
  387. if (list_style_image->is_abstract_image()) {
  388. m_list_style_image = list_style_image->as_abstract_image();
  389. m_list_style_image->load_any_resources(document());
  390. }
  391. computed_values.set_color(computed_style.color_or_fallback(CSS::PropertyID::Color, *this, CSS::InitialValues::color()));
  392. // FIXME: The default text decoration color value is `currentcolor`, but since we can't resolve that easily,
  393. // we just manually grab the value from `color`. This makes it dependent on `color` being
  394. // specified first, so it's far from ideal.
  395. computed_values.set_text_decoration_color(computed_style.color_or_fallback(CSS::PropertyID::TextDecorationColor, *this, computed_values.color()));
  396. if (auto maybe_text_decoration_thickness = computed_style.length_percentage(CSS::PropertyID::TextDecorationThickness); maybe_text_decoration_thickness.has_value())
  397. computed_values.set_text_decoration_thickness(maybe_text_decoration_thickness.release_value());
  398. computed_values.set_text_shadow(computed_style.text_shadow());
  399. computed_values.set_z_index(computed_style.z_index());
  400. computed_values.set_opacity(computed_style.opacity());
  401. if (auto maybe_visibility = computed_style.visibility(); maybe_visibility.has_value())
  402. computed_values.set_visibility(maybe_visibility.release_value());
  403. if (computed_values.opacity() == 0 || computed_values.visibility() != CSS::Visibility::Visible)
  404. m_visible = false;
  405. if (auto maybe_length_percentage = computed_style.length_percentage(CSS::PropertyID::Width); maybe_length_percentage.has_value())
  406. computed_values.set_width(maybe_length_percentage.release_value());
  407. if (auto maybe_length_percentage = computed_style.length_percentage(CSS::PropertyID::MinWidth); maybe_length_percentage.has_value())
  408. computed_values.set_min_width(maybe_length_percentage.release_value());
  409. if (auto maybe_length_percentage = computed_style.length_percentage(CSS::PropertyID::MaxWidth); maybe_length_percentage.has_value())
  410. computed_values.set_max_width(maybe_length_percentage.release_value());
  411. if (auto maybe_length_percentage = computed_style.length_percentage(CSS::PropertyID::Height); maybe_length_percentage.has_value())
  412. computed_values.set_height(maybe_length_percentage.release_value());
  413. if (auto maybe_length_percentage = computed_style.length_percentage(CSS::PropertyID::MinHeight); maybe_length_percentage.has_value())
  414. computed_values.set_min_height(maybe_length_percentage.release_value());
  415. if (auto maybe_length_percentage = computed_style.length_percentage(CSS::PropertyID::MaxHeight); maybe_length_percentage.has_value())
  416. computed_values.set_max_height(maybe_length_percentage.release_value());
  417. computed_values.set_inset(computed_style.length_box(CSS::PropertyID::Left, CSS::PropertyID::Top, CSS::PropertyID::Right, CSS::PropertyID::Bottom, CSS::Length::make_auto()));
  418. 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)));
  419. 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)));
  420. computed_values.set_box_shadow(computed_style.box_shadow());
  421. computed_values.set_transformations(computed_style.transformations());
  422. computed_values.set_transform_origin(computed_style.transform_origin());
  423. auto do_border_style = [&](CSS::BorderData& border, CSS::PropertyID width_property, CSS::PropertyID color_property, CSS::PropertyID style_property) {
  424. // FIXME: The default border color value is `currentcolor`, but since we can't resolve that easily,
  425. // we just manually grab the value from `color`. This makes it dependent on `color` being
  426. // specified first, so it's far from ideal.
  427. border.color = computed_style.color_or_fallback(color_property, *this, computed_values.color());
  428. border.line_style = computed_style.line_style(style_property).value_or(CSS::LineStyle::None);
  429. if (border.line_style == CSS::LineStyle::None)
  430. border.width = 0;
  431. else
  432. border.width = computed_style.length_or_fallback(width_property, CSS::Length::make_px(0)).to_px(*this);
  433. };
  434. do_border_style(computed_values.border_left(), CSS::PropertyID::BorderLeftWidth, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftStyle);
  435. do_border_style(computed_values.border_top(), CSS::PropertyID::BorderTopWidth, CSS::PropertyID::BorderTopColor, CSS::PropertyID::BorderTopStyle);
  436. do_border_style(computed_values.border_right(), CSS::PropertyID::BorderRightWidth, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightStyle);
  437. do_border_style(computed_values.border_bottom(), CSS::PropertyID::BorderBottomWidth, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomStyle);
  438. computed_values.set_content(computed_style.content());
  439. computed_values.set_grid_template_columns(computed_style.grid_template_columns());
  440. computed_values.set_grid_template_rows(computed_style.grid_template_rows());
  441. computed_values.set_grid_column_end(computed_style.grid_column_end());
  442. computed_values.set_grid_column_start(computed_style.grid_column_start());
  443. computed_values.set_grid_row_end(computed_style.grid_row_end());
  444. computed_values.set_grid_row_start(computed_style.grid_row_start());
  445. if (auto fill = computed_style.property(CSS::PropertyID::Fill); fill->has_color())
  446. computed_values.set_fill(fill->to_color(*this));
  447. if (auto stroke = computed_style.property(CSS::PropertyID::Stroke); stroke->has_color())
  448. computed_values.set_stroke(stroke->to_color(*this));
  449. auto stroke_width = computed_style.property(CSS::PropertyID::StrokeWidth);
  450. // FIXME: Converting to pixels isn't really correct - values should be in "user units"
  451. // https://svgwg.org/svg2-draft/coords.html#TermUserUnits
  452. if (stroke_width->is_numeric())
  453. computed_values.set_stroke_width(CSS::Length::make_px(stroke_width->to_number()));
  454. else
  455. computed_values.set_stroke_width(stroke_width->to_length());
  456. }
  457. bool Node::is_root_element() const
  458. {
  459. if (is_anonymous())
  460. return false;
  461. return is<HTML::HTMLHtmlElement>(*dom_node());
  462. }
  463. String Node::class_name() const
  464. {
  465. auto const* mangled_name = typeid(*this).name();
  466. return demangle({ mangled_name, strlen(mangled_name) });
  467. }
  468. String Node::debug_description() const
  469. {
  470. StringBuilder builder;
  471. builder.append(class_name().substring_view(13));
  472. if (dom_node()) {
  473. builder.appendff("<{}>", dom_node()->node_name());
  474. if (dom_node()->is_element()) {
  475. auto& element = static_cast<DOM::Element const&>(*dom_node());
  476. if (auto id = element.get_attribute(HTML::AttributeNames::id); !id.is_null())
  477. builder.appendff("#{}", id);
  478. for (auto const& class_name : element.class_names())
  479. builder.appendff(".{}", class_name);
  480. }
  481. } else {
  482. builder.append("(anonymous)"sv);
  483. }
  484. return builder.to_string();
  485. }
  486. bool Node::is_inline_block() const
  487. {
  488. return is_inline() && is<BlockContainer>(*this);
  489. }
  490. NonnullRefPtr<NodeWithStyle> NodeWithStyle::create_anonymous_wrapper() const
  491. {
  492. auto wrapper = adopt_ref(*new BlockContainer(const_cast<DOM::Document&>(document()), nullptr, m_computed_values.clone_inherited_values()));
  493. wrapper->m_font = m_font;
  494. wrapper->m_line_height = m_line_height;
  495. return wrapper;
  496. }
  497. void Node::set_paintable(RefPtr<Painting::Paintable> paintable)
  498. {
  499. m_paintable = move(paintable);
  500. }
  501. RefPtr<Painting::Paintable> Node::create_paintable() const
  502. {
  503. return nullptr;
  504. }
  505. }