Node.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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& style)
  167. {
  168. // https://drafts.csswg.org/css-sizing-3/#definite
  169. auto is_definite_size = [&](CSS::PropertyID property_id, bool width) {
  170. // A size that can be determined without performing layout; that is,
  171. // a <length>,
  172. // a measure of text (without consideration of line-wrapping),
  173. // a size of the initial containing block,
  174. // or a <percentage> or other formula (such as the “stretch-fit” sizing of non-replaced blocks [CSS2]) that is resolved solely against definite sizes.
  175. auto maybe_value = style.property(property_id);
  176. auto const* containing_block = this->containing_block();
  177. auto containing_block_has_definite_size = containing_block && (width ? containing_block->m_has_definite_width : containing_block->m_has_definite_height);
  178. if (maybe_value->has_auto()) {
  179. // NOTE: The width of a non-flex-item block is considered definite if it's auto and the containing block has definite width.
  180. if (width && is_block_container() && parent() && !parent()->computed_values().display().is_flex_inside())
  181. return containing_block_has_definite_size;
  182. return false;
  183. }
  184. auto maybe_length_percentage = style.length_percentage(property_id);
  185. if (!maybe_length_percentage.has_value())
  186. return false;
  187. auto length_percentage = maybe_length_percentage.release_value();
  188. if (length_percentage.is_length())
  189. return true;
  190. if (length_percentage.is_percentage())
  191. return containing_block_has_definite_size;
  192. // FIXME: Determine if calc() value is definite.
  193. return false;
  194. };
  195. m_has_definite_width = is_definite_size(CSS::PropertyID::Width, true);
  196. m_has_definite_height = is_definite_size(CSS::PropertyID::Height, false);
  197. }
  198. void NodeWithStyle::apply_style(const CSS::StyleProperties& computed_style)
  199. {
  200. auto& computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
  201. // NOTE: We have to be careful that font-related properties get set in the right order.
  202. // m_font is used by Length::to_px() when resolving sizes against this layout node.
  203. // That's why it has to be set before everything else.
  204. m_font = computed_style.computed_font();
  205. computed_values.set_font_size(computed_style.property(CSS::PropertyID::FontSize)->to_length().to_px(*this));
  206. computed_values.set_font_weight(computed_style.property(CSS::PropertyID::FontWeight)->to_integer());
  207. m_line_height = computed_style.line_height(*this);
  208. computed_values.set_vertical_align(computed_style.vertical_align());
  209. {
  210. auto attachments = computed_style.property(CSS::PropertyID::BackgroundAttachment);
  211. auto clips = computed_style.property(CSS::PropertyID::BackgroundClip);
  212. auto images = computed_style.property(CSS::PropertyID::BackgroundImage);
  213. auto origins = computed_style.property(CSS::PropertyID::BackgroundOrigin);
  214. auto positions = computed_style.property(CSS::PropertyID::BackgroundPosition);
  215. auto repeats = computed_style.property(CSS::PropertyID::BackgroundRepeat);
  216. auto sizes = computed_style.property(CSS::PropertyID::BackgroundSize);
  217. auto count_layers = [](auto maybe_style_value) -> size_t {
  218. if (maybe_style_value->is_value_list())
  219. return maybe_style_value->as_value_list().size();
  220. else
  221. return 1;
  222. };
  223. auto value_for_layer = [](auto& style_value, size_t layer_index) -> RefPtr<CSS::StyleValue> {
  224. if (style_value->is_value_list())
  225. return style_value->as_value_list().value_at(layer_index, true);
  226. return style_value;
  227. };
  228. size_t layer_count = 1;
  229. layer_count = max(layer_count, count_layers(attachments));
  230. layer_count = max(layer_count, count_layers(clips));
  231. layer_count = max(layer_count, count_layers(images));
  232. layer_count = max(layer_count, count_layers(origins));
  233. layer_count = max(layer_count, count_layers(positions));
  234. layer_count = max(layer_count, count_layers(repeats));
  235. layer_count = max(layer_count, count_layers(sizes));
  236. Vector<CSS::BackgroundLayerData> layers;
  237. layers.ensure_capacity(layer_count);
  238. for (size_t layer_index = 0; layer_index < layer_count; layer_index++) {
  239. CSS::BackgroundLayerData layer;
  240. if (auto image_value = value_for_layer(images, layer_index); image_value && image_value->is_image()) {
  241. layer.image = image_value->as_image();
  242. layer.image->load_bitmap(document());
  243. }
  244. if (auto attachment_value = value_for_layer(attachments, layer_index); attachment_value && attachment_value->has_identifier()) {
  245. switch (attachment_value->to_identifier()) {
  246. case CSS::ValueID::Fixed:
  247. layer.attachment = CSS::BackgroundAttachment::Fixed;
  248. break;
  249. case CSS::ValueID::Local:
  250. layer.attachment = CSS::BackgroundAttachment::Local;
  251. break;
  252. case CSS::ValueID::Scroll:
  253. layer.attachment = CSS::BackgroundAttachment::Scroll;
  254. break;
  255. default:
  256. break;
  257. }
  258. }
  259. auto as_box = [](auto value_id) {
  260. switch (value_id) {
  261. case CSS::ValueID::BorderBox:
  262. return CSS::BackgroundBox::BorderBox;
  263. case CSS::ValueID::ContentBox:
  264. return CSS::BackgroundBox::ContentBox;
  265. case CSS::ValueID::PaddingBox:
  266. return CSS::BackgroundBox::PaddingBox;
  267. default:
  268. VERIFY_NOT_REACHED();
  269. }
  270. };
  271. if (auto origin_value = value_for_layer(origins, layer_index); origin_value && origin_value->has_identifier()) {
  272. layer.origin = as_box(origin_value->to_identifier());
  273. }
  274. if (auto clip_value = value_for_layer(clips, layer_index); clip_value && clip_value->has_identifier()) {
  275. layer.clip = as_box(clip_value->to_identifier());
  276. }
  277. if (auto position_value = value_for_layer(positions, layer_index); position_value && position_value->is_position()) {
  278. auto& position = position_value->as_position();
  279. layer.position_edge_x = position.edge_x();
  280. layer.position_edge_y = position.edge_y();
  281. layer.position_offset_x = position.offset_x();
  282. layer.position_offset_y = position.offset_y();
  283. }
  284. if (auto size_value = value_for_layer(sizes, layer_index); size_value) {
  285. if (size_value->is_background_size()) {
  286. auto& size = size_value->as_background_size();
  287. layer.size_type = CSS::BackgroundSize::LengthPercentage;
  288. layer.size_x = size.size_x();
  289. layer.size_y = size.size_y();
  290. } else if (size_value->has_identifier()) {
  291. switch (size_value->to_identifier()) {
  292. case CSS::ValueID::Contain:
  293. layer.size_type = CSS::BackgroundSize::Contain;
  294. break;
  295. case CSS::ValueID::Cover:
  296. layer.size_type = CSS::BackgroundSize::Cover;
  297. break;
  298. default:
  299. break;
  300. }
  301. }
  302. }
  303. if (auto repeat_value = value_for_layer(repeats, layer_index); repeat_value && repeat_value->is_background_repeat()) {
  304. layer.repeat_x = repeat_value->as_background_repeat().repeat_x();
  305. layer.repeat_y = repeat_value->as_background_repeat().repeat_y();
  306. }
  307. layers.append(move(layer));
  308. }
  309. computed_values.set_background_layers(move(layers));
  310. }
  311. computed_values.set_background_color(computed_style.color_or_fallback(CSS::PropertyID::BackgroundColor, *this, CSS::InitialValues::background_color()));
  312. if (auto box_sizing = computed_style.box_sizing(); box_sizing.has_value())
  313. computed_values.set_box_sizing(box_sizing.release_value());
  314. if (auto maybe_font_variant = computed_style.font_variant(); maybe_font_variant.has_value())
  315. computed_values.set_font_variant(maybe_font_variant.release_value());
  316. // FIXME: BorderXRadius properties are now BorderRadiusStyleValues, so make use of that.
  317. auto border_bottom_left_radius = computed_style.property(CSS::PropertyID::BorderBottomLeftRadius);
  318. if (border_bottom_left_radius->is_border_radius()) {
  319. computed_values.set_border_bottom_left_radius(
  320. CSS::BorderRadiusData {
  321. border_bottom_left_radius->as_border_radius().horizontal_radius(),
  322. border_bottom_left_radius->as_border_radius().vertical_radius() });
  323. }
  324. auto border_bottom_right_radius = computed_style.property(CSS::PropertyID::BorderBottomRightRadius);
  325. if (border_bottom_right_radius->is_border_radius()) {
  326. computed_values.set_border_bottom_right_radius(
  327. CSS::BorderRadiusData {
  328. border_bottom_right_radius->as_border_radius().horizontal_radius(),
  329. border_bottom_right_radius->as_border_radius().vertical_radius() });
  330. }
  331. auto border_top_left_radius = computed_style.property(CSS::PropertyID::BorderTopLeftRadius);
  332. if (border_top_left_radius->is_border_radius()) {
  333. computed_values.set_border_top_left_radius(
  334. CSS::BorderRadiusData {
  335. border_top_left_radius->as_border_radius().horizontal_radius(),
  336. border_top_left_radius->as_border_radius().vertical_radius() });
  337. }
  338. auto border_top_right_radius = computed_style.property(CSS::PropertyID::BorderTopRightRadius);
  339. if (border_top_right_radius->is_border_radius()) {
  340. computed_values.set_border_top_right_radius(
  341. CSS::BorderRadiusData {
  342. border_top_right_radius->as_border_radius().horizontal_radius(),
  343. border_top_right_radius->as_border_radius().vertical_radius() });
  344. }
  345. computed_values.set_display(computed_style.display());
  346. auto flex_direction = computed_style.flex_direction();
  347. if (flex_direction.has_value())
  348. computed_values.set_flex_direction(flex_direction.value());
  349. auto flex_wrap = computed_style.flex_wrap();
  350. if (flex_wrap.has_value())
  351. computed_values.set_flex_wrap(flex_wrap.value());
  352. auto flex_basis = computed_style.flex_basis();
  353. if (flex_basis.has_value())
  354. computed_values.set_flex_basis(flex_basis.value());
  355. computed_values.set_flex_grow(computed_style.flex_grow());
  356. computed_values.set_flex_shrink(computed_style.flex_shrink());
  357. computed_values.set_order(computed_style.order());
  358. auto justify_content = computed_style.justify_content();
  359. if (justify_content.has_value())
  360. computed_values.set_justify_content(justify_content.value());
  361. auto align_items = computed_style.align_items();
  362. if (align_items.has_value())
  363. computed_values.set_align_items(align_items.value());
  364. auto align_self = computed_style.align_self();
  365. if (align_self.has_value())
  366. computed_values.set_align_self(align_self.value());
  367. auto position = computed_style.position();
  368. if (position.has_value())
  369. computed_values.set_position(position.value());
  370. auto text_align = computed_style.text_align();
  371. if (text_align.has_value())
  372. computed_values.set_text_align(text_align.value());
  373. auto text_justify = computed_style.text_justify();
  374. if (text_align.has_value())
  375. computed_values.set_text_justify(text_justify.value());
  376. auto white_space = computed_style.white_space();
  377. if (white_space.has_value())
  378. computed_values.set_white_space(white_space.value());
  379. auto float_ = computed_style.float_();
  380. if (float_.has_value())
  381. computed_values.set_float(float_.value());
  382. auto clear = computed_style.clear();
  383. if (clear.has_value())
  384. computed_values.set_clear(clear.value());
  385. auto overflow_x = computed_style.overflow_x();
  386. if (overflow_x.has_value())
  387. computed_values.set_overflow_x(overflow_x.value());
  388. auto overflow_y = computed_style.overflow_y();
  389. if (overflow_y.has_value())
  390. computed_values.set_overflow_y(overflow_y.value());
  391. auto cursor = computed_style.cursor();
  392. if (cursor.has_value())
  393. computed_values.set_cursor(cursor.value());
  394. auto image_rendering = computed_style.image_rendering();
  395. if (image_rendering.has_value())
  396. computed_values.set_image_rendering(image_rendering.value());
  397. auto pointer_events = computed_style.pointer_events();
  398. if (pointer_events.has_value())
  399. computed_values.set_pointer_events(pointer_events.value());
  400. computed_values.set_text_decoration_line(computed_style.text_decoration_line());
  401. auto text_decoration_style = computed_style.text_decoration_style();
  402. if (text_decoration_style.has_value())
  403. computed_values.set_text_decoration_style(text_decoration_style.value());
  404. auto text_transform = computed_style.text_transform();
  405. if (text_transform.has_value())
  406. computed_values.set_text_transform(text_transform.value());
  407. if (auto list_style_type = computed_style.list_style_type(); list_style_type.has_value())
  408. computed_values.set_list_style_type(list_style_type.value());
  409. auto list_style_image = computed_style.property(CSS::PropertyID::ListStyleImage);
  410. if (list_style_image->is_image()) {
  411. m_list_style_image = list_style_image->as_image();
  412. m_list_style_image->load_bitmap(document());
  413. }
  414. computed_values.set_color(computed_style.color_or_fallback(CSS::PropertyID::Color, *this, CSS::InitialValues::color()));
  415. // FIXME: The default text decoration color value is `currentcolor`, but since we can't resolve that easily,
  416. // we just manually grab the value from `color`. This makes it dependent on `color` being
  417. // specified first, so it's far from ideal.
  418. computed_values.set_text_decoration_color(computed_style.color_or_fallback(CSS::PropertyID::TextDecorationColor, *this, computed_values.color()));
  419. if (auto maybe_text_decoration_thickness = computed_style.length_percentage(CSS::PropertyID::TextDecorationThickness); maybe_text_decoration_thickness.has_value())
  420. computed_values.set_text_decoration_thickness(maybe_text_decoration_thickness.release_value());
  421. computed_values.set_text_shadow(computed_style.text_shadow());
  422. computed_values.set_z_index(computed_style.z_index());
  423. computed_values.set_opacity(computed_style.opacity());
  424. if (auto maybe_visibility = computed_style.visibility(); maybe_visibility.has_value())
  425. computed_values.set_visibility(maybe_visibility.release_value());
  426. if (computed_values.opacity() == 0 || computed_values.visibility() != CSS::Visibility::Visible)
  427. m_visible = false;
  428. if (auto maybe_length_percentage = computed_style.length_percentage(CSS::PropertyID::Width); maybe_length_percentage.has_value())
  429. computed_values.set_width(maybe_length_percentage.release_value());
  430. if (auto maybe_length_percentage = computed_style.length_percentage(CSS::PropertyID::MinWidth); maybe_length_percentage.has_value())
  431. computed_values.set_min_width(maybe_length_percentage.release_value());
  432. if (auto maybe_length_percentage = computed_style.length_percentage(CSS::PropertyID::MaxWidth); maybe_length_percentage.has_value())
  433. computed_values.set_max_width(maybe_length_percentage.release_value());
  434. if (auto maybe_length_percentage = computed_style.length_percentage(CSS::PropertyID::Height); maybe_length_percentage.has_value())
  435. computed_values.set_height(maybe_length_percentage.release_value());
  436. if (auto maybe_length_percentage = computed_style.length_percentage(CSS::PropertyID::MinHeight); maybe_length_percentage.has_value())
  437. computed_values.set_min_height(maybe_length_percentage.release_value());
  438. if (auto maybe_length_percentage = computed_style.length_percentage(CSS::PropertyID::MaxHeight); maybe_length_percentage.has_value())
  439. computed_values.set_max_height(maybe_length_percentage.release_value());
  440. computed_values.set_inset(computed_style.length_box(CSS::PropertyID::Left, CSS::PropertyID::Top, CSS::PropertyID::Right, CSS::PropertyID::Bottom, CSS::Length::make_auto()));
  441. 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)));
  442. 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)));
  443. computed_values.set_box_shadow(computed_style.box_shadow());
  444. computed_values.set_transformations(computed_style.transformations());
  445. computed_values.set_transform_origin(computed_style.transform_origin());
  446. auto do_border_style = [&](CSS::BorderData& border, CSS::PropertyID width_property, CSS::PropertyID color_property, CSS::PropertyID style_property) {
  447. // FIXME: The default border color value is `currentcolor`, but since we can't resolve that easily,
  448. // we just manually grab the value from `color`. This makes it dependent on `color` being
  449. // specified first, so it's far from ideal.
  450. border.color = computed_style.color_or_fallback(color_property, *this, computed_values.color());
  451. border.line_style = computed_style.line_style(style_property).value_or(CSS::LineStyle::None);
  452. if (border.line_style == CSS::LineStyle::None)
  453. border.width = 0;
  454. else
  455. border.width = computed_style.length_or_fallback(width_property, CSS::Length::make_px(0)).to_px(*this);
  456. };
  457. do_border_style(computed_values.border_left(), CSS::PropertyID::BorderLeftWidth, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftStyle);
  458. do_border_style(computed_values.border_top(), CSS::PropertyID::BorderTopWidth, CSS::PropertyID::BorderTopColor, CSS::PropertyID::BorderTopStyle);
  459. do_border_style(computed_values.border_right(), CSS::PropertyID::BorderRightWidth, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightStyle);
  460. do_border_style(computed_values.border_bottom(), CSS::PropertyID::BorderBottomWidth, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomStyle);
  461. computed_values.set_content(computed_style.content());
  462. if (auto fill = computed_style.property(CSS::PropertyID::Fill); fill->has_color())
  463. computed_values.set_fill(fill->to_color(*this));
  464. if (auto stroke = computed_style.property(CSS::PropertyID::Stroke); stroke->has_color())
  465. computed_values.set_stroke(stroke->to_color(*this));
  466. auto stroke_width = computed_style.property(CSS::PropertyID::StrokeWidth);
  467. // FIXME: Converting to pixels isn't really correct - values should be in "user units"
  468. // https://svgwg.org/svg2-draft/coords.html#TermUserUnits
  469. if (stroke_width->is_numeric())
  470. computed_values.set_stroke_width(CSS::Length::make_px(stroke_width->to_number()));
  471. else
  472. computed_values.set_stroke_width(stroke_width->to_length());
  473. }
  474. bool Node::is_root_element() const
  475. {
  476. if (is_anonymous())
  477. return false;
  478. return is<HTML::HTMLHtmlElement>(*dom_node());
  479. }
  480. String Node::class_name() const
  481. {
  482. auto const* mangled_name = typeid(*this).name();
  483. return demangle({ mangled_name, strlen(mangled_name) });
  484. }
  485. String Node::debug_description() const
  486. {
  487. StringBuilder builder;
  488. builder.append(class_name().substring_view(13));
  489. if (dom_node()) {
  490. builder.appendff("<{}>", dom_node()->node_name());
  491. if (dom_node()->is_element()) {
  492. auto& element = static_cast<DOM::Element const&>(*dom_node());
  493. if (auto id = element.get_attribute(HTML::AttributeNames::id); !id.is_null())
  494. builder.appendff("#{}", id);
  495. for (auto const& class_name : element.class_names())
  496. builder.appendff(".{}", class_name);
  497. }
  498. } else {
  499. builder.append("(anonymous)");
  500. }
  501. return builder.to_string();
  502. }
  503. bool Node::is_inline_block() const
  504. {
  505. return is_inline() && is<BlockContainer>(*this);
  506. }
  507. NonnullRefPtr<NodeWithStyle> NodeWithStyle::create_anonymous_wrapper() const
  508. {
  509. auto wrapper = adopt_ref(*new BlockContainer(const_cast<DOM::Document&>(document()), nullptr, m_computed_values.clone_inherited_values()));
  510. wrapper->m_font = m_font;
  511. wrapper->m_line_height = m_line_height;
  512. return wrapper;
  513. }
  514. void Node::set_paintable(RefPtr<Painting::Paintable> paintable)
  515. {
  516. m_paintable = move(paintable);
  517. }
  518. RefPtr<Painting::Paintable> Node::create_paintable() const
  519. {
  520. return nullptr;
  521. }
  522. }