Skip to content
Closed
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
13 changes: 11 additions & 2 deletions blocks/edit/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,18 @@ export function setDaMetadata(key, value) {
if (!daMdMap) return;
if (value === null || value === undefined) {
daMdMap.delete(key);
} else {
daMdMap.set(key, value);
return;
}
// The Y.Map serializes to a <div class="da-metadata"> block where values are
// interpolated as text. Non-strings (objects, Y types) would render as
// "[object Object]" in the saved HTML, so coerce at the boundary.
let stringValue = value;
if (typeof stringValue !== 'string') {
// eslint-disable-next-line no-console
console.warn(`setDaMetadata: non-string value for "${key}"`, value);
stringValue = typeof value === 'object' ? JSON.stringify(value) : String(value);
}
daMdMap.set(key, stringValue);
}

export function getDiffLabels() {
Expand Down
25 changes: 25 additions & 0 deletions test/unit/blocks/edit/utils/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,31 @@ describe('daMetadata functions', () => {
expect(getDaMetadata('key2')).to.be.null;
expect(getDaMetadata('key3')).to.equal('value3');
});

it('Coerces object values to JSON strings to avoid "[object Object]"', () => {
const originalWarn = console.warn;
console.warn = () => {};
try {
setDaMetadata('obj-key', { a: 1, b: 'two' });
expect(getDaMetadata('obj-key')).to.equal('{"a":1,"b":"two"}');
} finally {
console.warn = originalWarn;
}
});

it('Coerces non-string primitive values to strings', () => {
const originalWarn = console.warn;
console.warn = () => {};
try {
setDaMetadata('num-key', 42);
expect(getDaMetadata('num-key')).to.equal('42');

setDaMetadata('bool-key', true);
expect(getDaMetadata('bool-key')).to.equal('true');
} finally {
console.warn = originalWarn;
}
});
});
});

Expand Down
Loading