You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Shadow DOM serves for encapsulation. It allows a component to have its very own "shadow" DOM tree, that can't be accidentally accessed from the main document, may have local style rules, and more.
3
+
Shadow DOM 为封装而生。它可以让一个组件拥有自己的「影子」DOM 树,这个 DOM 树不能在主文档中被任意访问,可能拥有局部样式规则,还有其他特性。
4
4
5
-
## Built-in shadow DOM
5
+
## 内建 shadow DOM
6
6
7
-
Did you ever think how complex browser controls are created and styled?
7
+
你是否曾经思考过复杂的浏览器控件是如何被创建和添加样式的?
8
8
9
-
Such as `<input type="range">`:
9
+
比如 `<input type="range">`:
10
10
11
11
<p>
12
12
<inputtype="range">
13
13
</p>
14
14
15
-
The browser uses DOM/CSS internally to draw them. That DOM structure is normally hidden from us, but we can see it in developer tools. E.g. in Chrome, we need to enable in Dev Tools "Show user agent shadow DOM" option.
15
+
浏览器在内部使用 DOM/CSS 来绘制它们。这个 DOM 结构一般来说对我们是隐藏的,但我们可以在开发者工具里面看见它。比如,在 Chrome 里,我们需要打开「Show user agent shadow DOM」选项。
16
16
17
-
Then`<input type="range">`looks like this:
17
+
然后`<input type="range">`看起来会像这样:
18
18
19
19

