Repository navigation
Closed
Labels
A-content/domInteracting with the DOM from web contentB-feature-trackingThis issue tracks a particular high-level featureC-has-manual-testcaseA minimal testcase exists that can be run manually.I-enhancementNo impact; the issue is a missing or proposed feature.
Description
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>Platform:
I produced this bug using a OpenHarmony device, but I guess it can be reproduced using other platforms either.
Metadata
Metadata
Assignees
Labels
A-content/domInteracting with the DOM from web contentB-feature-trackingThis issue tracks a particular high-level featureC-has-manual-testcaseA minimal testcase exists that can be run manually.I-enhancementNo impact; the issue is a missing or proposed feature.

Activity
xiaochengh commentedon Jun 20, 2025
It's probably a feature request instead of a bug. The entire set of API related to constructed stylesheets needs support.
[-]CSSStyleSheet::replaceSync does not work as expected when used with a shadow root[/-][+]Feature request: Implement constructed stylesheet and related APIs[/+][-]Feature request: Implement constructed stylesheet and related APIs[/-][+]Feature request: Implement Document/ShadowRoot.adoptedStylesheets[/+]xiaochengh commentedon Jun 20, 2025
replaceSyncis already implemented by https://github.com/servo/servo/pull/36586/filesstevennovaryo commentedon Jul 6, 2025
Constructable stylesheet
CSSStyleSheet, the object supporting this API, is tracked by #36162.stevennovaryo commentedon Jul 8, 2025
Possible implementations steps for this API is as follow:
documentorshadowroot::StyleSheetInDocumentto have nullable owner, since constructable CSSStylesheet does not have owning node.AdoptedStylesheetworkflow that will be push the stylesheet into theStyleSetofDocumentandShadowRoot, without ObservableArray and with correct invalidations.CSSStylesheetwas accessible byDocumentorShadowDomAPI. Since we are only storing theStyleSheetInDocumentinsideStyleSetwe would need to reconstruct theCSSStylesheet.stevennovaryo commentedon Jul 8, 2025
I will look at this more.
script: Implement `DocumentOrShadowDOM.adoptedStylesheet` with `Froze…