simplestyle-js 4.0.3 → 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/createStyles.cjs +17 -7
- package/dist/esm/createStyles.mjs +17 -7
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-deprecated */ "use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", {
|
|
3
3
|
value: true
|
|
4
4
|
});
|
|
@@ -123,18 +123,28 @@ function replaceBackReferences(out, sheetContents) {
|
|
|
123
123
|
return (0, _plugins.getPosthooks)().reduce((prev, hook)=>hook(prev), outputSheetContents);
|
|
124
124
|
}
|
|
125
125
|
function createSheet(ruleId, sheetContents) {
|
|
126
|
+
const out = {
|
|
127
|
+
existing: null,
|
|
128
|
+
styleTag: null
|
|
129
|
+
};
|
|
126
130
|
const doc = globalThis.document;
|
|
127
|
-
if (doc === undefined) return
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const
|
|
131
|
+
if (doc === undefined) return out;
|
|
132
|
+
if (typeof doc?.head?.appendChild !== 'function' || typeof doc.createElement !== 'function') return out;
|
|
133
|
+
// attempt to reuse the style tag, if it existed
|
|
134
|
+
const existingTag = doc.getElementById?.(ruleId);
|
|
135
|
+
const existing = Boolean(existingTag);
|
|
136
|
+
const styleTag = existingTag ?? doc.createElement('style');
|
|
131
137
|
styleTag.id = ruleId;
|
|
132
138
|
styleTag.innerHTML = sheetContents;
|
|
133
|
-
|
|
139
|
+
out.existing = existing;
|
|
140
|
+
out.styleTag = styleTag;
|
|
141
|
+
return out;
|
|
134
142
|
}
|
|
135
143
|
function flushSheetContents(ruleId, sheetContents, options) {
|
|
136
144
|
// In case we're in come weird test environment that doesn't support JSDom
|
|
137
|
-
const styleTag = createSheet(ruleId, sheetContents);
|
|
145
|
+
const { existing, styleTag } = createSheet(ruleId, sheetContents);
|
|
146
|
+
// if the tag existed, DO NOT render it back out to the DOM.
|
|
147
|
+
if (existing) return styleTag;
|
|
138
148
|
if (styleTag) {
|
|
139
149
|
if (options?.insertAfter && options.insertBefore) {
|
|
140
150
|
throw new Error('Both insertAfter and insertBefore were provided. Please choose only one.');
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import merge from 'deepmerge';
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-deprecated */ import merge from 'deepmerge';
|
|
2
2
|
import { generateClassName } from './generateClassName.mjs';
|
|
3
3
|
import { getPosthooks } from './plugins.mjs';
|
|
4
4
|
function isNestedSelector(r) {
|
|
@@ -88,18 +88,28 @@ function replaceBackReferences(out, sheetContents) {
|
|
|
88
88
|
return getPosthooks().reduce((prev, hook)=>hook(prev), outputSheetContents);
|
|
89
89
|
}
|
|
90
90
|
function createSheet(ruleId, sheetContents) {
|
|
91
|
+
const out = {
|
|
92
|
+
existing: null,
|
|
93
|
+
styleTag: null
|
|
94
|
+
};
|
|
91
95
|
const doc = globalThis.document;
|
|
92
|
-
if (doc === undefined) return
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
+
if (doc === undefined) return out;
|
|
97
|
+
if (typeof doc?.head?.appendChild !== 'function' || typeof doc.createElement !== 'function') return out;
|
|
98
|
+
// attempt to reuse the style tag, if it existed
|
|
99
|
+
const existingTag = doc.getElementById?.(ruleId);
|
|
100
|
+
const existing = Boolean(existingTag);
|
|
101
|
+
const styleTag = existingTag ?? doc.createElement('style');
|
|
96
102
|
styleTag.id = ruleId;
|
|
97
103
|
styleTag.innerHTML = sheetContents;
|
|
98
|
-
|
|
104
|
+
out.existing = existing;
|
|
105
|
+
out.styleTag = styleTag;
|
|
106
|
+
return out;
|
|
99
107
|
}
|
|
100
108
|
function flushSheetContents(ruleId, sheetContents, options) {
|
|
101
109
|
// In case we're in come weird test environment that doesn't support JSDom
|
|
102
|
-
const styleTag = createSheet(ruleId, sheetContents);
|
|
110
|
+
const { existing, styleTag } = createSheet(ruleId, sheetContents);
|
|
111
|
+
// if the tag existed, DO NOT render it back out to the DOM.
|
|
112
|
+
if (existing) return styleTag;
|
|
103
113
|
if (styleTag) {
|
|
104
114
|
if (options?.insertAfter && options.insertBefore) {
|
|
105
115
|
throw new Error('Both insertAfter and insertBefore were provided. Please choose only one.');
|
package/package.json
CHANGED