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

Fixed value change observers not firing #697

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/data_map.ts
Expand Up @@ -43,6 +43,6 @@ export class DataMap {
}

getAttributeNameForKey(key: string): string {
return `data-${this.identifier}-${dasherize(key)}`
return `data-${this.identifier.toLowerCase()}-${dasherize(key)}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this is the right fix for the issue raised in #680. The attribute methods on Element seem to be case-insensitive, which leads me to believe that we need to week this somewhere else.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I get what you mean, they are case insensitive, meaning that you can read or write them with whatever casing and it would still work, but the mutation observer always reports the changed properties as lowercase strings. The consequence is that when we check if we should care about the property change, we always opt to discard the change event, because we instead stored the property with whatever casing was found in the document. I've not found this documented anywhere, but I tried both Chromium and Firefox and both did the same.

If I didn't get what you mean, could you point me in the direction where you think the fix should be made instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @RBastianini, I just realized that my comment wasn't very well thought out, sorry about that! Let me dig into this and come back with a more constructive answer. Thank you!

}
}
92 changes: 92 additions & 0 deletions src/tests/modules/core/value_tests_case_insensitive.ts
@@ -0,0 +1,92 @@
import ValueTests from "./value_tests"

export default class ValueCaseInsensitiveTests extends ValueTests {
identifier = "Test"

fixtureHTML = `
<div data-controller="${this.identifier}"
data-${this.identifier}-shadowed-boolean-value="true"
data-${this.identifier}-numeric-value="123"
data-${this.identifier}-string-value="ok"
data-${this.identifier}-ids-value="[1,2,3]"
data-${this.identifier}-options-value='{"one":[2,3]}'
data-${this.identifier}-time-24hr-value="true">
</div>
`

async "test changed callbacks"() {
this.assert.deepEqual(this.controller.loggedNumericValues, [123])
this.assert.deepEqual(this.controller.oldLoggedNumericValues, [0])

this.controller.numericValue = 0
await this.nextFrame
this.assert.deepEqual(this.controller.loggedNumericValues, [123, 0])
this.assert.deepEqual(this.controller.oldLoggedNumericValues, [0, 123])

this.set("numeric-value", "1")
await this.nextFrame
this.assert.deepEqual(this.controller.loggedNumericValues, [123, 0, 1])
this.assert.deepEqual(this.controller.oldLoggedNumericValues, [0, 123, 0])
}

async "test changed callbacks for object"() {
this.assert.deepEqual(this.controller.optionsValues, [{ one: [2, 3] }])
this.assert.deepEqual(this.controller.oldOptionsValues, [{}])

this.controller.optionsValue = { person: { name: "John", age: 42, active: true } }
await this.nextFrame
this.assert.deepEqual(this.controller.optionsValues, [
{ one: [2, 3] },
{ person: { name: "John", age: 42, active: true } },
])
this.assert.deepEqual(this.controller.oldOptionsValues, [{}, { one: [2, 3] }])

this.set("options-value", "{}")
await this.nextFrame
this.assert.deepEqual(this.controller.optionsValues, [
{ one: [2, 3] },
{ person: { name: "John", age: 42, active: true } },
{},
])
this.assert.deepEqual(this.controller.oldOptionsValues, [
{},
{ one: [2, 3] },
{ person: { name: "John", age: 42, active: true } },
])
}

async "test default values trigger changed callbacks"() {
this.assert.deepEqual(this.controller.loggedMissingStringValues, [""])
this.assert.deepEqual(this.controller.oldLoggedMissingStringValues, [undefined])

this.controller.missingStringValue = "hello"
await this.nextFrame
this.assert.deepEqual(this.controller.loggedMissingStringValues, ["", "hello"])
this.assert.deepEqual(this.controller.oldLoggedMissingStringValues, [undefined, ""])

this.controller.missingStringValue = undefined as any
await this.nextFrame
this.assert.deepEqual(this.controller.loggedMissingStringValues, ["", "hello", ""])
this.assert.deepEqual(this.controller.oldLoggedMissingStringValues, [undefined, "", "hello"])
}

has(name: string) {
return this.element.hasAttribute(this.attr(name))
}

get(name: string) {
return this.element.getAttribute(this.attr(name))
}

set(name: string, value: string) {
return this.element.setAttribute(this.attr(name), value)
}

attr(name: string) {
return `data-${this.identifier}-${name}`
}

get element() {
return this.controller.element
}
}