Skip to content

Commit

Permalink
feat(static-renderer): add support for namespacing in DOMOutputSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
nperez0111 committed Dec 31, 2024
1 parent d66bab4 commit 24c1009
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
10 changes: 8 additions & 2 deletions packages/static-renderer/src/pm/html-string/html-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { renderToElement } from '../extensionRenderer.js'
*/
export function serializeAttrsToHTMLString(attrs: Record<string, any>): string {
const output = Object.entries(attrs)
.map(([key, value]) => `${key}=${JSON.stringify(value)}`)
.map(([key, value]) => `${key.split(' ').at(-1)}=${JSON.stringify(value)}`)
.join(' ')

return output ? ` ${output}` : ''
Expand Down Expand Up @@ -44,7 +44,13 @@ export function domOutputSpecToHTMLString(
return () => content
}
if (typeof content === 'object' && 'length' in content) {
const [tag, attrs, children, ...rest] = content as DOMOutputSpecArray
const [_tag, attrs, children, ...rest] = content as DOMOutputSpecArray
let tag = _tag
const parts = tag.split(' ')

if (parts.length > 1) {
tag = `${parts[1]} xmlns="${parts[0]}"`
}

if (attrs === undefined) {
return () => `<${tag}/>`
Expand Down
22 changes: 21 additions & 1 deletion packages/static-renderer/src/pm/react/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,27 @@ export function domOutputSpecToReactElement(
return () => content
}
if (typeof content === 'object' && 'length' in content) {
const [tag, attrs, children, ...rest] = content as DOMOutputSpecArray
// eslint-disable-next-line prefer-const
let [tag, attrs, children, ...rest] = content as DOMOutputSpecArray
const parts = tag.split(' ')

if (parts.length > 1) {
tag = parts[1]
if (attrs === undefined) {
attrs = {
xmlns: parts[0],
}
}
if (attrs === 0) {
attrs = {
xmlns: parts[0],
}
children = 0
}
if (typeof attrs === 'object') {
attrs = Object.assign(attrs, { xmlns: parts[0] })
}
}

if (attrs === undefined) {
return () => React.createElement(tag, mapAttrsToHTMLAttributes(undefined, key.toString()))
Expand Down

0 comments on commit 24c1009

Please sign in to comment.