Skip to content

MutationObserver attribute change record only gives us the oldValue, but not the newValue? #521

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

Closed
trusktr opened this issue Oct 15, 2017 · 2 comments

Comments

@trusktr
Copy link

trusktr commented Oct 15, 2017

I only see an oldValue when trying to observe attribute changes. Are we expected to access the attribute values on the element, inside the observer's callback?

For example, here's mine:

        const observer = new MutationObserver( records => {

            for (const record of records) {
                behavior.attributeChangedCallback(
                    this.ownerElement,
                    record.attributeName,
                    record.oldValue,
                    this.ownerElement.getAttribute( record.attributeName )
                )
            }

        } )

where in my case behavior is part of an implementation of element behaviors (the has="" attribute), implemented on to of custom-attributes, but I haven't published it yet.

The behavior.attributeChangedCallback signature is (element, attributeName, oldValue, newValue), so as you see I'm getting the new value from the element directly.

But this seems dirty. I might be calling some Custom Element's overriden or extended getAttribute method and causing unknown side-effects.

How does the MutationObserver implementation get the element's oldValue? It seems that the new value should be gotten the same way as the engine gets the old value (if not already) in order to be consistent.

I don't know exactly what "the same way" means though. Does the engine get the original value passed into HTMLElement.prototype.setAttribute (the native one, even is monkey patched)? Or what does it do?

Should I be looping on element.attributes instead, and using that as the source of truth, rather than getAttribute?

I'm wondering about what MutationObserver does for oldValue, but in general, considering that nothing will change for a while, I'm looking for advice on what's best to do. Thanks!

@annevk
Copy link
Member

annevk commented Oct 16, 2017

If you cannot trust getAttribute() you cannot trust MutationObserver either. The idea is that you'd use getAttribute().

@annevk annevk closed this as completed Oct 16, 2017
@annevk
Copy link
Member

annevk commented Oct 16, 2017

Also, for usage questions, I encourage you to reach out to the stackoverflow.com community. This repository is for issues with the standard itself.

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

No branches or pull requests

2 participants