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 1 Test 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(
'',
'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(
''
);
expect( toVdom( container ) ).toMatchVNode(
h( 'div', null, [ 'Test 1', h( 'div', null, [ 'Test 2' ] ) ] )
);
expect( container.outerHTML ).toBe(
''
);
} );
it( 'should remove multiple comment nodes', () => {
const container = createElementFromHTML(
''
);
expect( toVdom( container ) ).toMatchVNode(
h( 'div', null, [
h( 'div', null, [ 'Test 1' ] ),
h( 'div', null, [ 'Test 2' ] ),
] )
);
expect( container.outerHTML ).toBe(
''
);
} );
it( 'should remove comment nodes and continue to their parents', () => {
const container = createElementFromHTML(
''
);
expect( toVdom( container ) ).toMatchVNode(
h( 'div', null, [
h( 'div', null, [ 'Test 1' ] ),
h( 'div', null, [ 'Test 2' ] ),
] )
);
expect( container.outerHTML ).toBe(
''
);
} );
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(
`Test `
);
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 ``
* content. Elements without the directive are skipped.
*
* @param vnode The vDOM to read.
* @return The namespaces.
*/
function testNamespaces( vnode: any ): Array< string | null > {
const found: Array< string | null > = [];
const visit = ( node: any ) => {
if ( ! node || typeof node !== 'object' ) {
return;
}
const entries = node.props.__directives?.test;
if ( entries ) {
found.push( entries[ 0 ].namespace );
}
[
...( node.props.content ?? [] ),
...( node.props.children ?? [] ),
].forEach( visit );
};
visit( vnode );
return found;
}
afterEach( () => {
document.body.innerHTML = '';
} );
it( 'should resolve the unprefixed directives of the root and of its template content against the given namespace', () => {
const root = attach(
``,
'#root'
);
expect( testNamespaces( toVdom( root, 'given' ) ) ).toEqual( [
'given',
'given',
'given',
] );
} );
it( 'should let a data-wp-interactive on the root or inside it and a namespace prefix take precedence over the given namespace', () => {
const root = attach(
` `,
'#root'
);
expect( testNamespaces( toVdom( root, 'given' ) ) ).toEqual( [
'own',
'inside',
'prefix',
'own',
] );
document.body.innerHTML = '';
const inner = attach(
``,
'#root'
);
expect( testNamespaces( toVdom( inner, 'given' ) ) ).toEqual( [
'given',
'inside',
'given',
] );
document.body.innerHTML = '';
const prefixed = attach(
` `,
'#root'
);
expect( testNamespaces( toVdom( prefixed, 'given' ) ) ).toEqual( [
'prefix',
] );
} );
it( 'should resolve the unprefixed directives against no namespace when null is given', () => {
const root = attach(
`
`,
'#root'
);
expect( testNamespaces( toVdom( root, null ) ) ).toEqual( [
null,
null,
] );
} );
it( 'should not read the ancestors of the root when no namespace is given', () => {
const root = attach(
``,
'#root'
);
expect( testNamespaces( toVdom( root ) ) ).toEqual( [
null,
null,
] );
// Neither does the given namespace come from them.
expect( testNamespaces( toVdom( root, 'given' ) ) ).toEqual( [
'given',
'given',
] );
} );
it( 'should convert islands as before when no namespace is given', () => {
const island = attach(
``,
'#root'
);
expect( testNamespaces( toVdom( island ) ) ).toEqual( [
'own',
'own',
] );
} );
it( 'should not leak the given namespace into a following conversion without one', () => {
const root = attach(
` `,
'#root'
);
expect( testNamespaces( toVdom( root, 'given' ) ) ).toEqual( [
'given',
] );
expect( testNamespaces( toVdom( root ) ) ).toEqual( [ null ] );
} );
} );
describe( 'getInteractiveNamespace', () => {
it.each( [
[ 'a string value', 'my-plugin', 'my-plugin' ],
[
'an object with a namespace string',
'{ "namespace": "object-ns", "state": {} }',
'object-ns',
],
[ 'an object without a namespace', '{ "state": {} }', null ],
[
'an object with a non-string namespace',
'{ "namespace": 1 }',
null,
],
[ 'a reserved string', 'constructor', null ],
[
'an object with a reserved namespace',
'{ "namespace": "__proto__" }',
null,
],
] )(
'should give the namespace that toVdom() applies for %s',
( _description, attributeValue, expected ) => {
const { value } = parseDirectiveValue( attributeValue );
expect( getInteractiveNamespace( value ) ).toBe( expected );
const root = createElementFromHTML(
``
);
const entries = ( toVdom( root ) as any ).props.children[ 0 ]
.props.__directives.test;
expect( entries[ 0 ].namespace ).toBe( expected );
}
);
} );
describe( 'Suffixes and Unique IDs', () => {
it( 'should parse directives with suffixes only', () => {
const element = createElementFromHTML(
`
`
);
expect( toVdom( element ) ).toMatchVNode(
h(
'div' as any,
{
'data-wp-test--one': 'test value 1',
'data-wp-test--two': 'test value 2',
__directives: {
test: [
{
namespace: null,
value: 'test value 1',
suffix: 'one',
uniqueId: null,
},
{
namespace: null,
value: 'test value 2',
suffix: 'two',
uniqueId: null,
},
],
},
},
[]
)
);
} );
it( 'should parse directive with unique ID only', () => {
const element = createElementFromHTML(
`
`
);
expect( toVdom( element ) ).toMatchVNode(
h(
'div' as any,
{
'data-wp-test---unique-id': 'test value',
__directives: {
test: [
{
namespace: null,
value: 'test value',
suffix: null,
uniqueId: 'unique-id',
},
],
},
},
[]
)
);
} );
it( 'should parse directive with suffix and unique ID', () => {
const element = createElementFromHTML(
`
`
);
expect( toVdom( element ) ).toMatchVNode(
h(
'div' as any,
{
'data-wp-test--suffix---unique-id': 'test value',
__directives: {
test: [
{
namespace: null,
value: 'test value',
suffix: 'suffix',
uniqueId: 'unique-id',
},
],
},
},
[]
)
);
} );
it( 'should handle multiple directives with different unique IDs', () => {
const element = createElementFromHTML(
`
`
);
expect( toVdom( element ) ).toMatchVNode(
h(
'div' as any,
{
'data-wp-test---plugin-a': 'value-a',
'data-wp-test---plugin-b': 'value-b',
'data-wp-test---plugin-c': 'value-c',
__directives: {
test: [
{
namespace: null,
value: 'value-a',
suffix: null,
uniqueId: 'plugin-a',
},
{
namespace: null,
value: 'value-b',
suffix: null,
uniqueId: 'plugin-b',
},
{
namespace: null,
value: 'value-c',
suffix: null,
uniqueId: 'plugin-c',
},
],
},
},
[]
)
);
} );
it( 'should handle mix of different suffixes and unique IDs', () => {
const element = createElementFromHTML(
`
`
);
expect( toVdom( element ) ).toMatchVNode(
h(
'div' as any,
{
'data-wp-test--suffix-a---id-1': 'value1',
'data-wp-test--suffix-a---id-2': 'value2',
'data-wp-test--suffix-b---id-1': 'value3',
'data-wp-test--suffix-c---id-1': 'value4',
__directives: {
test: [
{
namespace: null,
value: 'value1',
suffix: 'suffix-a',
uniqueId: 'id-1',
},
{
namespace: null,
value: 'value2',
suffix: 'suffix-a',
uniqueId: 'id-2',
},
{
namespace: null,
value: 'value3',
suffix: 'suffix-b',
uniqueId: 'id-1',
},
{
namespace: null,
value: 'value4',
suffix: 'suffix-c',
uniqueId: 'id-1',
},
],
},
},
[]
)
);
} );
it( 'should handle unique ID with namespace', () => {
const element = createElementFromHTML(
`
`
);
expect( toVdom( element ) ).toMatchVNode(
h(
'div' as any,
{
'data-wp-test---unique-id': 'my-namespace::test value',
__directives: {
test: [
{
namespace: 'my-namespace',
value: 'test value',
suffix: null,
uniqueId: 'unique-id',
},
],
},
},
[]
)
);
} );
it( 'should handle multiple directives with different namespaces and unique IDs', () => {
const element = createElementFromHTML(
`
`
);
expect( toVdom( element ) ).toMatchVNode(
h(
'div' as any,
{
'data-wp-test---id-a': 'namespace-a::value1',
'data-wp-test---id-b': 'namespace-b::value2',
__directives: {
test: [
{
namespace: 'namespace-a',
value: 'value1',
suffix: null,
uniqueId: 'id-a',
},
{
namespace: 'namespace-b',
value: 'value2',
suffix: null,
uniqueId: 'id-b',
},
],
},
},
[]
)
);
} );
it( 'should handle empty suffix (just two dashes)', () => {
const element = createElementFromHTML(
`
`
);
expect( toVdom( element ) ).toMatchVNode(
h(
'div' as any,
{
'data-wp-test--': 'test value',
__directives: {
test: [
{
namespace: null,
value: 'test value',
suffix: null,
uniqueId: null,
},
],
},
},
[]
)
);
} );
it( 'should handle empty unique ID (just three dashes)', () => {
const element = createElementFromHTML(
`
`
);
expect( toVdom( element ) ).toMatchVNode(
h(
'div' as any,
{
'data-wp-test---': 'test value',
__directives: {
test: [
{
namespace: null,
value: 'test value',
suffix: null,
uniqueId: null,
},
],
},
},
[]
)
);
} );
it( 'should handle only dashes (4 or more dashes)', () => {
const element = createElementFromHTML(
`
`
);
expect( toVdom( element ) ).toMatchVNode(
h(
'div' as any,
{
'data-wp-test----': 'test value',
__directives: {
test: [
{
namespace: null,
value: 'test value',
suffix: '--',
uniqueId: null,
},
],
},
},
[]
)
);
} );
it( 'should handle suffix starting with 4 or more dashes but containing valid characters', () => {
const element = createElementFromHTML(
`
`
);
expect( toVdom( element ) ).toMatchVNode(
h(
'div' as any,
{
'data-wp-test------custom-suffix': 'test value',
__directives: {
test: [
{
namespace: null,
value: 'test value',
suffix: '----custom-suffix',
uniqueId: null,
},
],
},
},
[]
)
);
} );
it( 'should handle complex pattern with multiple dashes', () => {
const element = createElementFromHTML(
`
`
);
expect( toVdom( element ) ).toMatchVNode(
h(
'div' as any,
{
'data-wp-test--complex--suffix---complex--unique---id':
'test value',
__directives: {
test: [
{
namespace: null,
value: 'test value',
suffix: 'complex--suffix',
uniqueId: 'complex--unique---id',
},
],
},
},
[]
)
);
} );
it( 'should handle suffix with dashes followed by unique ID', () => {
const element = createElementFromHTML(
`
`
);
expect( toVdom( element ) ).toMatchVNode(
h(
'div' as any,
{
'data-wp-test----suffix---unique-id': 'test value',
__directives: {
test: [
{
namespace: null,
value: 'test value',
suffix: '--suffix',
uniqueId: 'unique-id',
},
],
},
},
[]
)
);
} );
it( 'should handle unique IDs followed by suffix in wrong order', () => {
const element = createElementFromHTML(
`
`
);
expect( toVdom( element ) ).toMatchVNode(
h(
'div' as any,
{
'data-wp-test---unique-id--wrong-suffix': 'test value',
__directives: {
test: [
{
namespace: null,
value: 'test value',
suffix: null,
uniqueId: 'unique-id--wrong-suffix', // After the three dashes, everything is the unique ID.
},
],
},
},
[]
)
);
} );
it( 'should sort directives by suffix and uniqueId for stable ordering', () => {
const element = createElementFromHTML(
`
`
);
const vnode = toVdom( element ) as any;
const directives = vnode.props.__directives.test;
expect(
directives.map( ( d: any ) => [ d.suffix, d.uniqueId ] )
).toEqual( [
[ null, null ],
[ null, 'a' ],
[ null, 'z' ],
[ 'a', null ],
[ 'b', 'a' ],
[ 'b', 'z' ],
] );
} );
} );
} );
describe( 'HTML fallback snapshots', () => {
it( 'copies non-HTML template elements with their child nodes', () => {
const fallback = createElementFromHTML(
'Static
'
);
const snapshot = getHtmlFallback( toVdom( fallback ) as VNode< any > )!;
const svg = snapshot.firstChild as SVGElement;
const template = svg.firstChild as SVGElement;
expect( svg.namespaceURI ).toBe( 'http://www.w3.org/2000/svg' );
expect( template.localName ).toBe( 'template' );
expect( template.namespaceURI ).toBe( 'http://www.w3.org/2000/svg' );
expect( template.childNodes ).toHaveLength( 1 );
expect( template.firstChild ).toMatchObject( {
localName: 'text',
namespaceURI: 'http://www.w3.org/2000/svg',
} );
expect(
( template.firstChild as SVGElement ).firstChild
).toMatchObject( {
nodeType: Node.TEXT_NODE,
nodeValue: 'Static',
} );
} );
it( 'uses the live document template-contents owner for every copied node', () => {
const fallbackMarkup = `fallback
`;
const templateContentsDocument =
document.createElement( 'template' ).content.ownerDocument;
const liveSource = createElementFromHTML( fallbackMarkup );
const sourceTemplate = document.createElement( 'template' );
sourceTemplate.innerHTML = fallbackMarkup;
const templateSource =
sourceTemplate.content.querySelector( '[data-wp-html]' )!;
const parsedDocument = new DOMParser().parseFromString(
fallbackMarkup,
'text/html'
);
const parsedSource = parsedDocument.querySelector( '[data-wp-html]' )!;
const snapshots = [ liveSource, templateSource, parsedSource ].map(
( source ) => getHtmlFallback( toVdom( source ) as VNode< any > )!
);
// The inputs exercise the live, template-contents, and parsed-page docs.
expect( liveSource.ownerDocument ).toBe( document );
expect( templateSource.ownerDocument ).toBe( templateContentsDocument );
expect( parsedSource.ownerDocument ).not.toBe(
templateContentsDocument
);
expect(
parsedDocument.createElement( 'template' ).content.ownerDocument
).not.toBe( templateContentsDocument );
const snapshotOwnerDocuments = new Set< Document >();
/**
* Collects owner documents from a snapshot node and nested template contents.
*
* @param node Snapshot node to inspect.
*/
function collectTemplateContentsOwners( node: Node ): void {
snapshotOwnerDocuments.add( node.ownerDocument! );
if (
node.nodeType === 1 &&
( node as Element ).localName === 'template'
) {
const contents = ( node as HTMLTemplateElement ).content;
snapshotOwnerDocuments.add( contents.ownerDocument );
for ( const child of contents.childNodes ) {
collectTemplateContentsOwners( child );
}
}
for ( const child of node.childNodes ) {
collectTemplateContentsOwners( child );
}
}
for ( const snapshot of snapshots ) {
collectTemplateContentsOwners( snapshot );
}
expect( snapshotOwnerDocuments ).toEqual(
new Set( [ templateContentsDocument ] )
);
} );
} );