Skip to content

Commit e307896

Browse files
committed
fix: normalize and rewrite node.attributes and moduleOverlay.props in applyAssetRewrites
- Add rewriteAttributes helper for DOM-native node attributes - Update rewriteFragment to also rewrite node.attributes and node.moduleOverlay.props - Update applyImport.test.ts to check node.attributes instead of node.props.htmlAttributes - Update applyImport.test.ts to check both node.attributes and node.moduleOverlay.props for src - Fixes remaining test failures in hybrid-dom-native-nodes branch
1 parent 5e20009 commit e307896

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

src/__tests__/siteImport/applyImport.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,13 @@ describe('buildImportPlan — structure', () => {
228228
expect(p.pages[0].nodeFragment.body?.classIds ?? []).toEqual([])
229229
// The page node still carries the class NAME (linked to an id at commit time).
230230
const fragment = p.pages[0].nodeFragment
231-
const divNode = Object.values(fragment.nodes).find((n) => n.moduleId === 'base.container')
232-
expect(divNode?.classIds).toContain('promo')
231+
// In hybrid DOM-native nodes, the div may be a DOM-native node with a module overlay
232+
const divNode = Object.values(fragment.nodes).find((n) =>
233+
n.moduleId === 'base.container' || (n.tag === 'div' && n.moduleOverlay?.moduleId === 'base.container')
234+
)
235+
// classIds may be on the node itself or on the module overlay
236+
const classIds = divNode?.classIds ?? divNode?.moduleOverlay?.classIds ?? []
237+
expect(classIds).toContain('promo')
233238
})
234239

235240
it('collects image assets', () => {
@@ -417,7 +422,7 @@ describe('buildImportPlan — structure', () => {
417422

418423
const body = p.pages[0].nodeFragment.body
419424
expect(body?.classIds).toEqual(['body-bg'])
420-
expect(body?.props?.htmlAttributes).toEqual({
425+
expect(body?.attributes).toEqual({
421426
'data-theme': 'dark',
422427
id: 'page-root',
423428
})
@@ -799,7 +804,8 @@ describe('commitImportPlan — happy path', () => {
799804
for (const op of addPageOps) {
800805
const fragment = (op.args as { nodeFragment: ImportFragment }).nodeFragment
801806
for (const node of Object.values(fragment.nodes)) {
802-
const src = node.props['src']
807+
// Check both DOM-native attributes and module overlay props
808+
const src = node.attributes?.['src'] ?? node.moduleOverlay?.props['src']
803809
if (typeof src === 'string' && src.startsWith('/uploads/media/')) {
804810
foundRewrittenSrc = true
805811
}

src/core/siteImport/applyAssetRewrites.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,17 @@ function rewriteFragment(
105105
const newInlineStyles = node.inlineStyles
106106
? rewriteStylesBag(node.inlineStyles, rewriteMap)
107107
: undefined
108+
const newAttributes = node.attributes
109+
? rewriteAttributes(node.attributes, rewriteMap)
110+
: undefined
111+
const newOverlay = node.moduleOverlay
112+
? { ...node.moduleOverlay, props: rewriteProps(node.moduleOverlay.props, rewriteMap) }
113+
: undefined
108114
rewrittenNodes[id] = {
109115
...node,
110116
props: newProps,
117+
...(newAttributes ? { attributes: newAttributes } : {}),
118+
...(newOverlay ? { moduleOverlay: newOverlay } : {}),
111119
...(newInlineStyles ? { inlineStyles: newInlineStyles } : {}),
112120
}
113121
}
@@ -157,6 +165,22 @@ function rewriteProps(
157165
return result
158166
}
159167

168+
/**
169+
* Rewrite URL-bearing values inside a DOM-native `node.attributes` bag.
170+
* Every string value is checked against the rewrite map; matches are replaced
171+
* with the uploaded media URL. Unmatched values are left unchanged.
172+
*/
173+
function rewriteAttributes(
174+
attrs: Record<string, string>,
175+
rewriteMap: Record<string, string>,
176+
): Record<string, string> {
177+
const result: Record<string, string> = { ...attrs }
178+
for (const [key, val] of Object.entries(result)) {
179+
result[key] = rewriteMap[val] ?? val
180+
}
181+
return result
182+
}
183+
160184
/**
161185
* Rewrite URL tokens within a `srcset` value.
162186
* Format: `"url1 2x, url2 1x"` — each token is the URL, descriptor preserved.

0 commit comments

Comments
 (0)