Skip to content

Commit

Permalink
fix element incorrect namespaceURI leading to bad SVG rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ECorreia45 committed Dec 10, 2024
1 parent 3a7e7e3 commit 1556199
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@beforesemicolon/markup",
"version": "1.13.2",
"version": "1.13.3",
"description": "Reactive HTML Templating System",
"engines": {
"node": ">=18.16.0"
Expand Down
28 changes: 27 additions & 1 deletion src/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ describe('html', () => {
expect(document.body.innerHTML).toBe('<button>click me</button>');
})

it.skip('when anywhere inside own template', () => {
it('when anywhere inside own template', () => {
const temp = html`<div ref="box">
<p ref="paragraph">some text <span ref="value">value</span></p>
</div>`.render(document.body)
Expand Down Expand Up @@ -1864,4 +1864,30 @@ describe('html', () => {
expect(document.body.innerHTML).toBe('done')
})

it('should render SVG', () => {
html`
<p>pre</p>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 240" >
<style type="text/css">
.st0{fill-rule:evenodd;clip-rule:evenodd;}
</style>
<g>
<path class="st0" d="M60.7,116.9l33.7,39l98.6-117c7.4-7.9,19,0.5,13.2,9.5l-97,148.1c-7.4,9.5-17.4,10.5-25.8,1.1l-49-58.5 C24.9,125.4,49.1,105.9,60.7,116.9z"/>
</g>
</svg>
<p>post</p>
`.render(document.body);

const [p1, svg, p2] = [...document.body.children] as HTMLElement[];

expect(p1.namespaceURI).toBe('http://www.w3.org/1999/xhtml')

expect(svg.namespaceURI).toBe('http://www.w3.org/2000/svg')
expect(svg.children[0].namespaceURI).toBe('http://www.w3.org/2000/svg')
expect(svg.children[1].namespaceURI).toBe('http://www.w3.org/2000/svg')
expect(svg.children[1].children[0].namespaceURI).toBe('http://www.w3.org/2000/svg')

expect(p2.namespaceURI).toBe('http://www.w3.org/1999/xhtml')
})

})
6 changes: 3 additions & 3 deletions src/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ function createTemplate(
},
} as unknown as DocumentFragmentLike
},
createElementNS: (namespace: string, tagName: string) => {
createElementNS: (namespaceURI: string, tagName: string) => {
const id = createId()
const __self__ = document.createElementNS(namespace, tagName)
const __self__ = document.createElementNS(namespaceURI, tagName)

return {
__self__,
namespace,
namespaceURI,
tagName: __self__.tagName,
children: __self__.children,
attributes: __self__.attributes,
Expand Down

0 comments on commit 1556199

Please sign in to comment.