Skip to content

Commit 172130c

Browse files
committed
⏪ 撤销 addStyleSheet 的特性检测/回退
经讨论后认为现代浏览器(Chrome 73+ / Firefox 101+)都已支持 Constructable Stylesheets,main 分支用户基础已跨过临界点; 保留与原实现一致更利于代码维护。
1 parent ce43279 commit 172130c

2 files changed

Lines changed: 9 additions & 21 deletions

File tree

src/app/service/content/utils.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ describe("utils", () => {
458458
expect((sheet as any).cssText).toBe(css);
459459

460460
const adopted = (document as any).adoptedStyleSheets as CSSStyleSheet[];
461-
expect(adopted.includes(sheet as CSSStyleSheet)).toBe(true);
461+
expect(adopted.includes(sheet)).toBe(true);
462462
});
463463

464464
it("应该在已有样式表的基础上追加新的样式表", () => {
@@ -487,7 +487,7 @@ describe("utils", () => {
487487
expect((sheet as any).cssText).toBe("");
488488

489489
const adopted = (document as any).adoptedStyleSheets as CSSStyleSheet[];
490-
expect(adopted.includes(sheet as CSSStyleSheet)).toBe(true);
490+
expect(adopted.includes(sheet)).toBe(true);
491491
});
492492

493493
it("应该允许添加多个样式表", () => {

src/app/service/content/utils.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -222,26 +222,14 @@ export function addStyle(css: string): HTMLStyleElement {
222222
return document.documentElement.appendChild(dom);
223223
}
224224

225-
export function addStyleSheet(css: string): CSSStyleSheet | HTMLStyleElement {
225+
export function addStyleSheet(css: string): CSSStyleSheet {
226226
// see https://unarist.hatenablog.com/entry/2020/07/06/012540
227-
// 旧浏览器(如 Firefox 100 以下)不支持 Constructable Stylesheets / adoptedStyleSheets,回退到 <style> 注入
228-
if (
229-
typeof CSSStyleSheet === "function" &&
230-
typeof CSSStyleSheet.prototype?.replaceSync === "function" &&
231-
Array.isArray(document.adoptedStyleSheets)
232-
) {
233-
try {
234-
const sheet = new CSSStyleSheet();
235-
// it might return as Promise
236-
sheet.replaceSync(css);
237-
// adoptedStyleSheets is FrozenArray so it has to be re-assigned.
238-
document.adoptedStyleSheets = document.adoptedStyleSheets.concat(sheet);
239-
return sheet;
240-
} catch {
241-
// 个别环境下 new CSSStyleSheet() 仍可能抛出,落到 <style> fallback
242-
}
243-
}
244-
return addStyle(css);
227+
const sheet = new CSSStyleSheet();
228+
// it might return as Promise
229+
sheet.replaceSync(css);
230+
// adoptedStyleSheets is FrozenArray so it has to be re-assigned.
231+
document.adoptedStyleSheets = document.adoptedStyleSheets.concat(sheet);
232+
return sheet;
245233
}
246234

247235
export function metadataBlankOrTrue(metadata: SCMetadata, key: string): boolean {

0 commit comments

Comments
 (0)