|
1 | | -# Shadow DOM |
| 1 | +# سایه DOM |
2 | 2 |
|
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 محلی مربوط به خود. |
4 | 4 |
|
5 | | -## Built-in shadow DOM |
| 5 | +## سایه DOM های داخلی (Built-in) |
6 | 6 |
|
7 | | -Did you ever think how complex browser controls are created and styled? |
| 7 | +آیا تا به حال فکر کردهاید که چگونه کنترلرهای پیچیده مرورگر مثل المان زیر (ورودی محدود) ایجاد شده و style دهی میشوند؟ |
8 | 8 |
|
9 | | -Such as `<input type="range">`: |
| 9 | +`<input type="range">`: |
10 | 10 |
|
11 | 11 | <p> |
12 | 12 | <input type="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 این المانها از دیدگاه ما پنهان است ولی با استفاده از developer tools میتوان به آنها دسترسی پیدا کرد٬ برای مثال در chrome کافی است گزینه "Show user agent shadow DOM" را در Dev tools فعال کنید. |
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 | +چون المانهای Shadow DOM داخلی (built-in) از المانهای معمول 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: |
| 25 | +اگر در مثال بالا توجه کنید یک attribute به نام `pseudo` میبینید.`pseudo` یک ویژگی غیر استاندارد است که وجود آن دلایل تاریخی دارد اما با استفاده از آن میتوانیم المانهای زیرمجموعه را به شکل زیر style دهی کنیم. |
26 | 26 |
|
27 | 27 | ```html run autorun |
28 | 28 | <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 | + } |
33 | 33 | </style> |
34 | 34 |
|
35 | | -<input type="range"> |
| 35 | +<input type="range" /> |
36 | 36 | ``` |
37 | 37 |
|
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 استاندارد سازی شد تا توسعه دهندگان بتوانند المانهای کنترلی خود را بسازند. |
39 | 39 |
|
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) توضیحات کاملی از آن موجود است. |
41 | 41 |
|
42 | | -## Shadow tree |
| 42 | +## درخت Sadow |
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. درخت روشن -- یک زیر مجموعه معمولی از DOM است که از زیرمجموعه HTML ساخته شده است. تمام مثالهای که در فصول قبل دیدیم از نوع روشن بودند. |
| 47 | +2. درخت سایه -- یک زیرمجموعه پنهان از 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>. |
| 49 | +اگر المانی هر دو زیرمجموعه را داشت مرورگر فقط قسمت تاریک را نمایش خواهد داد. اما می توانیم نوعی ترکیب بندی بین درختان سایه و روشن ایجاد کنیم. جزیات بیشتر را در <info:slots-composition> خواهیم خواند. |
50 | 50 |
|
51 | | -Shadow tree can be used in Custom Elements to hide component internals and apply component-local styles. |
| 51 | +میتوان از درخت سایه برای پنهان سازی المانهای داخلی استفاده کرد و از استایل دهی محلی برای المان جدید استفاده کرد. |
52 | 52 |
|
53 | | -For example, this `<show-hello>` element hides its internal DOM in shadow tree: |
| 53 | +برای مثال`<show-hello>` المانهای داخلی خود را در سایه تاریک پنهان میکند و مقدار نمایش داده شده را به عنوان ویژگی دریافت میکند. |
54 | 54 |
|
55 | 55 | ```html run autorun height=60 |
56 | 56 | <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")} |
62 | 64 | </p>`; |
63 | | - } |
64 | | -}); |
| 65 | + } |
| 66 | + } |
| 67 | + ); |
65 | 68 | </script> |
66 | 69 |
|
67 | 70 | <show-hello name="John"></show-hello> |
68 | 71 | ``` |
69 | 72 |
|
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" خواهد بود. |
71 | 74 |
|
72 | 75 |  |
73 | 76 |
|
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) باشد یا یکی از المان های زیر |
75 | 83 |
|
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>` دیگر المانها مثل |
79 | 86 |
|
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) را مشخص میکند. و باید یکی از دو مقدار زیر را داشته باشد: |
82 | 88 |
|
83 | | - Any code is able to access the shadow tree of `elem`. |
84 | | -- `"closed"` -- `elem.shadowRoot` is always `null`. |
| 89 | +- `"open"` -- قابل دسترس باشد `elem.shadowRoot` که باعث میشود سایه تحت. |
85 | 90 |
|
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` را دارد. |
87 | 92 |
|
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` برای پر کردن آن استفاده کرد. |
89 | 98 |
|
90 | 99 | The element with a shadow root is called a "shadow tree host", and is available as the shadow root `host` property: |
91 | 100 |
|
| 101 | +به المانی که دارای shadow root باشد "shadow tree host" گفته میشود و با استفاده از ویژگی "host" قابل دسترسی است: |
| 102 | + |
92 | 103 | ```js |
93 | | -// assuming {mode: "open"}, otherwise elem.shadowRoot is null |
| 104 | +// است null برابر با elem.shadowRoot در غیر این صورت {"mode" : open} با فرض |
94 | 105 | alert(elem.shadowRoot.host === elem); // true |
95 | 106 | ``` |
96 | 107 |
|
97 | | -## Encapsulation |
| 108 | +## جداسازی (Encapsulation) |
98 | 109 |
|
99 | | -Shadow DOM is strongly delimited from the main document: |
| 110 | +دسترسی DOM سایه به طول کامل از سند اصلی گرفته شده است. |
100 | 111 |
|
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ها باید در درخت تاریک یکتا باشند. |
103 | 113 |
|
104 | | -For example: |
| 114 | +2. دام تاریک stylesheet مجزای مخصوص به خود را دارد. قوانین style خارجی (light DOM) در آن عمل نمیکنند. |
| 115 | + |
| 116 | +به مثال زیر توجه کنید: |
105 | 117 |
|
106 | 118 | ```html run untrusted height=40 |
107 | 119 | <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 | + } |
112 | 126 | </style> |
113 | 127 |
|
114 | 128 | <div id="elem"></div> |
115 | 129 |
|
116 | 130 | <script> |
117 | | - elem.attachShadow({mode: 'open'}); |
118 | | -*!* |
119 | | - // shadow tree has its own style (2) |
120 | | -*/!* |
| 131 | + elem.attachShadow({ mode: "open" }); |
| 132 | + /* |
| 133 | + * درخت تاریک استایل خود را دارد (۲) |
| 134 | + */ |
121 | 135 | 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> |
132 | 147 | ``` |
133 | 148 |
|
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ها را اجرا کنیم. |
137 | 152 |
|
138 | | -## References |
| 153 | +## منابع |
139 | 154 |
|
140 | 155 | - DOM: <https://dom.spec.whatwg.org/#shadow-trees> |
141 | 156 | - Compatibility: <https://caniuse.com/#feat=shadowdomv1> |
142 | 157 | - 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`. |
143 | 158 |
|
| 159 | +## خلاصه |
| 160 | + |
| 161 | +سایه DOM روشی است برای ایجاد یک المان محلی برای DOM. |
| 162 | + |
| 163 | +1. `shadowRoot = elem.attachShadow({mode: open|closed})` -- |
144 | 164 |
|
145 | | -## Summary |
| 165 | +یک دام سایه برای `elem` میسازد. اگر "mode="open باشد این المان با ویژگی `elem,shadowRoot` قابل دسترسی است. |
146 | 166 |
|
147 | | -Shadow DOM is a way to create a component-local DOM. |
| 167 | +2. با استفاده از ویژگی `innerHtml` یا ویژگی های دیگر DOM موجود در `shadowRoor` میتوان اجزای جدید به آن اضافه کرد. |
148 | 168 |
|
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: |
151 | 170 |
|
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 اصلی. |
156 | 174 |
|
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