Node.cpp 24 KB

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