Я пытаюсь использовать ShadowDomv1 (с https://github.com/webcomponents/webcomponentsjs и https://github.com/webcomponents/shadycss), но это не работает.
ShadowDom сам по себе работает, но css не инкапсулируется (как мы можем видеть с правилом h2
css).
Он работает так, как задумано в Chrome и Safari (но оба они изначально поддерживают ShadowDomv1).
Я что-то упускаю или это невозможно?
Вот jsbin: http://jsbin.com/maqohoxowu/edit?html,output
И код:
<script type="text/javascript" src="https://rawgithub.com/webcomponents/webcomponentsjs/master/webcomponents-hi-sd-ce.js"></script>
<style type="text/css">
h2 {
color: red;
border-bottom: 1px black dotted;
}
</style>
<h2>h2 red and dotted</h2>
<my-element>
</my-element>
<template id="myElementTemplate">
<style scope="my-element">
h2 {color: blue}
</style>
<div>
<h2>h2 blue and not dotted !</h2> <!-- Should not be dotted because of the encapsulation -->
</div>
</template>
<script type="text/javascript">
ShadyCSS.prepareTemplate(myElementTemplate, 'my-element');
class MyElement extends HTMLElement {
connectedCallback() {
ShadyCSS.styleElement(this);
if (!this.shadowRoot) {
this.attachShadow({mode: 'open'});
this.shadowRoot.appendChild(document.importNode(myElementTemplate.content, true));
}
ShadyCSS.styleElement(this);
}
}
customElements.define("my-element", MyElement);
</script>