mirror of
https://github.com/MercuryWorkshop/dreamlandjs.git
synced 2025-05-15 07:10:00 -04:00
fix css`` on old browsers
This commit is contained in:
parent
a60d4fb5c4
commit
a6db2049ff
1 changed files with 25 additions and 4 deletions
29
src/css.js
29
src/css.js
|
@ -1,6 +1,8 @@
|
|||
Object.assign(window, { css, rule: css, styled: { new: css, rule: css } })
|
||||
Object.assign(window, { css, styled: { new: css, rule: css } })
|
||||
const cssmap = {}
|
||||
export function css(strings, ...values) {
|
||||
|
||||
|
||||
export function css(strings, values = []) {
|
||||
let str = ''
|
||||
for (let f of strings) {
|
||||
str += f + (values.shift() || '')
|
||||
|
@ -19,8 +21,27 @@ export function css(strings, ...values) {
|
|||
const styleElement = document.createElement('style')
|
||||
document.head.appendChild(styleElement)
|
||||
|
||||
styleElement.textContent = `.${uid} { ${str}; }`
|
||||
cssmap[str] = uid
|
||||
|
||||
// kind of a hack. when css nesting stablizes this can be removed
|
||||
styleElement.textContent = str;
|
||||
let newstr = "";
|
||||
let selfstr = "";
|
||||
while (!styleElement.sheet.cssRules.length) {
|
||||
let [first, ...rest] = str.split("\n");
|
||||
selfstr += first + "\n";
|
||||
str = rest.join("\n");
|
||||
styleElement.textContent = str;
|
||||
}
|
||||
|
||||
|
||||
for (const rule of styleElement.sheet.cssRules) {
|
||||
rule.selectorText = `.${uid} ${rule.selectorText}`
|
||||
newstr += rule.cssText + "\n";
|
||||
}
|
||||
|
||||
styleElement.textContent = `.${uid} {${selfstr}}` + "\n" + newstr;
|
||||
console.log(styleElement.textContent);
|
||||
|
||||
cssmap[str] = uid
|
||||
return uid
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue