Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions pkgs/id/web/src/image-node-view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,68 @@ describe("committed attrs on mouse-up", () => {
expect(committed?.attrs.height).toBe(120);
});
});

// ── update() src sync (#1392) ─────────────────────────────────────

describe("update() src sync (#1392)", () => {
/** Count assignments to img.src via an own-property setter spy. */
function countSrcSets(img: HTMLImageElement): { sets: () => number } {
const proto = Object.getPrototypeOf(img) as object;
const desc = Object.getOwnPropertyDescriptor(proto, "src");
if (!desc?.get || !desc?.set) throw new Error("img src descriptor not found");
let count = 0;
Object.defineProperty(img, "src", {
configurable: true,
get() {
return desc.get!.call(this);
},
set(v: string) {
count += 1;
desc.set!.call(this, v);
},
});
return { sets: () => count };
}

it("an update with unchanged attrs does not reassign img.src", () => {
const { nv, img, view } = (() => {
const s = setup();
return { ...s, view: s.view };
})();
const counter = countSrcSets(img);
const same = richSchema.nodes.image.create({
src: "/api/images/abc123",
alt: "cat",
width: START_W,
height: START_H,
});
expect(nv.update(same)).toBe(true);
expect(counter.sets()).toBe(0);
expect(img.getAttribute("src")).toBe("/api/images/abc123");
void view;
});

it("a changed src updates the image exactly once, and clearing clears it", () => {
const { nv, img } = setup();
const counter = countSrcSets(img);
const changed = richSchema.nodes.image.create({
src: "/api/images/def456",
alt: "cat",
width: START_W,
height: START_H,
});
expect(nv.update(changed)).toBe(true);
expect(counter.sets()).toBe(1);
expect(img.getAttribute("src")).toBe("/api/images/def456");

const cleared = richSchema.nodes.image.create({
src: "",
alt: "cat",
width: START_W,
height: START_H,
});
expect(nv.update(cleared)).toBe(true);
expect(counter.sets()).toBe(2);
expect(img.getAttribute("src")).toBe("");
});
});
9 changes: 7 additions & 2 deletions pkgs/id/web/src/image-node-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,13 @@ export class ImageNodeView implements NodeView {
if (node.type !== this.node.type) return false;
this.node = node;

// Sync DOM
if (this.img.src !== node.attrs.src) this.img.src = node.attrs.src || "";
// Sync DOM. Compare against the authored attribute, not the
// img.src getter: the getter resolves relative sources to an
// absolute URL, so it never equals the authored relative attr and
// the old guard reassigned (reloading the image) on every update
// (#1392).
const nextSrc = node.attrs.src || "";
if (this.img.getAttribute("src") !== nextSrc) this.img.src = nextSrc;
if (node.attrs.alt != null) this.img.alt = node.attrs.alt;
if (node.attrs.title != null) this.img.title = node.attrs.title;

Expand Down