npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

translate-shadowdom

v0.0.3

Published

Set of utilities to transform Shadow DOM v1 to v0

Downloads

13

Readme

Translate-ShadowDOM Build Status

Set of utilities to translate between Shadow DOM v0 and v1 (both ways). Usefull, when writing "hybrid" Web Components which are V1 ready, but run in V0 environment/polyfill; or when runnung legacy code in V1 environment.

Live Demo

Online Translator

Small sample

If you have Shadow DOM prepared for V1

<div class="shadow-host">
    <p slot="my-slot">I want to be distributed as in V1</p>
</div>
<template>
    <style>
        ::slotted(p){
            color: green;
        }
    </style>
    <h2>Shadow DOM prepared for V1</h2>
    <slot name="my-slot"></slot>
</template>

You can now use it in your current app/element running on V0 environment (like current webcomponentsjs polyfill)

let sRoot = myElement.createShadowRoot();
sRoot.innerHTML = TranslateShadowDOM.v1tov0.html(compositionString);
// or if you already have it parsed as document fragment
TranslateShadowDOM.v1tov0.fragment(documentfragment, true);

To get

<template>
    <style>
        ::content p{
            color: green;
        }
    </style>
    <h2>Shadow DOM prepared for V1</h2>
    <content name="my-slot" select="[slot='my-slot']"></content>
</template>

So, it would get rendered as you would expect in V1:

<div class="shadow-host">
    <h2>Shadow DOM prepared for V1</h2>
    <p slot="my-slot">I want to be distributed as in V1</p>
</div>

Related custom elements

Features

  • Translates strings,
  • Modifies DocumentFragments,
  • Preserves attributes,
  • Translates HTML, CSS and basic JS
  • works both ways between V0 and V1

Install

Install the component using Bower:

$ bower install translate-shadowdom --save

npm

$ npm install translate-shadowdom --save

Or download as ZIP.

API

V1 to V0

TranslateShadowDOM.v1tov0.html(String compositionString) : String

Translates the HTML given in string, replacing all <slot name="foo">smth</slot> with <content name="foo" select="[slot='foo']">smth</content>

TranslateShadowDOM.v1tov0.slot(HTMLElement slot) : ContentElement

Replaces given SlotElement (or UnknownElement <slot>) with ContentElement (<content>).

TranslateShadowDOM.v1tov0.fragment(HTMLElement | DocumentFragment root, Boolean withStyle, Boolean withScript) : HTMLElement | DocumentFragment

Replaces all SlotElements (or UnknownElements <slot>) with ContentElements (<content>) in given root. If withStyle is set to true, will also translate CSS selectors in enclosed <style> elements. If withScript is set to true, will also translate JS code in enclosed <script> elements.

TranslateShadowDOM.v1tov0.css(String styleString) : String

Replaces ::slotted(.foo) with ::content .foo in given string.

V0 to V1

TranslateShadowDOM.v0tov1.html(String compositionString) : String

Translates the HTML given in string, replacing all <content select="[slot='foo']">smth</content> with <slot name="foo">smth</slot>

TranslateShadowDOM.v0tov1.content(HTMLElement content) : SlotElement

Replaces given ContentElement (or UnknownElement <content>) with SlotElement (<slot>). It preserves all attributes except name. If selector cannot be directly translated to slot name, default slot would be created.

TranslateShadowDOM.v0tov1.fragment(HTMLElement | DocumentFragment root, Boolean withStyle, Boolean withScript) : HTMLElement | DocumentFragment

Replaces all ContentElements (or UnknownElements <content>) with SlotElements (<slot>) in given root. It preserves all attributes except name. If selector cannot be directly translated to slot name, default slot would be created. If withStyle is set to true, will also translate CSS selectors in enclosed <style> elements. If withScript is set to true, will also translate JS code in enclosed <script> elements.

TranslateShadowDOM.v0tov1.css(String styleString) : String

Replaces ::content .foo with ::slotted(.foo) in given string.

Test suite

  • local browser ./test/index.html
  • online

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Open corresponding issue if needed
  6. Submit a pull request :D

History

For detailed changelog, check Releases.

License

MIT