diff --git a/8-web-components/3-shadow-dom/article.md b/8-web-components/3-shadow-dom/article.md index fafc4754c9..fcf8a3ce4e 100644 --- a/8-web-components/3-shadow-dom/article.md +++ b/8-web-components/3-shadow-dom/article.md @@ -1,32 +1,32 @@ -# Shadow DOM +# 影子 DOM(Shadow DOM) -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. +Shadow DOM 为封装而生。它可以让一个组件拥有自己的「影子」DOM 树,这个 DOM 树不能在主文档中被任意访问,可能拥有局部样式规则,还有其他特性。 -## Built-in shadow DOM +## 内建 shadow DOM -Did you ever think how complex browser controls are created and styled? +你是否曾经思考过复杂的浏览器控件是如何被创建和添加样式的? -Such as ``: +比如 ``:

-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. +浏览器在内部使用 DOM/CSS 来绘制它们。这个 DOM 结构一般来说对我们是隐藏的,但我们可以在开发者工具里面看见它。比如,在 Chrome 里,我们需要打开「Show user agent shadow DOM」选项。 -Then `` looks like this: +然后 `` 看起来会像这样: ![](shadow-dom-range.png) -What you see under `#shadow-root` is called "shadow DOM". +你在 `#shadow-root` 下看到的就是被称为「shadow DOM」的东西。 -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. +我们不能使用一般的 JavaScript 调用或者选择器来获取内建 shadow DOM 元素。它们不是常规的子元素,而是一个强大的封装手段。 -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: +在上面的例子中,我们可以看到一个有用的属性 `pseudo`。这是一个因为历史原因而存在的属性,并不在标准中。我们可以使用它来给子元素加上 CSS 样式,像这样: ```html run autorun @@ -116,7 +116,7 @@ For example: ``` -1. The style from the document does not affect the shadow tree. -2. ...But the style from the inside works. -3. To get elements in shadow tree, we must query from inside the tree. +1. 文档里面的样式对 shadow tree 没有任何效果。 +2. ……但是内部的样式是有效的。 +3. 为了获取 shadow tree 内部的元素,我们可以从树的内部查询。 -## References +## 参考 -- DOM: -- Compatibility: -- 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`. +- DOM: +- 兼容性: +- Shadow DOM 在很多其他标准中被提到,比如:[DOM Parsing](https://w3c.github.io/DOM-Parsing/#the-innerhtml-mixin) 指定了 shadow root 有 `innerHTML`。 -## Summary +## 总结 -Shadow DOM is a way to create a component-local DOM. +Shadow DOM 是创建组件级别 DOM 的一种方法。 -1. `shadowRoot = elem.attachShadow({mode: open|closed})` -- creates shadow DOM for `elem`. If `mode="open"`, then it's accessible as `elem.shadowRoot` property. -2. We can populate `shadowRoot` using `innerHTML` or other DOM methods. +1. `shadowRoot = elem.attachShadow({mode: open|closed})` —— 为 `elem` 创建 shadow DOM。如果 `mode="open"`,那么它通过 `elem.shadowRoot` 属性被访问。 +2. 我们可以使用 `innerHTML` 或者其他 DOM 方法来扩展 `shadowRoot`。 -Shadow DOM elements: -- Have their own ids space, -- Invisible to JavaScript selectors from the main document, such as `querySelector`, -- Use styles only from the shadow tree, not from the main document. +Shadow DOM 元素: +- 有自己的 id 空间。 +- 对主文档的 JavaScript 选择器隐身,比如 `querySelector`。 +- 只使用 shadow tree 内部的样式,不使用主文档的样式。 -Shadow DOM, if exists, is rendered by the browser instead of so-called "light DOM" (regular children). In the chapter we'll see how to compose them. +Shadow DOM,如果存在的话,会被浏览器渲染而不是所谓的 「light DOM」(普通子元素)。在 章节中我们将会看到如何组织它们。