Merge pull request #4619 from Tyriar/tyriar/4611_2
Avoid using global document, add copyright
This commit is contained in:
@@ -1,15 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2023 The xterm.js authors. All rights reserved.
|
||||||
|
* @license MIT
|
||||||
|
*/
|
||||||
|
|
||||||
export interface IStyleSheet {
|
export interface IStyleSheet {
|
||||||
dispose: () => void;
|
dispose: () => void;
|
||||||
setCss: (value: string) => void;
|
setCss: (value: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const createCssStyleSheet = (): IStyleSheet => {
|
const createCssStyleSheet = (doc: Document): IStyleSheet => {
|
||||||
const sheet = new CSSStyleSheet();
|
const sheet = new CSSStyleSheet();
|
||||||
document.adoptedStyleSheets.push(sheet);
|
doc.adoptedStyleSheets.push(sheet);
|
||||||
return {
|
return {
|
||||||
dispose() {
|
dispose() {
|
||||||
const index = document.adoptedStyleSheets.indexOf(sheet);
|
const index = doc.adoptedStyleSheets.indexOf(sheet);
|
||||||
document.adoptedStyleSheets.splice(index, 1);
|
doc.adoptedStyleSheets.splice(index, 1);
|
||||||
},
|
},
|
||||||
setCss(css) {
|
setCss(css) {
|
||||||
sheet.replaceSync(css);
|
sheet.replaceSync(css);
|
||||||
@@ -18,7 +23,8 @@ const createCssStyleSheet = (): IStyleSheet => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const createStyleElement = (parent: HTMLElement): IStyleSheet => {
|
const createStyleElement = (parent: HTMLElement): IStyleSheet => {
|
||||||
const element = document.createElement('style');
|
const doc = parent.ownerDocument;
|
||||||
|
const element = doc.createElement('style');
|
||||||
parent.append(element);
|
parent.append(element);
|
||||||
return {
|
return {
|
||||||
dispose() {
|
dispose() {
|
||||||
@@ -32,7 +38,7 @@ const createStyleElement = (parent: HTMLElement): IStyleSheet => {
|
|||||||
|
|
||||||
export const createStyle = (parent: HTMLElement): IStyleSheet => {
|
export const createStyle = (parent: HTMLElement): IStyleSheet => {
|
||||||
try {
|
try {
|
||||||
return createCssStyleSheet();
|
return createCssStyleSheet(parent.ownerDocument);
|
||||||
} catch {
|
} catch {
|
||||||
return createStyleElement(parent);
|
return createStyleElement(parent);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user