ViewportPaintable.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Range.h>
  7. #include <LibWeb/Layout/Viewport.h>
  8. #include <LibWeb/Painting/SVGPaintable.h>
  9. #include <LibWeb/Painting/SVGSVGPaintable.h>
  10. #include <LibWeb/Painting/StackingContext.h>
  11. #include <LibWeb/Painting/ViewportPaintable.h>
  12. #include <LibWeb/Selection/Selection.h>
  13. namespace Web::Painting {
  14. JS_DEFINE_ALLOCATOR(ViewportPaintable);
  15. JS::NonnullGCPtr<ViewportPaintable> ViewportPaintable::create(Layout::Viewport const& layout_viewport)
  16. {
  17. return layout_viewport.heap().allocate_without_realm<ViewportPaintable>(layout_viewport);
  18. }
  19. ViewportPaintable::ViewportPaintable(Layout::Viewport const& layout_viewport)
  20. : PaintableWithLines(layout_viewport)
  21. {
  22. }
  23. ViewportPaintable::~ViewportPaintable() = default;
  24. void ViewportPaintable::build_stacking_context_tree_if_needed()
  25. {
  26. if (stacking_context())
  27. return;
  28. build_stacking_context_tree();
  29. }
  30. void ViewportPaintable::build_stacking_context_tree()
  31. {
  32. set_stacking_context(make<StackingContext>(*this, nullptr, 0));
  33. size_t index_in_tree_order = 1;
  34. for_each_in_subtree([&](Paintable const& paintable) {
  35. const_cast<Paintable&>(paintable).invalidate_stacking_context();
  36. auto* parent_context = const_cast<Paintable&>(paintable).enclosing_stacking_context();
  37. auto establishes_stacking_context = paintable.layout_node().establishes_stacking_context();
  38. if ((paintable.is_positioned() || establishes_stacking_context) && paintable.computed_values().z_index().value_or(0) == 0)
  39. parent_context->m_positioned_descendants_with_stack_level_0_and_stacking_contexts.append(paintable);
  40. if (!paintable.is_positioned() && paintable.is_floating())
  41. parent_context->m_non_positioned_floating_descendants.append(paintable);
  42. if (!establishes_stacking_context) {
  43. VERIFY(!paintable.stacking_context());
  44. return TraversalDecision::Continue;
  45. }
  46. VERIFY(parent_context);
  47. const_cast<Paintable&>(paintable).set_stacking_context(make<Painting::StackingContext>(const_cast<Paintable&>(paintable), parent_context, index_in_tree_order++));
  48. return TraversalDecision::Continue;
  49. });
  50. stacking_context()->sort();
  51. }
  52. void ViewportPaintable::paint_all_phases(PaintContext& context)
  53. {
  54. build_stacking_context_tree_if_needed();
  55. context.recording_painter().translate(-context.device_viewport_rect().location().to_type<int>());
  56. stacking_context()->paint(context);
  57. }
  58. void ViewportPaintable::assign_scroll_frames()
  59. {
  60. int next_id = 0;
  61. for_each_in_subtree_of_type<PaintableBox>([&](auto const& paintable_box) {
  62. if (paintable_box.has_scrollable_overflow()) {
  63. auto scroll_frame = adopt_ref(*new ScrollFrame());
  64. scroll_frame->id = next_id++;
  65. scroll_state.set(paintable_box, move(scroll_frame));
  66. }
  67. return TraversalDecision::Continue;
  68. });
  69. for_each_in_subtree([&](auto const& paintable) {
  70. for (auto block = paintable.containing_block(); block; block = block->containing_block()) {
  71. if (auto scroll_frame = scroll_state.get(block); scroll_frame.has_value()) {
  72. if (paintable.is_paintable_box()) {
  73. auto const& paintable_box = static_cast<PaintableBox const&>(paintable);
  74. const_cast<PaintableBox&>(paintable_box).set_enclosing_scroll_frame(scroll_frame.value());
  75. } else if (paintable.is_inline_paintable()) {
  76. auto const& inline_paintable = static_cast<InlinePaintable const&>(paintable);
  77. const_cast<InlinePaintable&>(inline_paintable).set_enclosing_scroll_frame(scroll_frame.value());
  78. }
  79. break;
  80. }
  81. }
  82. return TraversalDecision::Continue;
  83. });
  84. }
  85. void ViewportPaintable::assign_clip_frames()
  86. {
  87. for_each_in_subtree_of_type<PaintableBox>([&](auto const& paintable_box) {
  88. auto overflow_x = paintable_box.computed_values().overflow_x();
  89. auto overflow_y = paintable_box.computed_values().overflow_y();
  90. auto has_hidden_overflow = overflow_x != CSS::Overflow::Visible && overflow_y != CSS::Overflow::Visible;
  91. if (has_hidden_overflow || paintable_box.get_clip_rect().has_value()) {
  92. auto clip_frame = adopt_ref(*new ClipFrame());
  93. clip_state.set(paintable_box, move(clip_frame));
  94. }
  95. return TraversalDecision::Continue;
  96. });
  97. for_each_in_subtree([&](auto const& paintable) {
  98. for (auto block = paintable.containing_block(); block; block = block->containing_block()) {
  99. if (auto clip_frame = clip_state.get(block); clip_frame.has_value()) {
  100. if (paintable.is_paintable_box()) {
  101. auto const& paintable_box = static_cast<PaintableBox const&>(paintable);
  102. const_cast<PaintableBox&>(paintable_box).set_enclosing_clip_frame(clip_frame.value());
  103. } else if (paintable.is_inline_paintable()) {
  104. auto const& inline_paintable = static_cast<InlinePaintable const&>(paintable);
  105. const_cast<InlinePaintable&>(inline_paintable).set_enclosing_clip_frame(clip_frame.value());
  106. }
  107. break;
  108. }
  109. }
  110. return TraversalDecision::Continue;
  111. });
  112. }
  113. void ViewportPaintable::refresh_scroll_state()
  114. {
  115. for (auto& it : scroll_state) {
  116. auto const& paintable_box = *it.key;
  117. auto& scroll_frame = *it.value;
  118. CSSPixelPoint offset;
  119. for (auto const* block = &paintable_box.layout_box(); block; block = block->containing_block()) {
  120. auto const& block_paintable_box = *block->paintable_box();
  121. offset.translate_by(block_paintable_box.scroll_offset());
  122. }
  123. scroll_frame.offset = -offset;
  124. }
  125. }
  126. void ViewportPaintable::refresh_clip_state()
  127. {
  128. for (auto& it : clip_state) {
  129. auto const& paintable_box = *it.key;
  130. auto& clip_frame = *it.value;
  131. auto overflow_x = paintable_box.computed_values().overflow_x();
  132. auto overflow_y = paintable_box.computed_values().overflow_y();
  133. // Start from CSS clip property if it exists.
  134. Optional<CSSPixelRect> clip_rect = paintable_box.get_clip_rect();
  135. clip_frame.clear_border_radii_clips();
  136. if (overflow_x != CSS::Overflow::Visible && overflow_y != CSS::Overflow::Visible) {
  137. auto overflow_clip_rect = paintable_box.compute_absolute_padding_rect_with_css_transform_applied();
  138. for (auto const* block = &paintable_box.layout_box(); !block->is_viewport(); block = block->containing_block()) {
  139. auto const& block_paintable_box = *block->paintable_box();
  140. auto block_overflow_x = block_paintable_box.computed_values().overflow_x();
  141. auto block_overflow_y = block_paintable_box.computed_values().overflow_y();
  142. if (block_overflow_x != CSS::Overflow::Visible && block_overflow_y != CSS::Overflow::Visible) {
  143. auto rect = block_paintable_box.compute_absolute_padding_rect_with_css_transform_applied();
  144. overflow_clip_rect.intersect(rect);
  145. auto border_radii_data = block_paintable_box.normalized_border_radii_data(ShrinkRadiiForBorders::Yes);
  146. if (border_radii_data.has_any_radius()) {
  147. BorderRadiiClip border_radii_clip { .rect = rect, .radii = border_radii_data };
  148. clip_frame.add_border_radii_clip(border_radii_clip);
  149. }
  150. }
  151. if (auto css_clip_property_rect = block->paintable_box()->get_clip_rect(); css_clip_property_rect.has_value())
  152. overflow_clip_rect.intersect(css_clip_property_rect.value());
  153. }
  154. clip_rect = overflow_clip_rect;
  155. }
  156. clip_frame.set_rect(*clip_rect);
  157. }
  158. }
  159. static Painting::BorderRadiiData normalize_border_radii_data(Layout::Node const& node, CSSPixelRect const& rect, CSS::BorderRadiusData const& top_left_radius, CSS::BorderRadiusData const& top_right_radius, CSS::BorderRadiusData const& bottom_right_radius, CSS::BorderRadiusData const& bottom_left_radius)
  160. {
  161. Painting::BorderRadiusData bottom_left_radius_px {};
  162. Painting::BorderRadiusData bottom_right_radius_px {};
  163. Painting::BorderRadiusData top_left_radius_px {};
  164. Painting::BorderRadiusData top_right_radius_px {};
  165. bottom_left_radius_px.horizontal_radius = bottom_left_radius.horizontal_radius.to_px(node, rect.width());
  166. bottom_right_radius_px.horizontal_radius = bottom_right_radius.horizontal_radius.to_px(node, rect.width());
  167. top_left_radius_px.horizontal_radius = top_left_radius.horizontal_radius.to_px(node, rect.width());
  168. top_right_radius_px.horizontal_radius = top_right_radius.horizontal_radius.to_px(node, rect.width());
  169. bottom_left_radius_px.vertical_radius = bottom_left_radius.vertical_radius.to_px(node, rect.height());
  170. bottom_right_radius_px.vertical_radius = bottom_right_radius.vertical_radius.to_px(node, rect.height());
  171. top_left_radius_px.vertical_radius = top_left_radius.vertical_radius.to_px(node, rect.height());
  172. top_right_radius_px.vertical_radius = top_right_radius.vertical_radius.to_px(node, rect.height());
  173. // Scale overlapping curves according to https://www.w3.org/TR/css-backgrounds-3/#corner-overlap
  174. // Let f = min(Li/Si), where i ∈ {top, right, bottom, left},
  175. // Si is the sum of the two corresponding radii of the corners on side i,
  176. // and Ltop = Lbottom = the width of the box, and Lleft = Lright = the height of the box.
  177. auto l_top = rect.width();
  178. auto l_bottom = l_top;
  179. auto l_left = rect.height();
  180. auto l_right = l_left;
  181. auto s_top = (top_left_radius_px.horizontal_radius + top_right_radius_px.horizontal_radius);
  182. auto s_right = (top_right_radius_px.vertical_radius + bottom_right_radius_px.vertical_radius);
  183. auto s_bottom = (bottom_left_radius_px.horizontal_radius + bottom_right_radius_px.horizontal_radius);
  184. auto s_left = (top_left_radius_px.vertical_radius + bottom_left_radius_px.vertical_radius);
  185. CSSPixelFraction f = 1;
  186. f = (s_top != 0) ? min(f, l_top / s_top) : f;
  187. f = (s_right != 0) ? min(f, l_right / s_right) : f;
  188. f = (s_bottom != 0) ? min(f, l_bottom / s_bottom) : f;
  189. f = (s_left != 0) ? min(f, l_left / s_left) : f;
  190. // If f < 1, then all corner radii are reduced by multiplying them by f.
  191. if (f < 1) {
  192. top_left_radius_px.horizontal_radius *= f;
  193. top_left_radius_px.vertical_radius *= f;
  194. top_right_radius_px.horizontal_radius *= f;
  195. top_right_radius_px.vertical_radius *= f;
  196. bottom_right_radius_px.horizontal_radius *= f;
  197. bottom_right_radius_px.vertical_radius *= f;
  198. bottom_left_radius_px.horizontal_radius *= f;
  199. bottom_left_radius_px.vertical_radius *= f;
  200. }
  201. return Painting::BorderRadiiData { top_left_radius_px, top_right_radius_px, bottom_right_radius_px, bottom_left_radius_px };
  202. }
  203. void ViewportPaintable::resolve_paint_only_properties()
  204. {
  205. // Resolves layout-dependent properties not handled during layout and stores them in the paint tree.
  206. // Properties resolved include:
  207. // - Border radii
  208. // - Box shadows
  209. // - Text shadows
  210. // - Transforms
  211. // - Transform origins
  212. // - Outlines
  213. for_each_in_inclusive_subtree([&](Paintable& paintable) {
  214. auto& layout_node = paintable.layout_node();
  215. auto const is_inline_paintable = paintable.is_inline_paintable();
  216. auto const is_paintable_box = paintable.is_paintable_box();
  217. auto const is_paintable_with_lines = paintable.is_paintable_with_lines();
  218. auto const& computed_values = layout_node.computed_values();
  219. // Border radii
  220. if (is_inline_paintable) {
  221. auto& inline_paintable = static_cast<Painting::InlinePaintable&>(paintable);
  222. auto& fragments = inline_paintable.fragments();
  223. auto const& top_left_border_radius = computed_values.border_top_left_radius();
  224. auto const& top_right_border_radius = computed_values.border_top_right_radius();
  225. auto const& bottom_right_border_radius = computed_values.border_bottom_right_radius();
  226. auto const& bottom_left_border_radius = computed_values.border_bottom_left_radius();
  227. auto containing_block_position_in_absolute_coordinates = inline_paintable.containing_block()->absolute_position();
  228. for (size_t i = 0; i < fragments.size(); ++i) {
  229. auto is_first_fragment = i == 0;
  230. auto is_last_fragment = i == fragments.size() - 1;
  231. auto& fragment = fragments[i];
  232. CSSPixelRect absolute_fragment_rect {
  233. containing_block_position_in_absolute_coordinates.translated(fragment.offset()),
  234. fragment.size()
  235. };
  236. if (is_first_fragment) {
  237. auto extra_start_width = inline_paintable.box_model().padding.left;
  238. absolute_fragment_rect.translate_by(-extra_start_width, 0);
  239. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_start_width);
  240. }
  241. if (is_last_fragment) {
  242. auto extra_end_width = inline_paintable.box_model().padding.right;
  243. absolute_fragment_rect.set_width(absolute_fragment_rect.width() + extra_end_width);
  244. }
  245. auto border_radii_data = normalize_border_radii_data(layout_node,
  246. absolute_fragment_rect, top_left_border_radius,
  247. top_right_border_radius,
  248. bottom_right_border_radius,
  249. bottom_left_border_radius);
  250. fragment.set_border_radii_data(border_radii_data);
  251. }
  252. }
  253. // Border radii
  254. if (is_paintable_box) {
  255. auto& paintable_box = static_cast<Painting::PaintableBox&>(paintable);
  256. CSSPixelRect const border_rect { 0, 0, paintable_box.border_box_width(), paintable_box.border_box_height() };
  257. auto const& border_top_left_radius = computed_values.border_top_left_radius();
  258. auto const& border_top_right_radius = computed_values.border_top_right_radius();
  259. auto const& border_bottom_right_radius = computed_values.border_bottom_right_radius();
  260. auto const& border_bottom_left_radius = computed_values.border_bottom_left_radius();
  261. auto radii_data = normalize_border_radii_data(layout_node, border_rect, border_top_left_radius,
  262. border_top_right_radius, border_bottom_right_radius,
  263. border_bottom_left_radius);
  264. paintable_box.set_border_radii_data(radii_data);
  265. }
  266. // Box shadows
  267. auto const& box_shadow_data = computed_values.box_shadow();
  268. if (!box_shadow_data.is_empty()) {
  269. Vector<Painting::ShadowData> resolved_box_shadow_data;
  270. resolved_box_shadow_data.ensure_capacity(box_shadow_data.size());
  271. for (auto const& layer : box_shadow_data) {
  272. resolved_box_shadow_data.empend(
  273. layer.color,
  274. layer.offset_x.to_px(layout_node),
  275. layer.offset_y.to_px(layout_node),
  276. layer.blur_radius.to_px(layout_node),
  277. layer.spread_distance.to_px(layout_node),
  278. layer.placement == CSS::ShadowPlacement::Outer ? Painting::ShadowPlacement::Outer
  279. : Painting::ShadowPlacement::Inner);
  280. }
  281. if (is<Painting::PaintableBox>(paintable)) {
  282. auto& paintable_box = static_cast<Painting::PaintableBox&>(paintable);
  283. paintable_box.set_box_shadow_data(move(resolved_box_shadow_data));
  284. } else if (is<Painting::InlinePaintable>(paintable)) {
  285. auto& inline_paintable = static_cast<Painting::InlinePaintable&>(paintable);
  286. inline_paintable.set_box_shadow_data(move(resolved_box_shadow_data));
  287. }
  288. }
  289. // Text shadows
  290. if (is_paintable_with_lines) {
  291. auto const& paintable_with_lines = static_cast<Painting::PaintableWithLines const&>(paintable);
  292. for (auto const& fragment : paintable_with_lines.fragments()) {
  293. auto const& text_shadow = fragment.m_layout_node->computed_values().text_shadow();
  294. if (!text_shadow.is_empty()) {
  295. Vector<Painting::ShadowData> resolved_shadow_data;
  296. resolved_shadow_data.ensure_capacity(text_shadow.size());
  297. for (auto const& layer : text_shadow) {
  298. resolved_shadow_data.empend(
  299. layer.color,
  300. layer.offset_x.to_px(layout_node),
  301. layer.offset_y.to_px(layout_node),
  302. layer.blur_radius.to_px(layout_node),
  303. layer.spread_distance.to_px(layout_node),
  304. Painting::ShadowPlacement::Outer);
  305. }
  306. const_cast<Painting::PaintableFragment&>(fragment).set_shadows(move(resolved_shadow_data));
  307. }
  308. }
  309. }
  310. // Transform and transform origin
  311. if (is_paintable_box) {
  312. auto& paintable_box = static_cast<Painting::PaintableBox&>(paintable);
  313. auto const& transformations = paintable_box.computed_values().transformations();
  314. if (!transformations.is_empty()) {
  315. auto matrix = Gfx::FloatMatrix4x4::identity();
  316. for (auto const& transform : transformations)
  317. matrix = matrix * transform.to_matrix(paintable_box).release_value();
  318. paintable_box.set_transform(matrix);
  319. }
  320. auto const& transform_origin = paintable_box.computed_values().transform_origin();
  321. // https://www.w3.org/TR/css-transforms-1/#transform-box
  322. auto transform_box = paintable_box.computed_values().transform_box();
  323. // For SVG elements without associated CSS layout box, the used value for content-box is fill-box and for
  324. // border-box is stroke-box.
  325. // FIXME: This currently detects any SVG element except the <svg> one. Is that correct?
  326. // And is it correct to use `else` below?
  327. if (is<Painting::SVGPaintable>(paintable_box)) {
  328. switch (transform_box) {
  329. case CSS::TransformBox::ContentBox:
  330. transform_box = CSS::TransformBox::FillBox;
  331. break;
  332. case CSS::TransformBox::BorderBox:
  333. transform_box = CSS::TransformBox::StrokeBox;
  334. break;
  335. default:
  336. break;
  337. }
  338. }
  339. // For elements with associated CSS layout box, the used value for fill-box is content-box and for
  340. // stroke-box and view-box is border-box.
  341. else {
  342. switch (transform_box) {
  343. case CSS::TransformBox::FillBox:
  344. transform_box = CSS::TransformBox::ContentBox;
  345. break;
  346. case CSS::TransformBox::StrokeBox:
  347. case CSS::TransformBox::ViewBox:
  348. transform_box = CSS::TransformBox::BorderBox;
  349. break;
  350. default:
  351. break;
  352. }
  353. }
  354. CSSPixelRect reference_box = [&]() {
  355. switch (transform_box) {
  356. case CSS::TransformBox::ContentBox:
  357. // Uses the content box as reference box.
  358. // FIXME: The reference box of a table is the border box of its table wrapper box, not its table box.
  359. return paintable_box.absolute_rect();
  360. case CSS::TransformBox::BorderBox:
  361. // Uses the border box as reference box.
  362. // FIXME: The reference box of a table is the border box of its table wrapper box, not its table box.
  363. return paintable_box.absolute_border_box_rect();
  364. case CSS::TransformBox::FillBox:
  365. // Uses the object bounding box as reference box.
  366. // FIXME: For now we're using the content rect as an approximation.
  367. return paintable_box.absolute_rect();
  368. case CSS::TransformBox::StrokeBox:
  369. // Uses the stroke bounding box as reference box.
  370. // FIXME: For now we're using the border rect as an approximation.
  371. return paintable_box.absolute_border_box_rect();
  372. case CSS::TransformBox::ViewBox:
  373. // Uses the nearest SVG viewport as reference box.
  374. // FIXME: If a viewBox attribute is specified for the SVG viewport creating element:
  375. // - The reference box is positioned at the origin of the coordinate system established by the viewBox attribute.
  376. // - The dimension of the reference box is set to the width and height values of the viewBox attribute.
  377. auto* svg_paintable = paintable_box.first_ancestor_of_type<Painting::SVGSVGPaintable>();
  378. if (!svg_paintable)
  379. return paintable_box.absolute_border_box_rect();
  380. return svg_paintable->absolute_rect();
  381. }
  382. VERIFY_NOT_REACHED();
  383. }();
  384. auto x = reference_box.left() + transform_origin.x.to_px(layout_node, reference_box.width());
  385. auto y = reference_box.top() + transform_origin.y.to_px(layout_node, reference_box.height());
  386. paintable_box.set_transform_origin({ x, y });
  387. paintable_box.set_transform_origin({ x, y });
  388. }
  389. // Outlines
  390. auto outline_width = computed_values.outline_width().to_px(layout_node);
  391. auto outline_data = borders_data_for_outline(layout_node, computed_values.outline_color(), computed_values.outline_style(), outline_width);
  392. auto outline_offset = computed_values.outline_offset().to_px(layout_node);
  393. if (is_paintable_box) {
  394. auto& paintable_box = static_cast<Painting::PaintableBox&>(paintable);
  395. paintable_box.set_outline_data(outline_data);
  396. paintable_box.set_outline_offset(outline_offset);
  397. } else if (is_inline_paintable) {
  398. auto& inline_paintable = static_cast<Painting::InlinePaintable&>(paintable);
  399. inline_paintable.set_outline_data(outline_data);
  400. inline_paintable.set_outline_offset(outline_offset);
  401. }
  402. return TraversalDecision::Continue;
  403. });
  404. }
  405. JS::GCPtr<Selection::Selection> ViewportPaintable::selection() const
  406. {
  407. return const_cast<DOM::Document&>(document()).get_selection();
  408. }
  409. void ViewportPaintable::recompute_selection_states()
  410. {
  411. // 1. Start by resetting the selection state of all layout nodes to None.
  412. for_each_in_inclusive_subtree([&](auto& layout_node) {
  413. layout_node.set_selection_state(SelectionState::None);
  414. return TraversalDecision::Continue;
  415. });
  416. // 2. If there is no active Selection or selected Range, return.
  417. auto selection = document().get_selection();
  418. if (!selection)
  419. return;
  420. auto range = selection->range();
  421. if (!range)
  422. return;
  423. auto* start_container = range->start_container();
  424. auto* end_container = range->end_container();
  425. // 3. If the selection starts and ends in the same node:
  426. if (start_container == end_container) {
  427. // 1. If the selection starts and ends at the same offset, return.
  428. if (range->start_offset() == range->end_offset()) {
  429. // NOTE: A zero-length selection should not be visible.
  430. return;
  431. }
  432. // 2. If it's a text node, mark it as StartAndEnd and return.
  433. if (is<DOM::Text>(*start_container)) {
  434. if (auto* paintable = start_container->paintable()) {
  435. paintable->set_selection_state(SelectionState::StartAndEnd);
  436. }
  437. return;
  438. }
  439. }
  440. if (start_container == end_container && is<DOM::Text>(*start_container)) {
  441. if (auto* paintable = start_container->paintable()) {
  442. paintable->set_selection_state(SelectionState::StartAndEnd);
  443. }
  444. return;
  445. }
  446. // 4. Mark the selection start node as Start (if text) or Full (if anything else).
  447. if (auto* paintable = start_container->paintable()) {
  448. if (is<DOM::Text>(*start_container))
  449. paintable->set_selection_state(SelectionState::Start);
  450. else
  451. paintable->set_selection_state(SelectionState::Full);
  452. }
  453. // 5. Mark the selection end node as End (if text) or Full (if anything else).
  454. if (auto* paintable = end_container->paintable()) {
  455. if (is<DOM::Text>(*end_container))
  456. paintable->set_selection_state(SelectionState::End);
  457. else
  458. paintable->set_selection_state(SelectionState::Full);
  459. }
  460. // 6. Mark the nodes between start node and end node (in tree order) as Full.
  461. for (auto* node = start_container->next_in_pre_order(); node && node != end_container; node = node->next_in_pre_order()) {
  462. if (auto* paintable = node->paintable())
  463. paintable->set_selection_state(SelectionState::Full);
  464. }
  465. }
  466. void ViewportPaintable::visit_edges(Visitor& visitor)
  467. {
  468. Base::visit_edges(visitor);
  469. visitor.visit(scroll_state);
  470. visitor.visit(clip_state);
  471. }
  472. }