ViewportPaintable.cpp 25 KB

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