20
20
21
-
What you see under `#shadow-root`is called "shadow DOM".
21
+
你在 `#shadow-root`下看到的就是被称为「shadow DOM」的东西。
22
22
23
-
We can't get built-in shadow DOM elements by regular JavaScript calls or selectors. These are not regular children, but a powerful encapsulation technique.
23
+
我们不能使用一般的 JavaScript 调用或者选择器来获取内建 shadow DOM 元素。它们不是常规的子元素,而是一个强大的封装手段。
24
24
25
-
In the example above, we can see a useful attribute `pseudo`. It's non-standard, exists for historical reasons. We can use it style subelements with CSS, like this:
Once again, `pseudo`is a non-standard attribute. Chronologically, browsers first started to experiment with internal DOM structures to implement controls, and then, after time, shadow DOM was standardized to allow us, developers, to do the similar thing.
38
+
重申一次,`pseudo`是一个非标准的属性。按照时间顺序来说,浏览器首先实验了使用内部 DOM 结构来实现控件,然后,在一段时间之后,shadow DOM 才被标准化来让我们,开发者们,做类似的事。
39
39
40
-
Further on, we'll use the modern shadow DOM standard, covered by [DOM spec](https://dom.spec.whatwg.org/#shadow-trees)other related specifications.
40
+
接下来,我们将要使用现代 shadow DOM 标准,它在 [DOM spec](https://dom.spec.whatwg.org/#shadow-trees)和其他相关标准中可以被找到。
41
41
42
42
## Shadow tree
43
43
44
-
A DOM element can have two types of DOM subtrees:
44
+
一个 DOM 元素可以有以下两类 DOM 子树:
45
45
46
-
1. Light tree -- a regular DOM subtree, made of HTML children. All subtrees that we've seen in previous chapters were "light".
47
-
2. Shadow tree -- a hidden DOM subtree, not reflected in HTML, hidden from prying eyes.
46
+
1. Light tree(光明树) —— 一个常规 DOM 子树,由 HTML 子元素组成。我们在之前章节看到的所有子树都是「光明的」。
47
+
2. Shadow tree(影子树) —— 一个隐藏的 DOM 子树,不在 HTML 中反映,无法被察觉。
48
48
49
-
If an element has both, then the browser renders only the shadow tree. But we can setup a kind of composition between shadow and light trees as well. We'll see the details later in the chapter <info:slots-composition>.
2.The `elem`must be either a custom element, or one of: "article", "aside", "blockquote", "body", "div", "footer", "h1..h6", "header", "main" "nav", "p", "section", or "span". Other elements, like `<img>`, can't host shadow tree.
Any code is able to access the shadow tree of `elem`.
84
-
-`"closed"` --`elem.shadowRoot`is always `null`.
83
+
任何代码都可以访问 `elem` 的 shadow tree。
84
+
-`「closed」` ——`elem.shadowRoot`永远是 `null`。
85
85
86
-
We can only access the shadow DOM by the reference returned by `attachShadow`(and probably hidden inside a class). Browser-native shadow trees, such as `<input type="range">`, are closed. There's no way to access them.
86
+
我们只能通过 `attachShadow`返回的指针来访问 shadow DOM(并且可能隐藏在一个 class 中)。浏览器原生的 shadow tree,比如 `<input type="range">`,是封闭的。没有任何方法可以访问它们。
87
87
88
-
The [shadow root](https://dom.spec.whatwg.org/#shadowroot), returned by `attachShadow`, is like an element: we can use `innerHTML`or DOM methods, such as `append`, to populate it.
88
+
`attachShadow` 返回的 [shadow root](https://dom.spec.whatwg.org/#shadowroot),和任何元素一样:我们可以使用 `innerHTML`或者 DOM 方法,比如 `append` 来扩展它。
89
89
90
-
The element with a shadow root is called a "shadow tree host", and is available as the shadow root `host`property:
90
+
我们称有 shadow root 的元素叫做「shadow tree host」,可以通过 shadow root 的 `host`属性访问:
91
91
92
92
```js
93
-
//assuming {mode: "open"}, otherwise elem.shadowRoot is null
93
+
//假设 {mode: "open"},否则 elem.shadowRoot 是 null
94
94
alert(elem.shadowRoot.host=== elem); // true
95
95
```
96
96
97
-
## Encapsulation
97
+
## 封装
98
98
99
-
Shadow DOM is strongly delimited from the main document:
99
+
Shadow DOM 被非常明显地和主文档分开:
100
100
101
-
1. Shadow DOM elements are not visible to `querySelector`from the light DOM. In particular, Shadow DOM elements may have ids that conflict with those in the light DOM. They must be unique only within the shadow tree.
102
-
2. Shadow DOM has own stylesheets. Style rules from the outer DOM don't get applied.
101
+
1. Shadow DOM 元素对于 light DOM 中的 `querySelector`不可见。实际上,Shadow DOM 中的元素可能与 light DOM 中某些元素的 id 冲突。这些元素必须在 shadow tree 中独一无二。
102
+
2. Shadow DOM 有自己的样式。外部样式规则在 shadow DOM 中不产生作用。
103
103
104
-
For example:
104
+
比如:
105
105
106
106
```html run untrusted height=40
107
107
<style>
108
108
*!*
109
-
/*document style won't apply to the shadow tree inside #elem (1) */
109
+
/*文档样式对 #elem 内的 shadow tree 无作用 (1) */
110
110
*/!*
111
111
p { color: red; }
112
112
</style>
@@ -116,42 +116,42 @@ For example:
116
116
<script>
117
117
elem.attachShadow({mode:'open'});
118
118
*!*
119
-
// shadow tree has its own style (2)
119
+
// shadow tree 有自己的样式 (2)
120
120
*/!*
121
121
elem.shadowRoot.innerHTML=`
122
122
<style>p { font-weight: bold; } </style>
123
123
<p>Hello, John!</p>
124
124
`;
125
125
126
126
*!*
127
-
// <p> is only visible from queries inside the shadow tree (3)
- Shadow DOM is mentioned in many other specifications, e.g. [DOM Parsing](https://w3c.github.io/DOM-Parsing/#the-innerhtml-mixin)specifies that shadow root has`innerHTML`.
140
+
- DOM:<https://dom.spec.whatwg.org/#shadow-trees>
141
+
-兼容性:<https://caniuse.com/#feat=shadowdomv1>
142
+
- Shadow DOM 在很多其他标准中被提到,比如:[DOM Parsing](https://w3c.github.io/DOM-Parsing/#the-innerhtml-mixin)指定了 shadow root 有`innerHTML`。
143
143
144
144
145
-
## Summary
145
+
## 总结
146
146
147
-
Shadow DOM is a way to create a component-local DOM.
147
+
Shadow DOM 是创建组件级别 DOM 的一种方法。
148
148
149
-
1.`shadowRoot = elem.attachShadow({mode: open|closed})`-- creates shadow DOM for `elem`. If `mode="open"`, then it's accessible as `elem.shadowRoot`property.
150
-
2.We can populate `shadowRoot` using `innerHTML`or other DOM methods.
-Invisible to JavaScript selectors from the main document, such as `querySelector`,
155
-
-Use styles only from the shadow tree, not from the main document.
152
+
Shadow DOM 元素:
153
+
-有自己的 id 空间。
154
+
-对主文档的 JavaScript 选择器隐身,比如 `querySelector`。
155
+
-只使用 shadow tree 内部的样式,不使用主文档的样式。
156
156
157
-
Shadow DOM, if exists, is rendered by the browser instead of so-called "light DOM" (regular children). In the chapter <info:slots-composition>we'll see how to compose them.
0 commit comments