Skip to content

Commit c282b74

Browse files
committed
add Shadow Dom
1 parent 998eb16 commit c282b74

1 file changed

Lines changed: 97 additions & 79 deletions

File tree

Lines changed: 97 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,175 @@
1-
# Shadow DOM
1+
# سایه‌ DOM
22

3-
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 جداسایزی یا (کپسوله کردن) است. Shadow DOM به یک المنت اجازه می دهد تا سند DOM "سایه‌ی" خود را داشته باشد، که به طور کامل از سند DOM اصلی قابل دسترسی نیست،سند Shadow DOM می‌تواند قوانین خاص خود را داشته باشد مثل style محلی مربوط به خود.
44

5-
## Built-in shadow DOM
5+
## سایه DOM های داخلی (Built-in)
66

7-
Did you ever think how complex browser controls are created and styled?
7+
آیا تا به حال فکر کرده‌اید که چگونه کنترلرهای پیچیده مرورگر مثل المان زیر (ورودی محدود) ایجاد شده و style دهی می‌شوند؟
88

9-
Such as `<input type="range">`:
9+
`<input type="range">`:
1010

1111
<p>
1212
<input type="range">
1313
</p>
1414

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 این المان‌ها از دیدگاه ما پنهان است ولی با استفاده از developer tools می‌توان به آن‌ها دسترسی پیدا کرد٬ برای مثال در chrome کافی‌ است گزینه "Show user agent shadow DOM" را در Dev tools فعال کنید.
1616

17-
Then `<input type="range">` looks like this:
17+
برای مثال `<"input type="range>` به شکل زیر است.
1818

1919
![](shadow-dom-range.png)
2020

21-
What you see under `#shadow-root` is called "shadow DOM".
21+
همه اجزای زیرمجموعه‌ی `#shadow-root` را می‌توان "shadow DOM" نامید.
2222

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+
چون المان‌های Shadow DOM داخلی (built-in) از المان‌های معمول DOM جدا سازی شده‌اند نمی‌توان با استفاده از انتخابگراهای جاوااسکریپت مرورگر به آنها دسترسی پیدا کرد.
2424

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:
25+
اگر در مثال بالا توجه کنید یک attribute به نام `pseudo` می‌بینید.`pseudo` یک ویژگی غیر استاندارد است که وجود آن دلایل تاریخی دارد اما با استفاده از آن می‌توانیم المان‌های زیرمجموعه را به شکل زیر style دهی کنیم.
2626

2727
```html run autorun
2828
<style>
29-
/* make the slider track red */
30-
input::-webkit-slider-runnable-track {
31-
background: red;
32-
}
29+
/* make the slider track red */
30+
input::-webkit-slider-runnable-track {
31+
background: red;
32+
}
3333
</style>
3434

35-
<input type="range">
35+
<input type="range" />
3636
```
3737

38-
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 استاندارد سازی شد تا توسعه دهندگان بتوانند المان‌های کنترلی خود را بسازند.
3939

40-
Further on, we'll use the modern shadow DOM standard, covered by [DOM spec](https://dom.spec.whatwg.org/#shadow-trees) and other related specifications.
40+
در ادامه از shadow DOM استاندارد استفاده خواهیم کرد که در [DOM spec](https://dom.spec.whatwg.org/#shadow-trees) توضیحات کاملی از آن موجود است.
4141

42-
## Shadow tree
42+
## درخت Sadow
4343

44-
A DOM element can have two types of DOM subtrees:
44+
یک المان DOM می‌تواند دو نوع زیرمجموعه DOM داشته باشد:
4545

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. درخت روشن -- یک زیر مجموعه معمولی از DOM است که از زیرمجموعه HTML ساخته شده است. تمام مثال‌های که در فصول قبل دیدیم از نوع روشن بودند.
47+
2. درخت سایه -- یک زیرمجموعه پنهان از DOM است که در HTML نمایش داده نمی‌شود و از چشم مخفی است.
4848

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>.
49+
اگر المانی هر دو زیرمجموعه را داشت مرورگر فقط قسمت تاریک را نمایش خواهد داد. اما می توانیم نوعی ترکیب بندی بین درختان سایه و روشن ایجاد کنیم. جزیات بیشتر را در <info:slots-composition> خواهیم خواند.
5050

51-
Shadow tree can be used in Custom Elements to hide component internals and apply component-local styles.
51+
می‌توان از درخت سایه برای پنهان سازی المان‌های داخلی استفاده کرد و از استایل دهی محلی برای المان جدید استفاده کرد.
5252

53-
For example, this `<show-hello>` element hides its internal DOM in shadow tree:
53+
برای مثال`<show-hello>` المان‌های داخلی خود را در سایه تاریک پنهان می‌کند و مقدار نمایش داده شده را به عنوان ویژگی دریافت می‌کند.
5454

5555
```html run autorun height=60
5656
<script>
57-
customElements.define('show-hello', class extends HTMLElement {
58-
connectedCallback() {
59-
const shadow = this.attachShadow({mode: 'open'});
60-
shadow.innerHTML = `<p>
61-
Hello, ${this.getAttribute('name')}
57+
customElements.define(
58+
"show-hello",
59+
class extends HTMLElement {
60+
connectedCallback() {
61+
const shadow = this.attachShadow({ mode: "open" });
62+
shadow.innerHTML = `<p>
63+
Hello, ${this.getAttribute("name")}
6264
</p>`;
63-
}
64-
});
65+
}
66+
}
67+
);
6568
</script>
6669

6770
<show-hello name="John"></show-hello>
6871
```
6972

70-
That's how the resulting DOM looks in Chrome dev tools, all the content is under "#shadow-root":
73+
خروجی DOM بالا در Chrome dev tool به شکل زیر نشان داده می‌شود و تمام اجزا زیر مجموعه "#shadow-root" خواهد بود.
7174

7275
![](shadow-dom-say-hello.png)
7376

74-
First, the call to `elem.attachShadow({mode: …})` creates a shadow tree.
77+
در ابتدا `elem.attachShadow({mode: …})` یک درخت سایه ایجاد می‌کند.
78+
79+
ما در این جا ۲ محدودیت داریم:
80+
81+
1. هر المان در صفحه فقط می‌تواند یک سایه داشته باشد.
82+
2. المان `elem` باید یک المان سفارشی سازی شده(custom) باشد یا یکی از المان های زیر
7583

76-
There are two limitations:
77-
1. We can create only one shadow root per element.
78-
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.
84+
"article", "aside", "blockquote", "body", "div", "footer", "h1..h6", "header", "main" "nav", "p", "section", "span"
85+
.نمی‌توانند دارای سایه باشند `<img>` دیگر المان‌ها مثل
7986

80-
The `mode` option sets the encapsulation level. It must have any of two values:
81-
- `"open"` -- the shadow root is available as `elem.shadowRoot`.
87+
گزینه `mode` سطح جداسازی (encapsulation) را مشخص می‌کند. و باید یکی از دو مقدار زیر را داشته باشد:
8288

83-
Any code is able to access the shadow tree of `elem`.
84-
- `"closed"` -- `elem.shadowRoot` is always `null`.
89+
- `"open"` -- قابل دسترس باشد `elem.shadowRoot` که باعث می‌شود سایه تحت.
8590

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.
91+
هر کدی قابلیت دسترسی به درخت سایه المان `elem` را دارد.
8792

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.
93+
- `"closed"` -- است `null` همیشه `elem.shadowRoot`.
94+
95+
فقط با استفاده از مقدار(refrance) براگردانده شده از `attachShadow`(که احتمالا یک کلاس پنهان داخلی دارد) می‌توانیم به DOM تاریک دسترسی پیدا کنیم. اما در مورد درخت‌های تاریک بومی مرورگر مثل `<input type="range">` که بسته(`"closed"`) هستند٬ هیچ راهی برای دسترسی به این درخت‌های تاریک وجود ندارد.
96+
97+
با [shadow root](https://dom.spec.whatwg.org/#shadowroot) که خروجی `attachShadow` است می‌توان مثل یک المان معمولی برخورد کرد و از `innerHTML` یا `append` برای پر کردن آن استفاده کرد.
8998

9099
The element with a shadow root is called a "shadow tree host", and is available as the shadow root `host` property:
91100

101+
به المانی که دارای shadow root باشد "shadow tree host" گفته می‌شود و با استفاده از ویژگی "host" قابل دسترسی است:
102+
92103
```js
93-
// assuming {mode: "open"}, otherwise elem.shadowRoot is null
104+
// است null برابر با elem.shadowRoot در غیر این صورت {"mode" : open} با فرض
94105
alert(elem.shadowRoot.host === elem); // true
95106
```
96107

97-
## Encapsulation
108+
## جداسازی (Encapsulation)
98109

99-
Shadow DOM is strongly delimited from the main document:
110+
دسترسی DOM سایه به طول کامل از سند اصلی گرفته شده است.
100111

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.
112+
1. المان‌های DOM سایه توسط `querySelector` های DOM روشن قابل شناسایی نیستنتد. به طور دقیق‌تر المان‌های داخلی DOM سایه ممکن است id هایی یکسانی با المان‌های DOM روشن داشته باشند اما این idها باید در درخت تاریک یکتا باشند.
103113

104-
For example:
114+
2. دام تاریک stylesheet مجزای مخصوص به خود را دارد. قوانین style خارجی (light DOM) در آن عمل نمی‌کنند.
115+
116+
به مثال زیر توجه کنید:
105117

106118
```html run untrusted height=40
107119
<style>
108-
*!*
109-
/* document style won't apply to the shadow tree inside #elem (1) */
110-
*/!*
111-
p { color: red; }
120+
/*
121+
* اجرا نمی‌شود (۱) #elem استایل سند بر درخت تاریک در
122+
*/
123+
p {
124+
color: red;
125+
}
112126
</style>
113127

