Skip to content

Commit 17d18bf

Browse files
authored
Merge pull request #317 from alas1n/Shadow-Dom
Shadow Dom
2 parents e66bf84 + 80f09d0 commit 17d18bf

File tree

1 file changed

+54
-54
lines changed

1 file changed

+54
-54
lines changed

Diff for: 8-web-components/3-shadow-dom/article.md

+54-54
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
11
# Shadow 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 نهان(shadow) خود را داشته باشد، که به طور کامل از DOM سند اصلی جداسازی شده است، سند shadow DOM می‌تواند قوانین خاص خود را داشته باشد مثل style محلی مربوط به خود.
44

5-
## Built-in shadow DOM
5+
## Shadow 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 */
29+
/* رنگ ریل را به قرمز تغییر می‌دهد */
3030
input::-webkit-slider-runnable-track {
3131
background: red;
3232
}
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+
## درخت سایه (Shadow Tree)
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. light tree -- یک زیر مجموعه معمولی از DOM است که از زیرمجموعه HTML ساخته شده است. تمام مثال‌های که در فصول قبل دیدیم از نوع light بودند.
47+
2. shadow tree -- یک زیرمجموعه پنهان از 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+
اگر المانی هر دو زیرمجموعه را داشت مرورگر فقط قسمت shadow را نمایش خواهد داد. اما می توانیم نوعی ترکیب بندی بین shadow treeها و light ایجاد کنیم. جزیات بیشتر را در <info:slots-composition> خواهیم خواند.
5050

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

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

5555
```html run autorun height=60
5656
<script>
@@ -67,46 +67,46 @@ customElements.define('show-hello', class extends HTMLElement {
6767
<show-hello name="John"></show-hello>
6868
```
6969

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

7272
![](shadow-dom-say-hello.png)
7373

74-
First, the call to `elem.attachShadow({mode: …})` creates a shadow tree.
74+
در ابتدا `elem.attachShadow({mode: …})` یک shadow tree ایجاد می‌کند.
7575

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.
76+
ما در این جا ۲ محدودیت داریم:
77+
1. هر المان در صفحه فقط می‌تواند یک shadow داشته باشد.
78+
2. المان `elem` باید یک المان سفارشی سازی شده(custom) یا یکی از المان های "article", "aside", "blockquote", "body", "div", "footer", "h1..h6", "header", "main" "nav", "p", "section", "span" باشد. دیگر المان ها مثل `<img>` نمی‌توانند shadow داشته باشند.
7979

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`.
80+
گزینه `mode` سطح جداسازی (encapsulation) را مشخص می‌کند. و باید یکی از دو مقدار زیر را داشته باشد:
81+
- `"open"` -- قابل دسترس باشد `elem.shadowRoot` که باعث می‌شود shadow توسط.
8282

83-
Any code is able to access the shadow tree of `elem`.
84-
- `"closed"` -- `elem.shadowRoot` is always `null`.
83+
هر کدی قابلیت دسترسی به shadow tree المان `elem` را دارد.
84+
- `"closed"` -- است `null` همیشه `elem.shadowRoot`.
8585

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+
فقط با استفاده از مقدار(refrance) براگردانده شده از `attachShadow`(که احتمالا یک کلاس پنهان داخلی دارد) می‌توانیم به shadow DOM دسترسی پیدا کنیم. اما در مورد shadow treeهای بومی مرورگر مثل `<input type="range">` که بسته(`"closed"`) هستند٬ هیچ راهی برای دسترسی به این shadow treeها وجود ندارد.
8787

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+
با [shadow root](https://dom.spec.whatwg.org/#shadowroot) که خروجی `attachShadow` است می‌توان مثل یک المان معمولی برخورد کرد و از `innerHTML` یا `append` برای پر کردن آن استفاده کرد.
8989

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" گفته می‌شود و با استفاده از ویژگی "host" قابل دسترسی است:
9191

9292
```js
93-
// assuming {mode: "open"}, otherwise elem.shadowRoot is null
93+
// است null برابر با elem.shadowRoot در غیر این صورت {"mode" : open} با فرض
9494
alert(elem.shadowRoot.host === elem); // true
9595
```
9696

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

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

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 توسط `querySelector` های light DOM قابل شناسایی نیستنتد. به طور دقیق‌تر المان‌های داخلی shadow DOM ممکن است id هایی یکسانی با المان‌های light DOM داشته باشند اما این idها باید در shadow tree یکتا باشند.
102+
2. سند stylesheet, DOM shadow مجزای مخصوص به خود را دارد. قوانین style خارجی (light DOM) در آن عمل نمی‌کنند.
103103

104-
For example:
104+
به مثال زیر توجه کنید:
105105

106106
```html run untrusted height=40
107107
<style>
108108
*!*
109-
/* document style won't apply to the shadow tree inside #elem (1) */
109+
/* اجرا نمی‌شود (۱) #elem استایل سند بر shadow tree در
110110
*/!*
111111
p { color: red; }
112112
</style>
@@ -116,42 +116,42 @@ For example:
116116
<script>
117117
elem.attachShadow({mode: 'open'});
118118
*!*
119-
// shadow tree has its own style (2)
119+
// shadow tree استایل خود را دارد (۲)
120120
*/!*
121121
elem.shadowRoot.innerHTML = `
122122
<style> p { font-weight: bold; } </style>
123123
<p>Hello, John!</p>
124124
`;
125125
126126
*!*
127-
// <p> is only visible from queries inside the shadow tree (3)
127+
// (۳) ‌های داخل shadow tree قابل دسترسی است qury فقط از <p>
128128
*/!*
129129
alert(document.querySelectorAll('p').length); // 0
130130
alert(elem.shadowRoot.querySelectorAll('p').length); // 1
131131
</script>
132132
```
133133

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.
134+
1. استایل از سند اصلی هیچ تاثیری روی shadow tree ندارد.
135+
2. اما استایل از داخل به خوبی کار می‌کند.
136+
3. برای دریافت المان‌های داخل shadow tree باید از داخل shadow tree این query‌ها را اجرا کنیم.
137137

138-
## References
138+
## منابع
139139

140140
- DOM: <https://dom.spec.whatwg.org/#shadow-trees>
141141
- Compatibility: <https://caniuse.com/#feat=shadowdomv1>
142142
- 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`.
143143

144144

145-
## Summary
145+
## خلاصه
146146

147-
Shadow DOM is a way to create a component-local DOM.
147+
DOM روشی است برای ایجاد یک المان محلی برای shadow DOM.
148148

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.
149+
1. `shadowRoot = elem.attachShadow({mode: open|closed})` --یک shadow DOM برای `elem` می‌سازد. اگر "mode="open باشد این المان با ویژگی `elem.shadowRoot` قابل دسترسی است.
150+
2. با استفاده از ویژگی `innerHtml` یا ویژگی های دیگر DOM موجود در `shadowRoot` می‌توان اجزای جدید به آن اضافه کرد.
151151

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.
152+
المان‌هایshadow DOM:
153+
- داری فضای مجزای خود هستند.
154+
- از انتخابگر‌های جاوا اسکریپت موجد در DOM اصلی مثل `querySelector` پنهان هستند.
155+
- از استایل‌های موجود در shadow tree خود استفاده می‌کنند و نه از استایل موجود در DOM اصلی.
156156

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

0 commit comments

Comments
 (0)