Skip to content

fix(wujie-core): proxy document.adoptedStyleSheets to shadowRoot - #1062

Merged
yiludege merged 3 commits into
Tencent:masterfrom
Gaubee:feat/adopted-stylesheets-support
Nov 14, 2026
Merged

yiludege merged 3 commits into
Tencent:masterfrom
Gaubee:feat/adopted-stylesheets-support

Conversation

@Gaubee

@Gaubee Gaubee commented Jan 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

在非降级模式下,将 document.adoptedStyleSheets 代理到 shadowRoot.adoptedStyleSheets。

fix #209

Problem

当子应用使用基于 adoptedStyleSheets 的 WebComponent(如 IconPark)时,会报错:

Uncaught DOMException: Failed to set the 'adoptedStyleSheets' property on 'ShadowRoot': 
Sharing constructed stylesheets in multiple documents is not allowed

原因是 wujie 将子应用的 UI 渲染在 shadowRoot 中,但 document.adoptedStyleSheets 仍然指向 iframe 的 document,导致 constructed stylesheets 无法在不同 document 之间共享。

Solution

在 WujieApp.connectedCallback 中(shadowRoot 创建之后),将 document.adoptedStyleSheets 的 getter/setter 代理到 shadowRoot.adoptedStyleSheets:

  • 代理 Document.prototype.adoptedStyleSheets
  • 代理 document.adoptedStyleSheets 实例属性

Changes

  • packages/wujie-core/src/shadow.ts: 在 connectedCallback 中添加 adoptedStyleSheets 代理逻辑
  • packages/wujie-core/__test__/integration/adoptedStyleSheets.test.ts: 添加测试用例

Testing

已在实际项目中验证:

  1. 基于 adoptedStyleSheets 的样式控制器正常工作
  2. 动态添加/移除样式表正确反映在 shadowRoot 中
  3. 不影响降级模式(degrade mode)的行为

Proxy document.adoptedStyleSheets to shadowRoot.adoptedStyleSheets in non-degrade mode.
This fixes the issue where constructed stylesheets cannot be shared between
the iframe document and shadowRoot, causing WebComponents using adoptedStyleSheets
(like IconPark) to fail with 'Sharing constructed stylesheets in multiple documents
is not allowed' error.

fix Tencent#209
@Gaubee
Gaubee force-pushed the feat/adopted-stylesheets-support branch from fc894bd to b57f302 Compare January 21, 2026 05:56
@Gaubee Gaubee changed the title feat(wujie-core): support document.adoptedStyleSheets proxy to shadowRoot fix(wujie-core): proxy document.adoptedStyleSheets to shadowRoot Jan 21, 2026
yiludege and others added 2 commits November 14, 2026 11:55
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>

# Conflicts:
#	packages/wujie-core/src/shadow.ts
@yiludege

Copy link
Copy Markdown
Collaborator

感谢提交!在浏览器里实测确认了这个问题:#771 之后子应用的 CSSStyleSheet 取的是主应用的构造函数,样式表属于主文档;子应用再赋给 iframe 自己的 document.adoptedStyleSheets 时会抛 NotAllowedError。这个 PR 把它转到 shadowRoot 上,直接写 document 和写 window.document 两种方式都生效,追加写法下前面的样式也会保留,主应用的 document.adoptedStyleSheets 不受影响。降级模式下的行为和 master 一致,没有变化。

我在这个分支上追加了两个提交:

  • 6d35356:两处 defineProperty 失败时由静默忽略改为输出 warn,方便排查;
  • ee3d14c:合入最新 master,解决 shadow.ts import 行的冲突。

core 单测和 tsc 均已通过,集成测试等 CI 运行。

@yiludege
yiludege merged commit 869da7a into Tencent:master Nov 14, 2026
3 checks passed
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

Successfully merging this pull request may close these issues.

Iconpark WebComponent 方式再子应用使用 Sharing constructed stylesheets in multiple documents is not allowed

2 participants