114128
<div id="elem"></div>
115129

116130
<script>
117-
elem.attachShadow({mode: 'open'});
118-
*!*
119-
// shadow tree has its own style (2)
120-
*/!*
131+
elem.attachShadow({ mode: "open" });
132+
/*
133+
* درخت تاریک استایل خود را دارد (۲)
134+
*/
121135
elem.shadowRoot.innerHTML = `
122-
<style> p { font-weight: bold; } </style>
123-
<p>Hello, John!</p>
124-
`;
125-
126-
*!*
127-
// <p> is only visible from queries inside the shadow tree (3)
128-
*/!*
129-
alert(document.querySelectorAll('p').length); // 0
130-
alert(elem.shadowRoot.querySelectorAll('p').length); // 1
131-
</script>
136+
<style> p { font-weight: bold; } </style>
137+
<p>Hello, John!</p>
138+
`;
139+
140+
/*
141+
* (۳) ‌های داخل درخت تاریک قابل دسترسی است qury فقط از
142+
*/
143+
144+
alert(document.querySelectorAll("p").length); // 0
145+
alert(elem.shadowRoot.querySelectorAll("p").length); // 1
146+
</script>
132147
```
133148

134-
1. The style from the document does not affect the shadow tree.
135-
2. ...But the style from the inside works.
136-
3. To get elements in shadow tree, we must query from inside the tree.
149+
1. استایل از سند اصلی هیچ تاثیری روی درخت تاریک ندارد.
150+
2. اما استایل از داخل به خوبی کار می‌کند.
151+
3. برای دریافت المان‌های داخل درخت تاریک باید از داخل درخت تاریک این query‌ها را اجرا کنیم.
137152

138-
## References
153+
## منابع
139154

140155
- DOM: <https://dom.spec.whatwg.org/#shadow-trees>
141156
- Compatibility: <https://caniuse.com/#feat=shadowdomv1>
142157
- 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`.
143158

159+
## خلاصه
160+
161+
سایه DOM روشی است برای ایجاد یک المان محلی برای DOM.
162+
163+
1. `shadowRoot = elem.attachShadow({mode: open|closed})` --
144164

145-
## Summary
165+
یک دام سایه برای `elem` می‌سازد. اگر "mode="open باشد این المان با ویژگی `elem,shadowRoot` قابل دسترسی است.
146166

147-
Shadow DOM is a way to create a component-local DOM.
167+
2. با استفاده از ویژگی `innerHtml` یا ویژگی های دیگر DOM موجود در `shadowRoor` می‌توان اجزای جدید به آن اضافه کرد.
148168

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.
169+
المان‌های سایه DOM:
151170

152-
Shadow DOM elements:
153-
- Have their own ids space,
154-
- Invisible to JavaScript selectors from the main document, such as `querySelector`,
155-
- Use styles only from the shadow tree, not from the main document.
171+
- داری فضای مجزای خود هستند.
172+
- از انتخابگر‌های جاوا اسکریپت موجد در DOM اصلی مثل `querySelector` پنهان هستند.
173+
- از استایل‌های موجود در درخت تاریک خود استفاده می‌کنند و نه از استایل موجود در DOM اصلی.
156174

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.
175+
اگر المانی در صفحه دارای DOM سایه بود، مرورگر به صورت پیشفرض المان‌های موجود در درخت تاریک را نمایش می‌دهد و از المان‌های DOM روشن یا همان زیرمجموعه‌های HTML معمول صرف نظر می‌کند. در فصل <info:slots-composition> در مورد ترکیب این ۲ بیشتر مطالعه می‌کنیم.

0 commit comments

Comments
 (0)