Skip to content

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

Closed
@trusktr

Description

@trusktr

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions