WebComponents bring Shadow DOM with isolated CSS to some browsers. This script simulates CSS isolation for all modern browsers (IE9+, Firefox...).
CommonJS applications may need to include the
mutation-observer polyfill NPM package as well if they need to support browsers that don't suppport
npm install mutation-observer shadowstyles
Or downoad the built file and manually add tag:
Pass one argument: array, string, or element. String provides a selector to match new shadow root elements. Element provides a direct reference to a root element. Array can contain a combination of both.
// Example with selector string onlydocument;// Example with array containing both typesdocument;
Targeting simulated shadows
Shadowed elements are given a
shadow attribute that must be present in the
Detecting native support
When using in conjunction with the Platform.js Shadow DOM polyfill, it can be useful to know whether the shadow DOM is emulated. Check the boolean constant:
Note: I'm considering wrapping the polyfilled
automatically isolate the shadow CSS, also allowing
to skip style isolation. See
test/mockup/webcomponent.html for implementation
How it works
- Search for child DOM of elements to isolate.
- Find which rules apply to those elements and add a
:not(buffer-attr*="uniqueId")pseudo-class with a unique ID to each rule. Shadowed elements gain the buffer attribute containing the unique ID of each negated rule.
Originally, the plan of this project was to support styles using the native
::shadow pseudo-class syntax. Until the fifteenth commit, this worked, but
only if all the page's stylesheets were rewritten completely. By only
using selectors that the browser already supports, the script can skip parsing
all of your CSS itself and rely on the browser's interpretation, only making
small changes as needed. The final result is much smoother operation.
Still to do
- Handle nested shadow DOM correctly
Build / Run Tests
# Clone repository to current directory$ git clone https://github.com/numtel/shadowstyles.git$ cd shadowstyles$ npm install# Install Grunt Client$ npm install -g grunt-cli# Run default operation to build minified file$ grunt# Running tests locally requires Selenium jar in repo# http://selenium-release.storage.googleapis.com/index.html# For example, 2.45.0:$ wget http://selenium-release.storage.googleapis.com/2.45/selenium-server-standalone-2.45.0.jar# If not running 2.45.0, update filename in GruntFile.js# Test using grunt-nightwatch:$ grunt nightwatch:local# Or use default test command alias:$ npm test