Better widgets with Shadow DOM and CSS scoped
One fundamental problem you may encounter when you create widgets built out of HTML and JavaScript is that the DOM tree inside your widget isn't isolated from the rest of the page (aka encapsulation). It means that the document stylesheet can override your widget stylesheet (and back and forth). Same thing for the IDs or the JavaScript that can modify your application behaviour.
A solution exists to isolate parts of your application from others : Iframes. Currently I'm working on a requirejs plugin to sandbox your application with iframes (and privileges separation) and a framework using this solution. I'll probably write a post about that :)
But there is better and something I'm really excited about : Shadow DOM and CSS scoped. Both address the lack of encapsulation I explained.
What is Shadow DOM?
W3C says:
The shadow DOM allows multiple DOM trees (in addition to the document tree) to be composed into one larger tree when rendered...
To sum up, you can create shadow roots. A shadow roots associated to an element is called shadow host which can contains child nodes. Those child nodes aren't exposed in the traditional DOM tree and can't be modified!
With shadow host you can easily separate the content from the presentation and some other stuff. I invite you to read this tutorial.
What is CSS scoped?
WHATWG says:
The scoped attribute is a boolean attribute. If set, it indicates that the styles are intended just for the subtree rooted at the style element's parent element, as opposed to the whole Document.
It allows developers to apply styles to only the host element and descendant elements (it overrides the document stylesheet but not inline styles). Here's some useful links:
Scoped css : http://davidwalsh.name/scoped-css
Saving the day with scoped css : http://css-tricks.com/saving-the-day-with-scoped-css/
The scoped attribute : http://html5doctor.com/the-scoped-attribute/
I will update my css-bundle github repo with an example of scoped css.












