Skip to content

Commit 6b8c2e9

Browse files
committed
Simplify dirty tracking
Depend on the native browser support for this.
1 parent 45d67fd commit 6b8c2e9

2 files changed

Lines changed: 8 additions & 9 deletions

File tree

src/morphlex.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -391,32 +391,31 @@ class Morph {
391391
}
392392

393393
#visitAttributes(from: Element, to: Element): void {
394-
const wasDirty = from.hasAttribute("morphlex-dirty")
395-
if (wasDirty) {
394+
if (from.hasAttribute("morphlex-dirty")) {
396395
from.removeAttribute("morphlex-dirty")
397396
}
398397

399398
// First pass: update/add attributes from reference (iterate forwards)
400399
for (const { name, value } of to.attributes) {
401400
if (name === "value") {
402401
if (isInputElement(from) && from.value !== value) {
403-
if (from !== this.#skipValuePropertyUpdateFor && (!this.#options.preserveChanges || !wasDirty)) {
402+
if (from !== this.#skipValuePropertyUpdateFor && !this.#options.preserveChanges) {
404403
from.value = value
405404
}
406405
}
407406
}
408407

409408
if (name === "selected") {
410409
if (isOptionElement(from) && !from.selected) {
411-
if (!this.#options.preserveChanges || !wasDirty) {
410+
if (!this.#options.preserveChanges) {
412411
from.selected = true
413412
}
414413
}
415414
}
416415

417416
if (name === "checked") {
418417
if (isInputElement(from) && !from.checked) {
419-
if (!this.#options.preserveChanges || !wasDirty) {
418+
if (!this.#options.preserveChanges) {
420419
from.checked = true
421420
}
422421
}
@@ -435,15 +434,15 @@ class Morph {
435434
if (!to.hasAttribute(name)) {
436435
if (name === "selected") {
437436
if (isOptionElement(from) && from.selected) {
438-
if (!this.#options.preserveChanges || !wasDirty) {
437+
if (!this.#options.preserveChanges) {
439438
from.selected = false
440439
}
441440
}
442441
}
443442

444443
if (name === "checked") {
445444
if (isInputElement(from) && from.checked) {
446-
if (!this.#options.preserveChanges || !wasDirty) {
445+
if (!this.#options.preserveChanges) {
447446
from.checked = false
448447
}
449448
}

test/new/inputs.browser.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe("text input", () => {
7373
expect(second.getAttribute("value")).toBe("a")
7474
})
7575

76-
test("morphing updates default while dirty and updates value once clean again", () => {
76+
test("morphing updates default while dirty and keeps value dirty", () => {
7777
const input = dom(`<input type="text" value="a">`) as HTMLInputElement
7878

7979
input.value = "b"
@@ -89,7 +89,7 @@ describe("text input", () => {
8989
expect(input.defaultValue).toBe("c")
9090

9191
morph(input, dom(`<input type="text" value="d">`), { preserveChanges: true })
92-
expect(input.value).toBe("d")
92+
expect(input.value).toBe("c")
9393
expect(input.defaultValue).toBe("d")
9494
})
9595
})

0 commit comments

Comments
 (0)