惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cloudbric
Cloudbric
T
Threat Research - Cisco Blogs
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog
P
Privacy & Cybersecurity Law Blog
H
Help Net Security
云风的 BLOG
云风的 BLOG
G
GRAHAM CLULEY
Spread Privacy
Spread Privacy
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
Project Zero
Project Zero
Engineering at Meta
Engineering at Meta
P
Privacy International News Feed
Blog — PlanetScale
Blog — PlanetScale
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
The Register - Security
The Register - Security
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Cisco Blogs
PCI Perspectives
PCI Perspectives
Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
A
About on SuperTechFans
W
WeLiveSecurity
GbyAI
GbyAI
V
Vulnerabilities – Threatpost
The GitHub Blog
The GitHub Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Check Point Blog
Y
Y Combinator Blog
月光博客
月光博客
Scott Helme
Scott Helme
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
U
Unit 42
G
Google Developers Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google Online Security Blog
Google Online Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 司徒正美

hayato

Chromium's DOM Deep Dive: Understanding Shadow DOM and Tree Structures - hayato Introducing the Task Scheduler in Chromium's Network Stack: A 0.3% Improvement in Loading Speed - hayato 出版のお知らせ 「普通の人が資産運用で99点をとる方法とその考え方」 - hayato Resource loading with Content Addressable Bundles - hayato 普通の人が資産運用で99点をとる方法とその考え方 - hayato Imperative Shadow DOM Distribution API - hayato Google にソフトウェアエンジニアとして入社して 10 年と 10 日がたちました - hayato Web のつくりかた: ソフトウェアエンジニアリングと雰囲気で理解する Web 標準とブラウザのつくりかた - hayato ブラウザのしくみ: データ構造とアルゴリズムと雰囲気で理解する DOM と Shadow DOM - hayato Rust が最強のプログラミング言語である証明 - hayato 六本木ではたらくソフトウェアエンジニアへのよくある質問とその答え (FAQ) (2015 - 2017) - hayato Simple timer application for the pomodoro technique - hayato Google の面接試験で出る (はずがない)「海賊と金貨」問題を空気を読まないでプログラムで解いてみる - hayato hayato.io (4topcoder) - hayato SRM432 - hayato SRM405 - hayato SRM386 - hayato SRM385 - hayato SRM384 - ゲーム - hayato SRM369 - hayato SRM368 - hayato SRM366 - typo 2 連発 - hayato Python - generator で素数生成 - hayato New Launguage - 新言語追加の予定? - hayato lxml で HTML スクレーピング - hayato SRM358 - Today is a Bad Day (-50). - hayato SRM349 FizzBuzz 問題が簡単すぎるそんなあなたへ。「赤いマーブル問題」。 - hayato Suffix Array - hayato google-code-prettify を導入してみる - hayato Subset の生成 - hayato 2006 年のまとめ - SRM332 - hayato Google Code Jam 2006 - Championship Round - hayato Blogger Beta に移行 - hayato Google Code Jam 2006 - Round1 に挑戦 - Generics と AutoBoxing - hayato Google Code Jam 2006 - Qualification Round 通過 - hayato Google Code Jam 2006 - Qualification Round に挑戦 - hayato Google Code Jam 2006 - Practice1 - hayato Google Code Jam 2006 - 開催決定 - hayato Stay tuned... Global Google Code Jam 2006 - 秋に開催? - hayato SRM308 - Hard DP - hayato SRM304 - 条件付確率 - hayato JavaWorld に寄稿しました - hayato 2006 TopCoder Open Final Round - hayato IBM Rational Software Development Forum Tokyo で講演します - hayato SRM298 - ひとふで書き - hayato SRM293 - ビンゴ - hayato 2006 TopCoder Open - Round1 撃墜 - hayato 2006 TopCoder Open - Odds 発表 - hayato 2006 TopCoder Open Qualification Round - hayato SRM291 - OutOfMemory - hayato デブサミ 2006 で講演します - hayato SRM287 - 気分は中学生?連立2元方程式 - hayato SRM281 - 2006 年はポイントゼロスタート - hayato SRM280 - Challenge 祭り - hayato SRM277 - 初賞金げっと - hayato マラソン・マッチ - hayato reStructuredText で blogger - hayato 2006 TopCoder Open - NSA がスポンサーに - hayato SRM276 - お買い物は楽しく DP で - hayato SRM273 - bit 演算は int で行われるのね - hayato SRM272 - Division1 - 4 度目の正直 - hayato JavaOne Tokyo で講演します - hayato SRM270 - Integer == Integer - hayato SRM268 - Division2 脱出 - hayato SRM266 - NASA, we have a problem... - hayato SRM265 - Division2 落ち... - hayato SRM262 - 初 Division1 は Typo に終わる - hayato SRM260 - TopCoder への挑戦のはじまり - hayato
What's New in Shadow DOM v1 (by examples) - hayato
2016-06-21 · via hayato

This document is my attempt to track the difference between Shadow DOM v0 and v1.

This is not a tutorial for Shadow DOM. Rather, this is my attempt to provide a guide for those who are already familiar with Shadow DOM v0 and want to migrate their components to v1. This guide should be considered work-in-progress. I will make my best efforts to maintain this guide.

  • Last update date: <2016-10-05 Wed>

Creating a shadow root

v0

Use Element.createShadowRoot().

let e = document.createElement("div");
let shadowRoot = e.createShadowRoot();

Use Element.attachShadow({ mode: 'open' }) for an open shadow root.

let e = document.createElement("div");
let shadowRoot = e.attachShadow({ mode: "open" });

Use Element.attachShadow({ mode: 'closed' }) for a closed shadow root.

let e = document.createElement("div");
let shadowRoot = e.attachShadow({ mode: "closed" });

A mode is mandatory in v1.

let e = document.createElement("div");
// let shadowRoot = e.attachShadow(); // Throws an exception because `mode` is not given.

Multiple Shadow Roots

v0

Supported.

let e = document.createElement("div");
let olderShadowRoot = e.createShadowRoot();
let youngerShadowRoot = e.createShadowRoot(); // It's okay. A shadow host can host more than one shadow roots.

Though multiple shadow roots were originally introduced to support an Inheritance Model for components, Blink has already deprecated this feature even in v0. Do not use multiple shadow roots.

v1

No longer supported.

let e = document.createElement("div");
let shadowRoot = e.attachShadow({ mode: "open" });
// let another = e.attachShadow({ mode: 'open' });  // Error.

A closed shadow root

v0

A shadow root is always open.

v1

v1 has a new kind of a shadow root, called closed.

The design goal of a closed mode is to disallow any access to a node in a closed shadow root from an outside world.

It is similar that a user's JavaScript can never access an inside of a <video> element in Google chrome. A <video> element is using a closed-mode shadow root in its implementation in Blink.

Open:

let e = document.createElement("div");
let shadowRoot = e.attachShadow({ mode: "open" });
console.assert(e.shadowRoot == shadowRoot); // It's okay. shadowHost.shadowRoot returns a shadow root if it is open.

Closed:

let e = document.createElement("div");
let shadowRoot = e.attachShadow({ mode: "closed" });
console.assert(e.shadowRoot == null); // shadowHost.shadowRoot does not return the shadow root if it is closed.

The following APIs are subject to this kind of constraints:

  • Element.shadowRoot
  • Element.assignedSlot
  • TextNode.assignedSlot
  • Event.composedPath()

To be precise, a concept of a unclosed node is used to decide its visibility between two nodes. A unclosed node is a binary relation between two nodes.

Shadow DOM is not a security mechanism. Please do not use Shadow DOM if you want a security. Nothing prevents Element.prototype.attachShadow from being hijacked.

Elements which can be a shadow host

v0

Every element can be a shadow host, theoretically.

let shadowRoot1 = document.createElement("div").createShadowRoot();
let shadowRoot2 = document.createElement("input").createShadowRoot(); // Should be okay.

This is not real. We never successfully define proper semantics for every elements. Thus, some of them do not work as intended. See this comment for the history. Blink has already banned most of the supports.

v1

A limited number of elements can be a shadow host.

let shadowRoot = document.createElement("div").attachShadow({ mode: "open" });
// document.createElement('input').attachShadow({ mode: 'open' });  // Error. `<input>` can not be a shadow host.

See the definition of the attachShadow for the complete list of such elements. Custom elements can be a shadow host.

Insertion Points (v0) vs Slots (v1)

v0

Use <content select=query> to select host's children. It can select host's children by CSS query selector.

<!-- Top level HTML -->
<my-host>
  <my-child id="c1" class="foo"></my-child>
  <my-child id="c2"></my-child>
  <my-child id="c3"></my-child>
</my-host>
<!-- <my-host>'s shadow tree -->
<div>
  <content id="i1" select=".foo"></content>
  <content id="i2" select="my-child"></content>
  <content id="i3"></content>
</div>

The result is:

Insertion pointDistributed nodes
#i1#c1
#i2#c2, #c3
#i3Empty

The v0 also had <shadow> insertion points, however, let me skip the explanation of <shadow> because multiple shadow roots are deprecated.

v1

Use <slot> to select host's children. It selects host's children by exact slot name matching.

<!-- Top level HTML -->
<my-host>
  <my-child id="c1" slot="slot1"></my-child>
  <my-child id="c2" slot="slot2"></my-child>
  <my-child id="c3"></my-child>
</my-host>
<!-- <my-host>'s shadow tree: -->
<div>
  <slot id="s1" name="slot1"></slot>
  <slot id="s2" name="slot2"></slot>
  <slot id="s3"></slot>
</div>

The result is:

SlotDistributed nodes
#s1#c1
#s2#c2
#s3 (also known as the "default slot")#c3

Re-distribution: Directly (v0) vs Indirectly by flattening (v1)

v0

<!-- Top level HTML -->
<my-host>
  <my-child id="c1" class="foo"></my-child>
  <my-child id="c2"></my-child>
  <my-child id="c3"></my-child>
</my-host>
<!-- <my-host>'s shadow tree -->
<my-splatoon>
  <content id="i1" select=".foo"></content>
  <my-child id="c4" class="foo"></my-child>
  <content id="i2" select="my-child"></content>
  <content id="i3"></content>
</my-splatoon>
<!-- <my-splatoon>'s shadow tree -->
<content id="i4" select="#c3"></content>
<content id="i5" select=".foo"></content>
<content id="i6"></content>

The result is:

Insertion pointDistributed nodes
#i1#c1
#i2#c2, #c3
#i3Empty
Insertion pointDistributed nodes
#i4#c3
#i5#c1, #c4
#i6#c2

v1

<!-- Top level HTML -->
<my-host>
  <my-child id="c1" slot="slot1"></my-child>
  <my-child id="c2" slot="slot2"></my-child>
  <my-child id="c3"></my-child>
</my-host>
<!-- <my-host>'s shadow tree -->
<my-splatoon>
  <slot id="s1" name="slot1" slot="slot4"></slot>
  <slot id="s2" name="slot2" slot="slot4"></slot>
  <my-child id="c4" slot="slot4"></my-child>
  <slot id="s3" slot="slot6"></slot>
</my-splatoon>
<!-- <my-splatoon>'s shadow tree -->
<slot id="s4" name="slot4"></slot>
<slot id="s5" name="slot5"></slot>
<slot id="s6" name="slot6"></slot>

The result is:

SlotDistributed nodes
#s1#c1
#s2#c2
#s3#c3
SlotDistributed nodes
#s4#c1, #c2, #c4
#s5empty
#s6#c3

You can find another complex example in the Shadow DOM specification.

Fallback contents

v0

No supports.

Blink has tried to support shadow as a function as a similar feature. That should have archived "a constructor call for a super class", however, we gave it up.

v1

Child nodes of <slot> can be used as fallback contents. A good analogy of this feature is "default value of function parameter" in a programming language.

The following example is borrowed from Blink's CL

<!-- Top-level HTML -->
<div id="host">
  <div id="child1" slot="slot2"></div>
</div>
<!-- #host's shadow tree -->
<slot name="slot1">
  <div id="fallback1"></div>
  <slot name="slot2">
    <div id="fallback2"></div>
  </slot>
</slot>
<slot name="slot3">
  <slot name="slot4">
    <div id="fallback3"></div>
  </slot>
</slot>

The result is

SlotAssigned nodesDistributed nodes
slot1empty#fallback1, #child1
slot2#child1#child1
slot3empty#fallback3
slot4empty#fallback3

Thus, the flat tree will be:

<div id="host">
  <div id="fallback1"></div>
  <div id="child1"></div>
  <div id="fallback3"></div>
</div>

Events to react the change of distributions

v0

No way.

v1

A v1 has a new kind of events, called slotchange. If a slot's distributed nodes changes as a result of DOM mutations, slotchange event will be fired at the end of a microtask.

HTML:

<!-- Top level HTML -->
<my-host>
  <my-child id="c1" slot="s1"></my-child>
</my-host>
<!-- <my-host>'s shadow tree -->
<slot id="i1" name="s1"></slot>

JavaScript:

slot_i1.addEventListener("slotchange", (e) => {
  console.log("fired");
});
const c2 = document.createElement("div");
my_host.appendChild(c2);
c2.setAttribute("slot", "s1");
// slotchange event will be fired on slot, '<slot id=i1 name=s1>', at the end of a micro task.

TODO(hayato): Explain this feature in-depth. For a while, see #issue 288 for the context.

Styling for distributed nodes

v0

Use ::content selector pseudo elements.

<!-- Top level HTML -->
<my-host>
  <my-child id="c1" class="foo"></my-child>
  <my-child id="c2"></my-child>
  <my-child id="c3"></my-child>
</my-host>
<!-- <my-host>'s shadow tree -->
<div>
  <content id="i1" select="my-child"></content>
</div>
<style>
  #i1::content .foo {
    color: red;
  }
</style>

#c1 becomes red.

v1

Use ::slotted (compound-selector) pseudo elements.

<!-- Top level HTML -->
<my-host>
  <my-child id="c1" slot="s1" class="foo"></my-child>
  <my-child id="c2" slot="s1"></my-child>
</my-host>
<!-- <my-host>'s shadow tree: -->
<div>
  <slot id="i1" name="s1"></slot>
</div>
<style>
  #i1::slotted(.foo) {
    color: red;
  }
</style>

#c1 becomes red.

While ::content can take any arbitrary selector, ::slotted can only take a compound selector (in the parenthesis). The reason of this restriction is to make a selector style-engine friendly, in terms of performance.

In v0, it is difficult to avoid a performance penalty caused by an arbitrary selector which crosses shadow boundaries.

Shadow piercing combinators

v0

Use /deep/ (zero-or-more shadow boundary crossing) and ::shadow (one level shadow boundary crossing).

These selectors were already deprecated in Blink. Do not use that.

v1

No alternative.

CSS Cascading order

v0

The spec has a bug and the implementation in Blink is broken. It's too late to fix it without breaking the Web.

v1

Clarified. In short: "A rule in an outer tree wins a rule in an inner tree".

Because /deep/ and ::shadow are unavailable in v1, only ::slotted is affected by the new rule, as of now.

See this document for the example.

Sequential Focus Navigation

v0

A document tree and a shadow tree are forming a scope of sequential focus navigation.

v1

In addition to v0, <slot> becomes a scope of sequential focus navigation.

See the comment in the spec issue for an example.

TODO(hayato): Explain the concept behind the scene and its behavior here.

DelegatesFocus

TODO(hayato): Explain this.

ActiveElement

TODO(hayato): Explain the difference. For a while, see webcomponents #358.

Events across shadow boundaries

v0

Events are propagating across shadow boundaries by default, except for a limited kinds of events. See the list.

v1

Events are scoped in a tree by default, except for some of UA UIEvents.

For user-made synthetic events, you can control the behavior by a composed flag.

HTML:

<!-- Top level HTML -->
<my-host></my-host>
<!-- <my-host>'s shadow tree -->
<div id="d1"></div>
</style>

JavaScript:

my_host.addEventListener("my-click1", (e) => {
  console.log("my-click1 is fired"); // This will not be called.
});
my_host.addEventListener("my-click2", (e) => {
  console.log("my-click2 is fired"); // This will be called.
});

d1.dispatchEvent(new Event("my-click1", { bubbles: true }));
d1.dispatchEvent(new Event("my-click2", { bubbles: true, composed: true }));

At #my-host, only an event listener for my-click2 is called.

Getting Event path

v0

Use Event.path, which is a property.

v1

Use Event.composedPath(), which is a function.

There is a small difference between them. After an event dispatching is done, Event.composedPath() returns an empty array, while Event.path does not.

Functions which are renamed

V0V1
insertionPoint.getDistributedNodes()slot.assignedNodes({flatten: true})
No equivalenceslot.assignedNodes()
Element.getDestinationInsertionPoints()Element.assignedSlot (The meaning is slightly different. It returns only the directly assigned slot.)

New utility functions in Node

These functions are just utility functions. Thus, v0 or v1 does not matter.

Questions?

If you find a typo, mistake or a question in this document, please file an issue here.

If you have a question about the Web Standard itself, please see the followings:

We have been upstream-ing Shadow DOM specification into the DOM Standard, the HTML Standard, or the CSS Scoping. The content of the Shadow DOM specification might not reflect the latest status. See webcomponents #377 for details.