Skip to content

Commit cdfdc60

Browse files
authored
Merge pull request #290 from pierangelomiceli/026_shadow
Shadow DOM and events
2 parents bb40348 + 6a74717 commit cdfdc60

File tree

1 file changed

+54
-54
lines changed

1 file changed

+54
-54
lines changed
Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
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+
Lo Shadow DOM serve all'incapsulamento e permette al componente di avere il proprio DOM "shadow", al quale il documento principale non può accedere nemmeno accidentalmente. Inoltre può avere regole di stile con scope locale e molto altro ancora.
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+
Avete mai pensato a come, dei controlli così complessi come quelli del browser, vengono creati e stilizzati?
88

9-
Such as `<input type="range">`:
9+
Prendiamo `<input type="range">` come esempio:
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+
Il browser usa la combinazione DOM e CSS internamente, per visualizzarli a schermo. Normalmente la struttura del DOM ci è invisibile, ma possiamo visualizzarla negli strumenti dello sviluppatore dei browser. Ad esempio, negli strumenti di sviluppo di Chrome, si può attivarne la visualizzazione nelle impostazioni generiche, l'opzione "Show user agent shadow DOM".
1616

17-
Then `<input type="range">` looks like this:
17+
Così facendo `<input type="range">` verrà mostrato in questo modo:
1818

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

21-
What you see under `#shadow-root` is called "shadow DOM".
21+
Quello che vediamo alla voce `#shadow-root` viene chiamato "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+
Non possiamo lavorare sugli elementi built-in dello Shadow DOM tramite normale chiamate o selettori CSS. Non sono nodi figli normali, ma una potente tecnica di incapsulamento.
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+
Nell'esempio precedente, possiamo notare l'attributo `pseudo` che è molto utile. Non è un attributo standard, esiste per ragioni storiche e possiamo usarlo per stilizzare i sottoelementi tramite CSS, in questo modo:
2626

