Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTMLLinkElement.prototype.use to handle adoptedStylesheets #22

Closed
mantou132 opened this issue Nov 27, 2023 · 2 comments
Closed

Add HTMLLinkElement.prototype.use to handle adoptedStylesheets #22

mantou132 opened this issue Nov 27, 2023 · 2 comments

Comments

@mantou132
Copy link

mantou132 commented Nov 27, 2023

e.g:

<template id="style1">
  <style>
    * {
      color: red;
    }
  </style>
</template>
<template id="style2">
  <style>
    * {
      background: blue;
    }
  </style>
</template>
<my-element>
  <template shadowrootmode="open">
    <p>MyElement</p>
    <link rel="stylesheet" use="style1"></link>
  </template>
</my-element>
<link rel="stylesheet" use="style1"></link>
<link rel="stylesheet" use="style2"></link>

On the parser side, the style1 stylesheet will be applied to document and my-element#shadowRoot, and the style2 stylesheet will be only applied to document.

Advantage:

  1. Backward compatibility
  2. Strong expression ability, corresponding to DocumnetOrShadowRoot.adoptedStylesheets
  3. No duplicate code will be generated during serialization

Serialization

For manual serialization of adoptedStylesheets while traversing the DOM, can control the id generation method. can also add argument option to getInnerHTML to specify the id generation method, e.g:

// Automatic generated
document.body.getInnerHTML({ convertAdoptedStylesheetsToTemplate: true })
// Specify generation method
document.body.getInnerHTML({ convertAdoptedStylesheetsToTemplate: Math.random })
@mantou132
Copy link
Author

Other solution: let the link element href attribute support fragment-only URLs:

<link rel="stylesheet" href="#style1" />

But this conflicts with the SVG use element and CSS: WICG/webcomponents#772 (comment)

@mfreed7
Copy link
Owner

mfreed7 commented Nov 27, 2023

Thanks for the suggestion, but this repo is probably not the best place to file it. Declarative shadow DOM is now a web standard, so new issues and suggestions should be raised on the WHATWG repo.

@mfreed7 mfreed7 closed this as completed Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants