Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(517)

Side by Side Diff: pkg/shadow_dom/tool/README.md

Issue 22951003: Build shadow_dom package in dart/pkg (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Use ShadowDOM test file Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 ## Learn the tech
Jennifer Messerly 2013/08/15 21:01:37 why is this file under "tool"?
Jennifer Messerly 2013/08/15 21:30:44 doh, I'm totally confused. The README.md already e
2
3 ### Basic usage
4
5 var el = document.createElement('div');
6 var shadow = el.createShadowRoot();
7 shadow.innerHTML = '<content select="h1"></content>';
8
9 ### Shadow DOM subtrees
10
11 Shadow DOM allows a single node to express three subtrees: _light DOM_, _shadow DOM_, and _composed DOM_.
12
13 A component user supplies the light DOM; the node has a (hidden) shadow DOM; and the composed DOM is what is actually rendered in the browser. At render time, t he light DOM is merged with the shadow DOM to produce the composed DOM. For exam ple:
14
15 **Light DOM**
16
17 <my-custom-element>
18 <!-- everything in here is my-custom-element's light DOM -->
19 <q>Hello World</q>
20 </my-custom-element>
21
22 **Shadow DOM**
23
24 <!-- shadow-root is attached to my-custom-element, but is not a child -->
25 <shadow-root>
26 <!-- everything in here is my-custom-element's shadow DOM -->
27 <span>People say: <content></content></span>
28 </shadow-root>
29
30 **Composed (rendered) DOM**
31
32 <!-- rendered DOM -->
33 <my-custom-element>
34 <span>People say: <q>Hello World</q></span>
35 </my-custom-element>
36
37 The following is true about this example:
38
39 * The light DOM that belongs to `<my-custom-element>` is visible to the user as its normal subtree. It can expressed by `.childNodes`, `.children`, `.innerHTML` or any other property or method that gives you information about a node's subtr ee.
40 * Nodes in light DOM or shadow DOM express parent and sibling relationships that match their respective tree structures; the relationships that exist in the ren dered tree are not expressed anywhere in DOM.
41
42 So, while in the final rendered tree `<span>` is a child of `<my-custom-element> ` and the parent of `<q>`, interrogating those nodes will tell you that the `<sp an>` is a child of `<shadow-root>` and `<q>` is a child of `<my-custom-element>` , and that those two nodes are unrelated.
43
44 In this way, the user can manipulate light DOM or shadow DOM directly as regular DOM subtrees, and let the system take care of keeping the render tree synchroni zed.
45
46 ## Polyfill details
47
48 A polyfill to provide Shadow DOM functionality in browsers that don't
49 support it natively. This section explains how a proper (native) implementation
50 differs from our polyfill implementation.
51
52 ### Logical DOM
53
54 The light DOM and the shadow DOM is referred to as the logical DOM. This is the DOM that the developer interacts with. The composed DOM is what the browser sees and uses to render the pixels on the screen.
55
56 ### Wrappers
57
58 The polyfill is implemented using _wrappers_. A wrapper wraps the native DOM nod e in a wrapper node. The wrapper node looks and behaves identical to the native node (minus bugs and known limitations). For example:
59
60 var div = document.createElement('div');
61 div.innerHTML = '<b>Hello world</b>';
62 assert(div.firstChild instanceof HTMLElement);
63
64 But `div` is actually a wrapper of the element that the browser normally gives y ou. This wrapper just happen to have the same interface as the browser provided element.
65
66 It has an `innerHTML` setter that works just like the native `innerHTML` but it instead of working on the composed tree it works on the local DOM. When you chan ge the logical DOM tree like this it might cause the composed tree to need to be re-rendered. This does not happen immediately, but it is scheduled to happen la ter as needed.
67
68 The wrapper node also have a `firstChild` getter which once again works on the l ogical DOM.
69
70 `instanceof` still works because we have replaced the global `HTMLElement` const ructor with our custom one.
71
72 #### More Logical DOM
73
74 The `wrappers.Node` object keeps track of the logical (light as well as shadow, but not composed) DOM. Internally it has has the 5 fundamental Node pointers, `p arentNode`, `firstChild`, `lastChild`, `nextSibling` and `previousSibling`. When the DOM tree is manipulated these pointers are updated to always represent the logical tree. When the shadow DOM renderer needs to render the visual tree, thes e internal pointers are updated as needed.
75
76 #### Wrap all the objects!
77
78 The intent is to wrap all the DOM objects that interact with the DOM tree. For t his polyfill to be completely transparent we need to wrap a lot of APIs. Any met hod, accessor or constructor that takes or returns a Node or an object that indi rectly touches a node needs to be wrapped. As you can imagine there are a lot of these. At the moment we have done the most common ones but there are sure to be missing ones as soon as you try to use this with your code.
79
80 ### `wrap` and `unwrap`
81
82 There are bound to be cases where we haven't done the wrapping for you. In those cases you can use `wrap` to create a wrapper of a native object, or `unwrap` to get the underlying native object from a wrapper. These two functions are availa ble on the `ShadowDOMPolyfill` object.
83
84 #### Event Retargetting
85
86 An important aspect of the shadow DOM is that events are retargetted to never ex pose the shadow DOM to the light DOM. For example.
87
88 var div = document.createElement('div');
89 div.innerHTML = 'Click me';
90 var shadow = div.createShadowRoot();
91 shadow.innerHTML = '<b><content></content></b>';
92
93 If the user clicks on the `div` the real `target` of the click event is the `<b> ` element. But that element is not visible in the light DOM so the target is the refore retargetted to the `div` element itself. However, if there is an event li stener on the `<content>`, `<b>` or the shadow root, the target should be visibl e to the event listener.
94
95 Similar issues occur with `relatedTarget` in `mouseover` and `mouseout` events.
96
97 To support this kind of behavior the event dispatching in the browser has to be reimplemented by the polyfill.
98
99 #### Known issues
100
101 * CSS encapsulation is not implemented.
102 * `Object.prototype.toString` does not return the same string as for native obje cts.
103 * No live `NodeList`s. All node lists are snapshotted upon read.
104 * `document`, `window`, `document.body`, `document.head` and others are non conf igurable and cannot be overridden. We are trying to make these work as seamlessl y as possible but there will doubtlessly be cases where there will be problems; for those cases you can use `wrap` and `unwrap` to get unblocked.
105 * `onclick` and other `on*` attribute event handler do not wrap the event object as needed.
106 * Cross window/frame access is not implemented.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698