import assert from 'node:assert/strict'; import fs from 'node:fs'; import vm from 'node:vm'; const sourcePath = new URL('../src/Modules/GrapesJS/resources/assets/js/save-page.js', import.meta.url); const source = fs.readFileSync(sourcePath, 'utf8'); class TestElement { constructor() { this._innerHTML = ''; } set innerHTML(value) { this._innerHTML = value; } get innerHTML() { return this._innerHTML; } querySelectorAll() { const pattern = /]*\bphpb-blocks-container\b[^>]*)>([\s\S]*?)<\/div>/g; return [...this._innerHTML.matchAll(pattern)].map((match, index) => { return new TestContainer(this, index, ``, match[2]); }); } replaceContainer(index, containerHtml) { const pattern = /]*\bphpb-blocks-container\b[^>]*)>([\s\S]*?)<\/div>/g; let currentIndex = 0; this._innerHTML = this._innerHTML.replace(pattern, match => { return currentIndex++ === index ? containerHtml : match; }); } appendChild(container) { this._innerHTML += container.outerHTML; } } class TestContainer { constructor(parent, index, openingTag, innerHTML) { this.parent = parent; this.index = index; this.openingTag = openingTag; this._innerHTML = innerHTML; } set innerHTML(value) { this._innerHTML = value; this.parent.replaceContainer(this.index, `${this.openingTag}${value}`); } get innerHTML() { return this._innerHTML; } get outerHTML() { return `${this.openingTag}${this._innerHTML}`; } cloneNode() { return new TestContainer(null, 0, this.openingTag, this._innerHTML); } } const window = {}; const document = { getElementById: () => null, addEventListener: () => {}, createElement: () => new TestElement(), }; vm.runInNewContext(source, { window, document, console, setTimeout, URLSearchParams }); const sourceBlocks = { parent: { html: '

Source locale text

[block slug="child" id="new-child"]
', settings: {label: 'Source setting'}, blocks: { existingChild: {html: '

Source child text

', settings: {label: 'Source child setting'}}, newChild: {html: '

New shared block

', settings: {label: 'New setting'}}, }, }, newParent: {html: '

New shared parent

', settings: {label: 'New parent setting'}}, }; const targetBlocks = { parent: { html: '

Translated parent text

[block slug="child" id="existing-child"]
', settings: {label: 'Translated parent setting'}, blocks: { existingChild: {html: '

Existing translated child

', settings: {label: 'Translated child setting'}}, }, }, localeOnlyParent: {html: '

Existing locale-only content

'}, }; const synchronizedBlocks = window.synchronizePageBlockVariants(sourceBlocks, targetBlocks); assert.match(synchronizedBlocks.parent.html, /Translated parent text/); assert.match(synchronizedBlocks.parent.html, /id="new-child"/); assert.equal(synchronizedBlocks.parent.settings.label, 'Translated parent setting'); assert.equal(synchronizedBlocks.parent.blocks.existingChild.html, '

Existing translated child

'); assert.equal(synchronizedBlocks.parent.blocks.existingChild.settings.label, 'Translated child setting'); assert.equal(synchronizedBlocks.parent.blocks.newChild.html, '

New shared block

'); assert.equal(synchronizedBlocks.newParent.html, '

New shared parent

'); assert.equal(synchronizedBlocks.localeOnlyParent.html, '

Existing locale-only content

'); sourceBlocks.parent.blocks.newChild.html = '

Source changed after synchronization

'; assert.equal(synchronizedBlocks.parent.blocks.newChild.html, '

New shared block

'); console.log('PASS: shared block structure synchronizes without overwriting existing locale content.');