summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/dom/shadow_dom_v0_test.cc
blob: c20d7e70dd28ef8345ac42f8946c8932452cf133 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/css_value_keywords.h"
#include "third_party/blink/renderer/core/dom/shadow_root.h"
#include "third_party/blink/renderer/core/dom/shadow_root_v0.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/html/html_body_element.h"
#include "third_party/blink/renderer/core/testing/sim/sim_request.h"
#include "third_party/blink/renderer/core/testing/sim/sim_test.h"

namespace blink {

namespace {

bool HasSelectorForIdInShadow(Element* host, const AtomicString& id) {
  DCHECK(host);
  return host->GetShadowRoot()->V0().EnsureSelectFeatureSet().HasSelectorForId(
      id);
}

bool HasSelectorForClassInShadow(Element* host,
                                 const AtomicString& class_name) {
  DCHECK(host);
  return host->GetShadowRoot()
      ->V0()
      .EnsureSelectFeatureSet()
      .HasSelectorForClass(class_name);
}

bool HasSelectorForAttributeInShadow(Element* host,
                                     const AtomicString& attribute_name) {
  DCHECK(host);
  return host->GetShadowRoot()
      ->V0()
      .EnsureSelectFeatureSet()
      .HasSelectorForAttribute(attribute_name);
}

class ShadowDOMV0Test : public SimTest {};

TEST_F(ShadowDOMV0Test, FeatureSetId) {
  LoadURL("about:blank");
  auto* host = GetDocument().CreateRawElement(html_names::kDivTag);
  auto* content = GetDocument().CreateRawElement(html_names::kContentTag);
  content->setAttribute("select", "#foo");
  host->CreateV0ShadowRootForTesting().AppendChild(content);
  EXPECT_TRUE(HasSelectorForIdInShadow(host, "foo"));
  EXPECT_FALSE(HasSelectorForIdInShadow(host, "bar"));
  EXPECT_FALSE(HasSelectorForIdInShadow(host, "host"));
  content->setAttribute("select", "#bar");
  EXPECT_TRUE(HasSelectorForIdInShadow(host, "bar"));
  EXPECT_FALSE(HasSelectorForIdInShadow(host, "foo"));
  content->setAttribute("select", "");
  EXPECT_FALSE(HasSelectorForIdInShadow(host, "bar"));
  EXPECT_FALSE(HasSelectorForIdInShadow(host, "foo"));
}

TEST_F(ShadowDOMV0Test, FeatureSetClassName) {
  LoadURL("about:blank");
  auto* host = GetDocument().CreateRawElement(html_names::kDivTag);
  auto* content = GetDocument().CreateRawElement(html_names::kContentTag);
  content->setAttribute("select", ".foo");
  host->CreateV0ShadowRootForTesting().AppendChild(content);
  EXPECT_TRUE(HasSelectorForClassInShadow(host, "foo"));
  EXPECT_FALSE(HasSelectorForClassInShadow(host, "bar"));
  EXPECT_FALSE(HasSelectorForClassInShadow(host, "host"));
  content->setAttribute("select", ".bar");
  EXPECT_TRUE(HasSelectorForClassInShadow(host, "bar"));
  EXPECT_FALSE(HasSelectorForClassInShadow(host, "foo"));
  content->setAttribute("select", "");
  EXPECT_FALSE(HasSelectorForClassInShadow(host, "bar"));
  EXPECT_FALSE(HasSelectorForClassInShadow(host, "foo"));
}

TEST_F(ShadowDOMV0Test, FeatureSetAttributeName) {
  LoadURL("about:blank");
  auto* host = GetDocument().CreateRawElement(html_names::kDivTag);
  auto* content = GetDocument().CreateRawElement(html_names::kContentTag);
  content->setAttribute("select", "div[foo]");
  host->CreateV0ShadowRootForTesting().AppendChild(content);
  EXPECT_TRUE(HasSelectorForAttributeInShadow(host, "foo"));
  EXPECT_FALSE(HasSelectorForAttributeInShadow(host, "bar"));
  EXPECT_FALSE(HasSelectorForAttributeInShadow(host, "host"));
  content->setAttribute("select", "div[bar]");
  EXPECT_TRUE(HasSelectorForAttributeInShadow(host, "bar"));
  EXPECT_FALSE(HasSelectorForAttributeInShadow(host, "foo"));
  content->setAttribute("select", "");
  EXPECT_FALSE(HasSelectorForAttributeInShadow(host, "bar"));
  EXPECT_FALSE(HasSelectorForAttributeInShadow(host, "foo"));
}

TEST_F(ShadowDOMV0Test, FeatureSetMultipleSelectors) {
  LoadURL("about:blank");
  auto* host = GetDocument().CreateRawElement(html_names::kDivTag);
  auto* content = GetDocument().CreateRawElement(html_names::kContentTag);
  content->setAttribute("select", "#foo,.bar,div[baz]");
  host->CreateV0ShadowRootForTesting().AppendChild(content);
  EXPECT_TRUE(HasSelectorForIdInShadow(host, "foo"));
  EXPECT_FALSE(HasSelectorForIdInShadow(host, "bar"));
  EXPECT_FALSE(HasSelectorForIdInShadow(host, "baz"));
  EXPECT_FALSE(HasSelectorForClassInShadow(host, "foo"));
  EXPECT_TRUE(HasSelectorForClassInShadow(host, "bar"));
  EXPECT_FALSE(HasSelectorForClassInShadow(host, "baz"));
  EXPECT_FALSE(HasSelectorForAttributeInShadow(host, "foo"));
  EXPECT_FALSE(HasSelectorForAttributeInShadow(host, "bar"));
  EXPECT_TRUE(HasSelectorForAttributeInShadow(host, "baz"));
}

TEST_F(ShadowDOMV0Test, FeatureSetSubtree) {
  LoadURL("about:blank");
  auto* host = GetDocument().CreateRawElement(html_names::kDivTag);
  host->CreateV0ShadowRootForTesting().setInnerHTML(R"HTML(
    <div>
      <div></div>
      <content select='*'></content>
      <div>
        <content select='div[foo=piyo]'></content>
      </div>
    </div>
  )HTML");
  EXPECT_FALSE(HasSelectorForIdInShadow(host, "foo"));
  EXPECT_FALSE(HasSelectorForClassInShadow(host, "foo"));
  EXPECT_TRUE(HasSelectorForAttributeInShadow(host, "foo"));
  EXPECT_FALSE(HasSelectorForAttributeInShadow(host, "piyo"));
}

TEST_F(ShadowDOMV0Test, FeatureSetMultipleShadowRoots) {
  LoadURL("about:blank");
  auto* host = GetDocument().CreateRawElement(html_names::kDivTag);
  auto& host_shadow = host->CreateV0ShadowRootForTesting();
  host_shadow.setInnerHTML("<content select='#foo'></content>");
  auto* child = GetDocument().CreateRawElement(html_names::kDivTag);
  auto& child_root = child->CreateV0ShadowRootForTesting();
  auto* child_content = GetDocument().CreateRawElement(html_names::kContentTag);
  child_content->setAttribute("select", "#bar");
  child_root.AppendChild(child_content);
  host_shadow.AppendChild(child);
  EXPECT_TRUE(HasSelectorForIdInShadow(host, "foo"));
  EXPECT_TRUE(HasSelectorForIdInShadow(host, "bar"));
  EXPECT_FALSE(HasSelectorForIdInShadow(host, "baz"));
  child_content->setAttribute("select", "#baz");
  EXPECT_TRUE(HasSelectorForIdInShadow(host, "foo"));
  EXPECT_FALSE(HasSelectorForIdInShadow(host, "bar"));
  EXPECT_TRUE(HasSelectorForIdInShadow(host, "baz"));
}

TEST_F(ShadowDOMV0Test, ReattachNonDistributedElements) {
  LoadURL("about:blank");

  auto* host = GetDocument().CreateRawElement(html_names::kDivTag);
  auto* inner_host = GetDocument().CreateRawElement(html_names::kDivTag);
  auto* span = GetDocument().CreateRawElement(html_names::kSpanTag);

  GetDocument().body()->appendChild(host);
  host->appendChild(inner_host);
  inner_host->appendChild(span);

  GetDocument().View()->UpdateAllLifecyclePhases(DocumentUpdateReason::kTest);

  host->CreateV0ShadowRootForTesting();
  inner_host->CreateV0ShadowRootForTesting();
  inner_host->SetInlineStyleProperty(CSSPropertyID::kDisplay,
                                     CSSValueID::kInlineBlock);
  span->SetInlineStyleProperty(CSSPropertyID::kDisplay, CSSValueID::kBlock);

  GetDocument().View()->UpdateAllLifecyclePhases(DocumentUpdateReason::kTest);

  EXPECT_FALSE(span->NeedsReattachLayoutTree());
}

TEST_F(ShadowDOMV0Test, DetachLayoutTreeOnShadowRootCreation) {
  LoadURL("about:blank");

  auto* host = GetDocument().CreateRawElement(html_names::kDivTag);
  host->SetInlineStyleProperty(CSSPropertyID::kDisplay, CSSValueID::kContents);
  auto* span = GetDocument().CreateRawElement(html_names::kSpanTag);
  host->appendChild(span);
  GetDocument().body()->appendChild(host);
  GetDocument().View()->UpdateAllLifecyclePhases(DocumentUpdateReason::kTest);

  EXPECT_TRUE(span->GetLayoutObject());

  host->CreateV0ShadowRootForTesting();

  EXPECT_FALSE(span->GetLayoutObject());
}

}  // namespace

}  // namespace blink