File: style_scope.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 (84 lines) | stat: -rw-r--r-- 3,584 bytes parent folder | download | duplicates (4)
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
// Copyright 2022 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_CSS_STYLE_SCOPE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_CSS_STYLE_SCOPE_H_

#include <optional>

#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/css/css_selector_list.h"
#include "third_party/blink/renderer/core/css/parser/css_nesting_type.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/member.h"
#include "third_party/blink/renderer/platform/heap/visitor.h"

namespace blink {

class CSSParserTokenStream;
class StyleRule;
class StyleSheetContents;

class CORE_EXPORT StyleScope final : public GarbageCollected<StyleScope> {
 public:
  // Construct a StyleScope with explicit roots specified by elements matching
  // the `from` selector list (within the StyleRule). The (optional) `to`
  // parameter selects the the limit elements, i.e. the extent of the scope.
  //
  // Note that the `from` selector list is represented here as a "dummy"
  // StyleRule instead of a CSSSelectorList, because scopes need to behave
  // as style rules to integrate with CSS Nesting.
  // https://drafts.csswg.org/css-nesting-1/#nesting-at-scope
  StyleScope(StyleRule* from, CSSSelectorList* to);
  // Construct a StyleScope with implicit roots at the parent nodes of the
  // stylesheet's owner nodes. Note that StyleScopes with implicit roots
  // can still have limits.
  explicit StyleScope(StyleSheetContents* contents, CSSSelectorList* to);
  StyleScope(const StyleScope&);
  // Note that the `nesting_type` and `parent_rule_for_nesting` provided here
  // are only used for parsing the <scope-start> selector. The <scope-end>
  // selector and style rules within the scope's body will use
  // CSSNestingType::kScope and `RuleForNesting()` instead.
  static StyleScope* Parse(CSSParserTokenStream& stream,
                           const CSSParserContext* context,
                           CSSNestingType nesting_type,
                           StyleRule* parent_rule_for_nesting,
                           StyleSheetContents* style_sheet);

  void Trace(blink::Visitor*) const;

  StyleScope* CopyWithParent(const StyleScope*) const;

  // From() and To() both return the first CSSSelector in a list, or nullptr
  // if there is no list.
  const CSSSelector* From() const;
  const CSSSelector* To() const;
  const StyleScope* Parent() const { return parent_.Get(); }

  // The rule to use for resolving the nesting selector (&) for this scope's
  // inner rules.
  StyleRule* RuleForNesting() const { return from_.Get(); }

  // Returns a copy of StyleScope, with any '&' selectors in the prelude updated
  // to `new_parent`. If no '&' selectors required an update, returns 'this'.
  //
  // See also CSSSelector::Renest.
  const StyleScope* Renest(StyleRule* new_parent) const;

  // https://drafts.csswg.org/css-cascade-6/#implicit-scope
  bool IsImplicit() const { return contents_.Get() != nullptr; }

 private:
  // If `contents_` is not nullptr, then this is a prelude-less @scope rule
  // which is implicitly scoped to the owner node's parent.
  Member<StyleSheetContents> contents_;
  Member<StyleRule> from_;      // May be nullptr.
  Member<CSSSelectorList> to_;  // May be nullptr.
  Member<const StyleScope> parent_;
  mutable std::optional<unsigned> specificity_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_CSS_STYLE_SCOPE_H_