Open chenyulun opened 2 years ago
The HTML <style>
element does not support the disabled
attribute. IE supports it, but no other browser does. This is why the ponyfill ignores the disabled
attribute on <style>
elements.
The ponyfill does support the disabled
property on the HTMLLinkElement.sheet
and HTMLStyleElement.sheet
objects. Alternatively, you can also just remove the <link>
or <style>
element from the DOM.
Parsed cssTree and pasreVars should be cached and not re-parsed
See the __cssVars
property attached to any <link>
or <style>
element with a data-cssvars="src"
attribute. You'll find the the cssTree
object and the original CSS text stored there. The CSS text is used for comparison because any change to it will require generating new CSS output (i.e., the ponyfill has to be aware of more than just variable changes). Variables are cached elsewhere and do not need to be stored on a per-node basis:
You are right about the unnecessary processing here though:
Instead of blindly assigning nodeCSS
to node.__cssVars.text
, the ponyfill should be checking to see if node.__cssVars.text
exists and if it matches nodeCSS
to determine if the node needs to be processed.
const nodeCSS = cssArray[i];
const hasCSSCache = Boolean(node.__cssVars && node.__cssVars.text);
const hasCSSChanged = hasCSSCache && node.__cssVars.text !== nodeCSS;
// Node data cache
node.__cssVars = node.__cssVars || {};
node.__cssVars.text = nodeCSS;
// Only process CSS if it has changed or contains a
// custom property declarations or function
if ((hasCSSCache && hasCSSChanged) || regex.cssVars.test(nodeCSS)) {
I'll have to do some testing to verify that the change is is beneficial. It's been a while since I've looked at the code.
root.css
root2.css
index.html
Now the text color is green Enter in console
The text color remains the same. The expected text color should be red
Disabling to become available is normal
Enter in console
The reason is that the listening change function is not supported
Any change to the Disabled property should be reprocessed
by the way: Parsed cssTree and pasreVars should be cached and not re-parsed the original content of unchanged link and style, If the cache exists, use it directly. Remove cssTree and VARS caches when link and style elements change again