Skip to content

Feature request: Implement Document/ShadowRoot.adoptedStylesheets #37561

Closed

Description

@Jinwei-Bi

Describe the feature:
Since Document/ShadowRoot.adoptedStylesheets feature is incomplete, the constructed stylesheet can not be applied. [https://developer.mozilla.org/en-US/docs/Web/API/Document/adoptedStyleSheets]

To Reproduce:
I ran the following test with Servo and Chromium respectively. The result is attached as follow.

Test case
<!DOCTYPE` html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Shadow DOM with Global and Local Styles</title>
    <style id="global-styles">
        /* global style */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f8f8f8;
        }
    </style>
</head>
<body>
<div id="app"></div>

<script>
    // create a constructed stylesheet
    const globalStyleSheet = new CSSStyleSheet();
    globalStyleSheet.replaceSync(`
        /* global style */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f8f8f8;
        }

        .m-list {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            background-color: green;
        }

        .g-price {
            height: 30px;
            line-height: 1;
            padding: 0 10px;
            color: #ff6600;
        }

        .fixed {
            height: 50px;
            font-weight: bold;
            color: #000;
        }

        .g-height {
            height: 15px;
        }

        .g-pro {
            padding-left: 5px;
            margin-top: 5px;
        }
    `);

    // add the global style sheet to the document
    const styleElement = document.createElement('style');
    styleElement.adoptedStyleSheets = [globalStyleSheet];
    document.head.appendChild(styleElement);

    // create a custom-element
    class MyApp extends HTMLElement {
        constructor() {
            super();

            // create a Shadow DOM
            const shadow = this.attachShadow({ mode: 'open' });

            // createa a template
            const template = document.createElement('template');
            template.innerHTML = `
                <style>
                    .g-price {
                        height: 25px;
                        padding: 0 7px;
                    }

                    .fixed {
                        height: 41px;
                    }

                    .g-height {
                        height: 10px;
                    }

                    .g-pro {
                        padding-left: 2px;
                        margin-top: 2px;
                    }
                </style>
                <div class="m-list">
                    <div class="m-omit1 m-font24">肥瘦交融,久炖弥香</div>
                    <div class="m-bold m-font30 fixed m-omit2">阿根廷草饲牛腩1kg</div>
                    <div class="m-flex__center g-price">
                        <span class="m-cost m-bold m-font30">118</span>
                    </div>
                    <div class="g-pro"></div>
                    <div class="g-height"></div>
                </div>
            `;

            // attach template contents to Shadow DOM
            shadow.appendChild(template.content.cloneNode(true));

            // add global stylesheet to Shadow DOM
            shadow.adoptedStyleSheets = [globalStyleSheet];

            // debugging message
            console.log('Shadow DOM created and styles applied');
            console.log('Local styles:', template.content.querySelector('style').innerText);
            console.log('Global styles:', globalStyleSheet.cssRules);
        }
    }

    // define custom-element
    customElements.define('my-app', MyApp);

    // use custom-element
    const appContainer = document.getElementById('app');
    appContainer.innerHTML = '<my-app></my-app>';

    // debugging message
    console.log('Custom element <my-app> created and added to the DOM');
</script>
</body>
</html>

Image

Platform:
I produced this bug using a OpenHarmony device, but I guess it can be reproduced using other platforms either.

Activity

  1. added
    C-untriaged
    on Jun 19, 2025
  2. added
    A-content/dom
    I-wrong
    C-has-manual-testcase
    and removed
    C-untriaged
    on Jun 19, 2025
  3. xiaochengh commented on Jun 20, 2025

    @xiaochengh
    Contributor

    It's probably a feature request instead of a bug. The entire set of API related to constructed stylesheets needs support.

  4. changed the title [-]CSSStyleSheet::replaceSync does not work as expected when used with a shadow root[/-] [+]Feature request: Implement constructed stylesheet and related APIs[/+] on Jun 20, 2025
  5. added
    I-enhancement
    B-feature-tracking
    and removed
    I-wrong
    on Jun 20, 2025
  6. changed the title [-]Feature request: Implement constructed stylesheet and related APIs[/-] [+]Feature request: Implement Document/ShadowRoot.adoptedStylesheets[/+] on Jun 20, 2025
  7. xiaochengh commented on Jun 20, 2025

    @xiaochengh
    Contributor
  8. stevennovaryo commented on Jul 6, 2025

    @stevennovaryo
    Member

    Constructable stylesheet CSSStyleSheet, the object supporting this API, is tracked by #36162.

  9. stevennovaryo commented on Jul 8, 2025

    @stevennovaryo
    Member

    Possible implementations steps for this API is as follow:

    1. Allow documentorshadowroot::StyleSheetInDocument to have nullable owner, since constructable CSSStylesheet does not have owning node.
    2. Implement minimal AdoptedStylesheet workflow that will be push the stylesheet into the StyleSet of Document and ShadowRoot, without ObservableArray and with correct invalidations.
    3. Correctly handle stylesheet styleset ordering according to https://drafts.csswg.org/cssom/#css-style-sheet-collections.
    4. Make sure that constructed CSSStylesheet was accessible by Document or ShadowDom API. Since we are only storing the StyleSheetInDocument inside StyleSet we would need to reconstruct the CSSStylesheet.
    5. Implement observable array.
  10. stevennovaryo commented on Jul 8, 2025

    @stevennovaryo
    Member

    I will look at this more.

  11. self-assigned this
    on Jul 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-content/domB-feature-trackingC-has-manual-testcaseI-enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions