Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] link set setAttribute disabled not work #169

Open
chenyulun opened this issue Mar 30, 2022 · 1 comment
Open

[bug] link set setAttribute disabled not work #169

chenyulun opened this issue Mar 30, 2022 · 1 comment

Comments

@chenyulun
Copy link

root.css

:root {
  --color-1: red;
}
.color{
  color: var(--color-1)
}

root2.css

:root {
  --color-1: green;
}

.color{
  color: var(--color-1)
}

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link href="./root.css" rel="stylesheet" />
  <link href="./root2.css" rel="stylesheet" id="root2" />
  <style>
    .bg {
        background-color: var(--color-1);
    }
  </style>
</head>
<body>
  <div class="color">1111</div>
    <div class="bg">2222</div>
</body>
</html>

Now the text color is green
Enter in console

document.getElementById('root2').setAttribute('disabled', '')

The text color remains the same. The expected text color should be red

Disabling to become available is normal

-  <link href="./root2.css" rel="stylesheet" id="root2" />
+  <link href="./root2.css" rel="stylesheet" id="root2" disabled />

Enter in console

document.getElementById('root2').removeAttribute('disabled', '')

The reason is that the listening change function is not supported

Any change to the Disabled property should be reprocessed

function isValidAttributeMutation(mutation) {
        let isValid = false;
        // mutation: MutationRecord
        // addedNodes: NodeList []
        // attributeName: "disabled"
        // attributeNamespace: null
        // nextSibling: null
        // oldValue: null
        // previousSibling: null
        // removedNodes: NodeList []
        // target: link#root2
        // type: "attributes"
        if (mutation.type === 'attributes' && isLink(mutation.target) && !isDisabled(mutation.target)) {
            const isEnabledMutation = mutation.attributeName === 'disabled';
            const isHrefMutation = mutation.attributeName === 'href';
            const isSkipNode = mutation.target.getAttribute('data-cssvars') === 'skip';
            const isSrcNode = mutation.target.getAttribute('data-cssvars') === 'src';

            // Enabled
            if (isEnabledMutation) {
                isValid = !isSkipNode && !isSrcNode;
            }
            // Href
            else if (isHrefMutation) {
                if (isSkipNode) {
                    mutation.target.setAttribute('data-cssvars', '');
                }
                else if (isSrcNode) {
                    resetCssNodes(settings.rootElement, true);
                }

                isValid = true;
            }
        }

        return isValid;
    }

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

if(node.__cssVars.cssTree) {
Object.assign(variableStore.dom, node.__cssVars.nodeVars);
                            return;
                        }
// ...
const nodeVars = parseVars(cssTree, {
  parseHost: Boolean(settings.rootElement.host),
  // store    : variableStore.dom,
  onWarning: handleWarning
});

Object.assign(variableStore.dom, nodeVars);

// Cache data
node.__cssVars.tree = cssTree;
node.__cssVars.vars = nodeVars;
@jhildenbiddle
Copy link
Owner

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.

https://stackoverflow.com/questions/5757231/disabled-true-can-be-set-dynamically-on-style-why-not-statically

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:

const variableStore = {
// Parsed DOM values (from <link> and <style> nodes)
dom : {},
// Temporary storage for each job
job : {},
// Persisted options.variables values
user: {}
};

You are right about the unnecessary processing here though:

const nodeCSS = cssArray[i];
// Node data cache
node.__cssVars = node.__cssVars || {};
node.__cssVars.text = nodeCSS;
// Only process CSS contains a custom property
// declarations or function
if (regex.cssVars.test(nodeCSS)) {

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants