import { h, type ComponentChild, type VNode } from 'preact'; import { afterEach, describe, expect, it, vi } from 'vitest'; import { getHtmlFallback, getInteractiveNamespace, parseDirectiveValue, toVdom, } from '../vdom'; declare module 'vitest' { // Must match Vitest's generic declaration for module augmentation. interface Matchers< R extends void | Promise< void > = void | Promise< void >, // eslint-disable-next-line @typescript-eslint/no-unused-vars T = unknown, > { toMatchVNode: ( expected: ComponentChild ) => R; } } function createElementFromHTML( html: string ): HTMLElement { const div = document.createElement( 'div' ); div.innerHTML = html.trim(); return div.firstChild as HTMLElement; } const normalizeVNode = ( obj: ComponentChild ) => JSON.stringify( obj ).replace( /"__v":\d+/g, '"__v":null' ); expect.extend( { toMatchVNode( received: ComponentChild, expected: ComponentChild ) { const pass = normalizeVNode( received ) === normalizeVNode( expected ); return { pass, message: () => pass ? "Expected VNodes don't match" : 'Expected VNode match', }; }, } ); describe( 'toVdom', () => { describe( 'parseDirectiveValue', () => { it( 'should separate a namespace from a plain directive value', () => { expect( parseDirectiveValue( 'myplugin::sidebar' ) ).toEqual( { namespace: 'myplugin', value: 'sidebar', } ); } ); it( 'should keep plain objects and preserve other JSON values as strings', () => { expect( parseDirectiveValue( 'myplugin::{"id":"sidebar"}' ) ).toEqual( { namespace: 'myplugin', value: { id: 'sidebar' }, } ); expect( parseDirectiveValue( '123' ) ).toEqual( { namespace: null, value: '123', } ); expect( parseDirectiveValue( '[1,2]' ) ).toEqual( { namespace: null, value: '[1,2]', } ); expect( parseDirectiveValue( 'null' ) ).toEqual( { namespace: null, value: 'null', } ); } ); it( 'should parse plain objects preceded by whitespace', () => { expect( parseDirectiveValue( ' \n\t{"id":"sidebar"}' ) ).toEqual( { namespace: null, value: { id: 'sidebar' }, } ); expect( parseDirectiveValue( 'myplugin:: {"id":"sidebar"}' ) ).toEqual( { namespace: 'myplugin', value: { id: 'sidebar' }, } ); } ); } ); describe( 'Basic node types', () => { it( 'should convert text nodes to strings', () => { const textNode = createElementFromHTML( 'Hello World' ); expect( toVdom( textNode ) ).toMatchVNode( 'Hello World' ); } ); it( 'should convert element nodes to vDOM elements', () => { const element = createElementFromHTML( '
' ); expect( toVdom( element ) ).toMatchVNode( h( 'div', null, [] ) ); } ); it( 'should handle nested elements', () => { const element = createElementFromHTML( '
Test 1Test 2
' ); expect( toVdom( element ) ).toMatchVNode( h( 'div', null, [ h( 'span', null, [ 'Test 1' ] ), 'Test 2' ] ) ); } ); it( 'should convert CDATA sections to text nodes and continue to their siblings', () => { const parser = new DOMParser(); const xml = parser.parseFromString( '
Test 1Test 2
', 'application/xml' ); expect( toVdom( xml.querySelector( 'div' ) as Node ) ).toMatchVNode( h( 'div', null, [ 'Test 1', 'CDATA content', 'Test 2' ] ) ); expect( xml.querySelector( 'div' )?.outerHTML ).toBe( '
Test 1CDATA contentTest 2
' ); } ); it( 'should convert CDATA sections to text nodes and continue to their parent', () => { const parser = new DOMParser(); const xml = parser.parseFromString( '
Test 1
Test 2
', 'application/xml' ); expect( toVdom( xml.querySelector( 'div' ) as Node ) ).toMatchVNode( h( 'div', null, [ h( 'div', null, [ 'Test 1', 'CDATA content' ] ), h( 'div', null, [ 'Test 2' ] ), ] ) ); expect( xml.querySelector( 'div' )?.outerHTML ).toBe( '
Test 1CDATA content
Test 2
' ); } ); it( 'should remove comment nodes and continue to their siblings', () => { const container = createElementFromHTML( '
Test 1
Test 2
' ); expect( toVdom( container ) ).toMatchVNode( h( 'div', null, [ 'Test 1', h( 'div', null, [ 'Test 2' ] ) ] ) ); expect( container.outerHTML ).toBe( '
Test 1
Test 2
' ); } ); it( 'should remove multiple comment nodes', () => { const container = createElementFromHTML( '
Test 1
Test 2
' ); expect( toVdom( container ) ).toMatchVNode( h( 'div', null, [ h( 'div', null, [ 'Test 1' ] ), h( 'div', null, [ 'Test 2' ] ), ] ) ); expect( container.outerHTML ).toBe( '
Test 1
Test 2
' ); } ); it( 'should remove comment nodes and continue to their parents', () => { const container = createElementFromHTML( '
Test 1
Test 2
' ); expect( toVdom( container ) ).toMatchVNode( h( 'div', null, [ h( 'div', null, [ 'Test 1' ] ), h( 'div', null, [ 'Test 2' ] ), ] ) ); expect( container.outerHTML ).toBe( '
Test 1
Test 2
' ); } ); it( 'should remove processing instruction nodes', () => { const parser = new DOMParser(); const xml = parser.parseFromString( '
', 'application/xml' ); expect( toVdom( xml.querySelector( 'div' ) as Node ) ).toMatchVNode( h( 'div', null, [] ) ); expect( xml.querySelector( 'div' )?.outerHTML ).toBe( '
' ); } ); it( 'should handle template elements', () => { const template = createElementFromHTML( `` ); expect( toVdom( template ) ).toMatchVNode( h( 'template' as any, { content: [ 'Test' ], }, [] ) ); } ); it( 'should remove comment nodes inside templates', () => { const container = createElementFromHTML( '
' ); expect( toVdom( container ) ).toMatchVNode( h( 'div', null, [ h( 'template' as any, { content: [ h( 'div', null, [ 'Test 1' ] ) ], }, [] ), ] ) ); expect( container.outerHTML ).toBe( '
' ); } ); } ); describe( 'Attribute handling', () => { it( 'should properly parse regular HTML attributes', () => { const element = createElementFromHTML( '
' ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, { id: 'test-id', class: 'test-class', 'data-test': 'test-value', }, [] ) ); } ); it( 'should skip ref attributes', () => { const element = createElementFromHTML( '
' ); expect( toVdom( element ) ).toMatchVNode( h( 'div', { id: 'test-id' }, [] ) ); } ); it( 'should warn about malformed directive names', () => { const console = global.console; const originalWarn = console.warn; console.warn = vi.fn(); toVdom( createElementFromHTML( `
` ) ); expect( console.warn ).toHaveBeenCalledWith( `Found malformed directive name: data-wp-invalid[name].` ); console.warn = originalWarn; } ); it( 'should convert boolean attribute "open" to true', () => { const element = createElementFromHTML( '
' ); expect( toVdom( element ) ).toMatchVNode( h( 'details', { open: true }, [] ) ); } ); it( 'should convert boolean attribute "disabled" to true', () => { const element = createElementFromHTML( '' ); expect( toVdom( element ) ).toMatchVNode( h( 'button', { disabled: true }, [] ) ); } ); it( 'should convert boolean attribute "checked" to true', () => { const element = createElementFromHTML( '' ); expect( toVdom( element ) ).toMatchVNode( h( 'input', { type: 'checkbox', checked: true }, [] ) ); } ); it( 'should keep camelCase boolean attributes as empty strings', () => { // readonly maps to readOnly property, so typeof element['readonly'] // is undefined, not boolean. This falls through to Preact's setAttribute. const element = createElementFromHTML( '' ); expect( toVdom( element ) ).toMatchVNode( h( 'input', { readonly: '' }, [] ) ); } ); it( 'should preserve non-boolean empty string attributes', () => { const element = createElementFromHTML( '' ); expect( toVdom( element ) ).toMatchVNode( h( 'input', { value: '' }, [] ) ); } ); it( 'should preserve non-empty attribute values unchanged', () => { const element = createElementFromHTML( '' ); expect( toVdom( element ) ).toMatchVNode( h( 'input', { type: 'text', value: 'hello' }, [] ) ); } ); it( 'should preserve the case of SVG attribute names', () => { const element = createElementFromHTML( '' ); expect( toVdom( element ) ).toMatchVNode( h( 'svg', { viewBox: '0 0 24 24' }, [] ) ); } ); } ); describe( 'Directive processing', () => { it.each( [ 'data-wp--bind', 'data-wp---foo', 'data-wp----foo' ] )( 'should reject malformed directive name %s', ( attributeName ) => { const element = createElementFromHTML( `
` ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, { [ attributeName ]: '', __directives: {}, }, [] ) ); expect( global.console.warn ).toHaveBeenCalledTimes( 1 ); expect( global.console ).toHaveWarnedWith( `Found malformed directive name: ${ attributeName }.` ); } ); it.each( [ [ 'data-wp-bind', 'bind', null ], [ 'data-wp-style--var', 'style', 'var' ], [ 'data-wp-style----var', 'style', '--var' ], ] )( 'should parse valid directive name %s', ( attributeName, prefix, suffix ) => { const element = createElementFromHTML( `
` ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, { [ attributeName ]: '', __directives: { [ prefix ]: [ { namespace: null, value: '', suffix, uniqueId: null, }, ], }, }, [] ) ); } ); it( 'should process simple directives', () => { const element = createElementFromHTML( `
` ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, { 'data-wp-test-one': '', 'data-wp-test-two': 'test value', __directives: { 'test-one': [ { namespace: null, value: '', suffix: null, uniqueId: null, }, ], 'test-two': [ { namespace: null, value: 'test value', suffix: null, uniqueId: null, }, ], }, }, [] ) ); } ); it( 'should parse JSON values in directives', () => { const element = createElementFromHTML( `
` ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, { 'data-wp-test': '{"key": "value"}', __directives: { test: [ { namespace: null, value: { key: 'value' }, suffix: null, uniqueId: null, }, ], }, }, [] ) ); } ); it( 'should handle malformed JSON and keep as string', () => { const element = createElementFromHTML( `
` ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, { 'data-wp-test': '{malformed: json}', __directives: { test: [ { namespace: null, value: '{malformed: json}', suffix: null, uniqueId: null, }, ], }, }, [] ) ); } ); it( 'should preserve values that JSON would parse but are not objects', () => { const element = createElementFromHTML( `
` ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, { 'data-wp-test--one': 'true', 'data-wp-test--two': '"test value"', __directives: { test: [ { namespace: null, value: 'true', suffix: 'one', uniqueId: null, }, { namespace: null, value: '"test value"', suffix: 'two', uniqueId: null, }, ], }, }, [] ) ); } ); it( 'should handle directives in template elements', () => { const template = createElementFromHTML( `` ); expect( toVdom( template ) ).toMatchVNode( h( 'template' as any, { 'data-wp-test': 'test value', __directives: { test: [ { namespace: null, value: 'test value', suffix: null, uniqueId: null, }, ], }, content: [ h( 'div' as any, null, [] ) ], }, [] ) ); } ); it( 'should handle directives inside template elements', () => { const template = createElementFromHTML( `` ); expect( toVdom( template ) ).toMatchVNode( h( 'template' as any, { content: [ h( 'div' as any, { 'data-wp-test': 'test value', __directives: { test: [ { namespace: null, value: 'test value', suffix: null, uniqueId: null, }, ], }, }, [] ), ], }, [] ) ); } ); } ); describe( 'Namespaces', () => { it( 'should process directives with a custom namespace', () => { const element = createElementFromHTML( `
` ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, { 'data-wp-test': 'my-namespace::test value', __directives: { test: [ { namespace: 'my-namespace', value: 'test value', suffix: null, uniqueId: null, }, ], }, }, [] ) ); } ); it( 'should parse JSON values with a custom namespace', () => { const element = createElementFromHTML( `
` ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, { 'data-wp-test': 'my-namespace::{"key": "value"}', __directives: { test: [ { namespace: 'my-namespace', value: { key: 'value' }, suffix: null, uniqueId: null, }, ], }, }, [] ) ); } ); it( 'should use the default namespace provided in the same element', () => { const element = createElementFromHTML( `
` ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, { 'data-wp-interactive': 'my-namespace', 'data-wp-test': 'test value', __directives: { test: [ { namespace: 'my-namespace', value: 'test value', suffix: null, uniqueId: null, }, ], }, }, [] ) ); } ); it( 'should override the default namespace provided in a parent element', () => { const element = createElementFromHTML( `
` ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, { 'data-wp-interactive': 'parent-namespace', }, [ h( 'div' as any, { 'data-wp-interactive': 'my-namespace', 'data-wp-test': 'test value', __directives: { test: [ { namespace: 'my-namespace', value: 'test value', suffix: null, uniqueId: null, }, ], }, }, [] ), ] ) ); } ); it( 'should handle default namespaces provided in the a JSON object', () => { const element = createElementFromHTML( `
` ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, { 'data-wp-interactive': '{ "namespace": "my-namespace" }', 'data-wp-test': 'test value', __directives: { test: [ { namespace: 'my-namespace', value: 'test value', suffix: null, uniqueId: null, }, ], }, }, [] ) ); } ); it( 'should recover the parent default namespace after a closing element', () => { const element = createElementFromHTML( `
` ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, { 'data-wp-interactive': 'parent-namespace', }, [ h( 'div' as any, { 'data-wp-interactive': 'child-namespace', }, [] ), h( 'div' as any, { 'data-wp-test': 'test value', __directives: { test: [ { namespace: 'parent-namespace', value: 'test value', suffix: null, uniqueId: null, }, ], }, }, [] ), ] ) ); } ); it( 'should reset the default namespace after that last closing element', () => { const element = createElementFromHTML( `
` ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, null, [ h( 'div' as any, { 'data-wp-interactive': 'my-namespace', }, [] ), h( 'div' as any, { 'data-wp-test': 'test value', __directives: { test: [ { namespace: null, value: 'test value', suffix: null, uniqueId: null, }, ], }, }, [] ), ] ) ); } ); it( 'should override the default namespace with a custom one in the same element', () => { const element = createElementFromHTML( `
` ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, { 'data-wp-interactive': 'my-namespace', 'data-wp-test': 'custom-namespace::test value', __directives: { test: [ { namespace: 'custom-namespace', value: 'test value', suffix: null, uniqueId: null, }, ], }, }, [] ) ); } ); it( 'should override the default namespace with a custom one in a child element', () => { const element = createElementFromHTML( `
` ); expect( toVdom( element ) ).toMatchVNode( h( 'div' as any, { 'data-wp-interactive': 'my-namespace', }, [ h( 'div' as any, { 'data-wp-test': 'custom-namespace::test value', __directives: { test: [ { namespace: 'custom-namespace', value: 'test value', suffix: null, uniqueId: null, }, ], }, }, [] ), ] ) ); } ); it( 'should pass down namespaces defined in template elements', () => { const template = createElementFromHTML( `` ); expect( toVdom( template ) ).toMatchVNode( h( 'template' as any, { 'data-wp-interactive': 'my-namespace', content: [ h( 'div' as any, { 'data-wp-test': 'test value', __directives: { test: [ { namespace: 'my-namespace', value: 'test value', suffix: null, uniqueId: null, }, ], }, }, [] ), ], }, [] ) ); } ); it( 'should pass down namespaces defined in template parents', () => { const template = createElementFromHTML( `
` ); expect( toVdom( template ) ).toMatchVNode( h( 'div' as any, { 'data-wp-interactive': 'my-namespace', }, [ h( 'template' as any, { content: [ h( 'div' as any, { 'data-wp-test': 'test value', __directives: { test: [ { namespace: 'my-namespace', value: 'test value', suffix: null, uniqueId: null, }, ], }, }, [] ), ], }, [] ), ] ) ); } ); } ); describe( 'Initial namespace', () => { /** * Appends the markup to the document body and returns the element * matching the selector, so the conversion root has ancestors. * * @param html Markup to attach. * @param selector Selector of the element to return. * @return The matching element. */ function attach( html: string, selector: string ): HTMLElement { const container = document.createElement( 'div' ); container.innerHTML = html.trim(); document.body.append( container ); return container.querySelector( selector )!; } /** * Lists the namespace of the `data-wp-test` directive of every * element in the vDOM, in document order, including `