File: whitespace_attacher.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (105 lines) | stat: -rw-r--r-- 4,116 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_DOM_WHITESPACE_ATTACHER_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_DOM_WHITESPACE_ATTACHER_H_

#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/heap/member.h"

namespace blink {

class Element;
class LayoutObject;
class Node;
class Text;

// The WhitespaceAttacher is used during the layout tree rebuild to lazily re-
// attach whitespace LayoutObjects when necessary. For more details about white-
// space LayoutObjects, see the WhitespaceLayoutObjects.md file in this
// directory.
//
// As RebuildLayoutTree walks from last to first child, we track the last text
// node, or the last skipped display:contents element we have seen. These are
// reset to null as soon as we encounter an in-flow element.
//
// If the tracked text node needed a (re-)attach, we call
// ReattachWhitespaceSiblings once we visit or re-attach the first preceding
// in-flow.
//
// If we re-attach a preceding in-flow, we also call ReattachWhitespaceSiblings
// since the need for a succeeding whitespace LayoutObject may change.

class CORE_EXPORT WhitespaceAttacher {
  STACK_ALLOCATED();

 public:
  WhitespaceAttacher() = default;
  ~WhitespaceAttacher();

  void DidVisitText(Text*);
  void DidReattachText(Text*);
  void DidVisitElement(Element*);
  void DidReattachElement(Element*, LayoutObject*);
  bool LastTextNodeNeedsReattach() const {
    return last_text_node_needs_reattach_;
  }
  void SetReattachAllWhitespaceNodes() {
    reattach_all_whitespace_nodes_ = true;
  }
  bool TraverseIntoDisplayContents() const {
    return last_text_node_needs_reattach_ || reattach_all_whitespace_nodes_;
  }

 private:
  void DidReattach(Node*, LayoutObject*);
  void ReattachWhitespaceSiblings(LayoutObject* previous_in_flow);
  void ForceLastTextNodeNeedsReattach();
  void UpdateLastTextNodeFromDisplayContents();

  void SetLastTextNode(Text* text) {
    last_display_contents_ = nullptr;
    last_text_node_ = text;
    if (!text)
      last_text_node_needs_reattach_ = false;
  }

  // If we encounter a display:contents, without traversing its flat tree
  // children during layout tree rebuild, we store that element and start
  // traversing it for text nodes as needed if we re-attach a preceding node
  // without encountering a text node or an in-flow element first.
  //
  // If there is already a text node which needs re-attachment, we traverse into
  // the display:contents element instead as we need to find the last in-flow
  // descendant of that subtree which is used to check if the re-attached text
  // node needs a LayoutText or not.
  //
  // Invariants:
  // DCHECK(!last_display_contents_ || !last_text_node_needs_reattach_)
  // DCHECK(last_text_node_ || !last_text_node_needs_reattach_)
  Element* last_display_contents_ = nullptr;

  // The last text node we've visited during rebuild for this attacher.
  Text* last_text_node_ = nullptr;

  // Set to true if we need to re-attach last_text_node_ when:
  // 1. We visiting a previous in-flow sibling, or
  // 2. We get to the start of the sibling list during the rebuild.
  bool last_text_node_needs_reattach_ = false;

  // Removing a node from the DOM may cause the need for a whitespace
  // LayoutObject to be attached or detached. When the display type changes on
  // an element, the WhitespaceAttacher keeps track of the last text node seen
  // and re-attaches whitespaces as necessary during the tree walk. When
  // removing an element from the tree, we can not selectively track which
  // whitespace nodes which needs to be checked for re-attachment. Thus, we need
  // to check all LayoutText children of the layout tree parent of the removed
  // node for re-attachment. Set to true when all whitespace children needs to
  // be checked for re-attachement.
  bool reattach_all_whitespace_nodes_ = false;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_DOM_WHITESPACE_ATTACHER_H_