2727
```html run autorun
2828
<style>
29-
/* make the slider track red */
29+
/* colora la traccia di rosso */
3030
input::-webkit-slider-runnable-track {
3131
background: red;
3232
}
@@ -35,22 +35,22 @@ input::-webkit-slider-runnable-track {
3535
<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+
Ripetiamolo ancora una volta, `pseudo` non è un attributo standard. Storicamente, i browser hanno cominciato a sperimentare con le strutture interne del DOM per implementare dei controlli, e con il passare del tempo, lo Shadow DOM è stato standardizzato per permettere a noi sviluppatori, di fare alla stessa maniera.
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+
Più avanti, utilizzeremo lo standard Shadow DOM moderno, nella sezione delle [specifiche DOM](https://dom.spec.whatwg.org/#shadow-trees) ed altre specifiche correlate.
4141

4242
## Shadow tree
4343

44-
A DOM element can have two types of DOM subtrees:
44+
Un elemento DOM può contenere due tipi di sottoalberi:
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 -- un normale sottoalbero DOM, fatto di figli HTML. Tutti i sottoalberi affrontati nei capitoli precedenti appartengono a questa categoria "light".
47+
2. Shadow tree -- un sottoalbero DOM nascosto, senza un elemento corrispondente nell'HTML, nascosto da "occhi indiscreti".
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+
Se un elemento li ha entrambi, il browser renderizza solo lo Shadow tree. Tuttavia possiamo impostare una sorta di composizione tra gli il light e lo Shadow tree. Vedremo in dettaglio l'argomento nell'apposita sezione <info:slots-composition>.
5050

51-
Shadow tree can be used in Custom Elements to hide component internals and apply component-local styles.
51+
Lo Shadow tree può essere usato all'interno dei Custom Elements per nascondere i componenti interni ed applicare gli stili localmente all'interno componente.
5252

53-
For example, this `<show-hello>` element hides its internal DOM in shadow tree:
53+
Per esempio, questo elemento, `<show-hello>` nasconde il suo DOM interno dell' 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+
Ecco come risulta il DOM, negli strumenti di sviluppo di Chrome, con il contenuto inserito in "#shadow-root":
7171

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

74-
First, the call to `elem.attachShadow({mode: …})` creates a shadow tree.
74+
Inizialmente, la chiamata a `elem.attachShadow({mode: …})` crea uno 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+
Ci sono due limitazioni:
77+
1. Possiamo creare solo una Shadow root per ogni elemento.
78+
2. L'elemento `elem` deve essere, o un elemento personalizzato, o uno tra questi elementi: "article", "aside", "blockquote", "body", "div", "footer", "h1..h6", "header", "main" "nav", "p", "section", o "span". Altri elementi, come ad esempio, `<img>`, non possono contenere uno Shadow tree.
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+
L'opzione `mode` imposta il livello di incapsulamento. Le opzioni possibili sono:
81+
- `"open"` -- la Shadow root è disponibile tramite `elem.shadowRoot`.
8282

83-
Any code is able to access the shadow tree of `elem`.
84-
- `"closed"` -- `elem.shadowRoot` is always `null`.
83+
Lo Shadow tree di `elem` è accessibile da qualunque punto del codice.
84+
- `"closed"` -- `elem.shadowRoot` è sempre `null`.
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+
Lo Shadow DOM è accessibile esclusivamente dal riferimento restituito da `attachShadow` (il quale, con ogni probabilità, sarà nascosto dentro una classe. Shadow tree nativi del browser, come `<input type="range">`, appartengono a questa categoria, e non c'è modo di accedervi.
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+
La [Shadow root](https://dom.spec.whatwg.org/#shadowroot), restituita con `attachShadow`, è come un elemento: possiamo usare `innerHTML` o i metodi DOM, come `append`, per popolarlo di elementi.
8989

90-
The element with a shadow root is called a "shadow tree host", and is available as the shadow root `host` property:
90+
L'elemento con una Shadow root viene invece chiamato "Shadow tree host", ed è disponibile attraverso la proprietà `host` della Shadow root:
9191

9292
```js
93-
// assuming {mode: "open"}, otherwise elem.shadowRoot is null
93+
// supponiamo di avere {mode: "open"}, altrimenti elem.shadowRoot sarebbe null
9494
alert(elem.shadowRoot.host === elem); // true
9595
```
9696

97-
## Encapsulation
97+
## Incapsulamento
9898

99-
Shadow DOM is strongly delimited from the main document:
99+
Vi è una separazione marcata tra lo Shadow DOM ed il documento principale:
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. Gli elementi dello Shadow DOM non sono rilevabili tramite `querySelector` del light DOM. In particolare, gli id degli elementi dello Shadow DOM, potrebbero andare in conflitto con quelli dell'albero del light DOM.
102+
2. Lo Shadow DOM ha i suoi fogli di stile e le regole di stile del DOM esterno non vengono applicate.
103103

104-
For example:
104+
Per esempio:
105105

106106
```html run untrusted height=40
107107
<style>
108108
*!*
109-
/* document style won't apply to the shadow tree inside #elem (1) */
109+
/* lo stile del documento non viene applicato allo Shadow tree contenuto in #elem (1) */
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+
// Lo Shadow tree possiede un proprio stile (2)
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+
// <p> e' visibile solamente da queries dentro lo Shadow tree (3)
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. Lo stile del documento non influenza lo Shadow tree.
135+
2. ...ma lo stile all'interno sì.
136+
3. Per avere gli elementi dentro lo Shadow tree, dobbiamo fare le query da dentro l'albero.
137137

138-
## References
138+
## Riferimenti
139139

140140
- DOM: <https://dom.spec.whatwg.org/#shadow-trees>
141-
- Compatibility: <https://caniuse.com/#feat=shadowdomv1>
142-
- 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`.
141+
- Compatibilità: <https://caniuse.com/#feat=shadowdomv1>
142+
- Lo Shadow DOM viene menzionato in molte altre specifiche, ad esempio in [DOM Parsing](https://w3c.github.io/DOM-Parsing/#the-innerhtml-mixin) specifica che la Shadow root possiede la proprietà `innerHTML`.
143143

144144

145-
## Summary
145+
## Riepilogo
146146

147-
Shadow DOM is a way to create a component-local DOM.
147+
Lo "Shadow DOM" è una modalità di creazione di un componente DOM locale.
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. Il comando `shadowRoot = elem.attachShadow({mode: open|closed})` crea uno Shadow DOM per `elem`. Se `mode="open"`, allora sarà possibile accedervi attraverso la proprietà `elem.shadowRoot`.
150+
2. Possiamo popolare `shadowRoot` usando `innerHTML` o altri metodi DOM.
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+
Gli elementi dello Shadow DOM:
153+
- Hanno la loro area per gli id
154+
- Sono invisibili ai selettori JavaScript dal documento principale, se cercati con `querySelector`
155+
- Usano gli stili dello Shadow tree, e non quelli del documento principale.
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+
Lo Shadow DOM, se esiste, viene renderizzato dal browser al posto del cosiddetto "light DOM" (normali nodi figli). Nel capitolo <info:slots-composition> vedremo come comporli.

0 commit comments

Comments
 (0)