2023-11-21 10:46:15 +00:00
|
|
|
// @ts-nocheck
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-01-26 11:39:25 +00:00
|
|
|
import { h } from 'preact';
|
2023-11-21 10:46:15 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-02-20 16:48:33 +00:00
|
|
|
import { directivePrefix as p } from './constants';
|
|
|
|
|
2023-06-27 10:22:12 +00:00
|
|
|
const ignoreAttr = `data-${ p }-ignore`;
|
|
|
|
const islandAttr = `data-${ p }-interactive`;
|
|
|
|
const fullPrefix = `data-${ p }-`;
|
2023-11-21 10:46:15 +00:00
|
|
|
let namespace = null;
|
2023-06-27 10:22:12 +00:00
|
|
|
|
|
|
|
// Regular expression for directive parsing.
|
|
|
|
const directiveParser = new RegExp(
|
|
|
|
`^data-${ p }-` + // ${p} must be a prefix string, like 'wp'.
|
|
|
|
// Match alphanumeric characters including hyphen-separated
|
|
|
|
// segments. It excludes underscore intentionally to prevent confusion.
|
|
|
|
// E.g., "custom-directive".
|
|
|
|
'([a-z0-9]+(?:-[a-z0-9]+)*)' +
|
|
|
|
// (Optional) Match '--' followed by any alphanumeric charachters. It
|
|
|
|
// excludes underscore intentionally to prevent confusion, but it can
|
|
|
|
// contain multiple hyphens. E.g., "--custom-prefix--with-more-info".
|
2023-09-22 21:26:36 +00:00
|
|
|
'(?:--([a-z0-9_-]+))?$',
|
2023-06-27 10:22:12 +00:00
|
|
|
'i' // Case insensitive.
|
|
|
|
);
|
2023-01-26 11:39:25 +00:00
|
|
|
|
2023-11-21 10:46:15 +00:00
|
|
|
// Regular expression for reference parsing. It can contain a namespace before
|
|
|
|
// the reference, separated by `::`, like `some-namespace::state.somePath`.
|
|
|
|
// Namespaces can contain any alphanumeric characters, hyphens, underscores or
|
|
|
|
// forward slashes. References don't have any restrictions.
|
|
|
|
const nsPathRegExp = /^([\w-_\/]+)::(.+)$/;
|
|
|
|
|
2023-01-26 11:39:25 +00:00
|
|
|
export const hydratedIslands = new WeakSet();
|
|
|
|
|
2023-06-27 10:22:12 +00:00
|
|
|
// Recursive function that transforms a DOM tree into vDOM.
|
|
|
|
export function toVdom( root ) {
|
|
|
|
const treeWalker = document.createTreeWalker(
|
|
|
|
root,
|
|
|
|
205 // ELEMENT + TEXT + COMMENT + CDATA_SECTION + PROCESSING_INSTRUCTION
|
|
|
|
);
|
2023-01-26 11:39:25 +00:00
|
|
|
|
2023-06-27 10:22:12 +00:00
|
|
|
function walk( node ) {
|
|
|
|
const { attributes, nodeType } = node;
|
|
|
|
|
|
|
|
if ( nodeType === 3 ) return [ node.data ];
|
|
|
|
if ( nodeType === 4 ) {
|
|
|
|
const next = treeWalker.nextSibling();
|
2023-11-21 10:46:15 +00:00
|
|
|
node.replaceWith( new window.Text( node.nodeValue ) );
|
2023-06-27 10:22:12 +00:00
|
|
|
return [ node.nodeValue, next ];
|
|
|
|
}
|
|
|
|
if ( nodeType === 8 || nodeType === 7 ) {
|
|
|
|
const next = treeWalker.nextSibling();
|
|
|
|
node.remove();
|
|
|
|
return [ null, next ];
|
|
|
|
}
|
2023-01-26 11:39:25 +00:00
|
|
|
|
2023-06-27 10:22:12 +00:00
|
|
|
const props = {};
|
|
|
|
const children = [];
|
2023-11-21 10:46:15 +00:00
|
|
|
const directives = [];
|
2023-06-27 10:22:12 +00:00
|
|
|
let ignore = false;
|
|
|
|
let island = false;
|
|
|
|
|
|
|
|
for ( let i = 0; i < attributes.length; i++ ) {
|
|
|
|
const n = attributes[ i ].name;
|
|
|
|
if (
|
|
|
|
n[ fullPrefix.length ] &&
|
|
|
|
n.slice( 0, fullPrefix.length ) === fullPrefix
|
|
|
|
) {
|
|
|
|
if ( n === ignoreAttr ) {
|
|
|
|
ignore = true;
|
|
|
|
} else {
|
2023-11-21 10:46:15 +00:00
|
|
|
let [ ns, value ] = nsPathRegExp
|
|
|
|
.exec( attributes[ i ].value )
|
|
|
|
?.slice( 1 ) ?? [ null, attributes[ i ].value ];
|
2023-06-27 10:22:12 +00:00
|
|
|
try {
|
2023-11-21 10:46:15 +00:00
|
|
|
value = JSON.parse( value );
|
2023-06-27 10:22:12 +00:00
|
|
|
} catch ( e ) {}
|
2023-11-21 10:46:15 +00:00
|
|
|
if ( n === islandAttr ) {
|
|
|
|
island = true;
|
|
|
|
namespace = value?.namespace ?? null;
|
|
|
|
} else {
|
|
|
|
directives.push( [ n, ns, value ] );
|
|
|
|
}
|
2023-06-27 10:22:12 +00:00
|
|
|
}
|
|
|
|
} else if ( n === 'ref' ) {
|
|
|
|
continue;
|
2023-01-26 11:39:25 +00:00
|
|
|
}
|
|
|
|
props[ n ] = attributes[ i ].value;
|
|
|
|
}
|
|
|
|
|
2023-06-27 10:22:12 +00:00
|
|
|
if ( ignore && ! island )
|
|
|
|
return [
|
|
|
|
h( node.localName, {
|
|
|
|
...props,
|
|
|
|
innerHTML: node.innerHTML,
|
|
|
|
__directives: { ignore: true },
|
|
|
|
} ),
|
|
|
|
];
|
|
|
|
if ( island ) hydratedIslands.add( node );
|
2023-01-26 11:39:25 +00:00
|
|
|
|
2023-11-21 10:46:15 +00:00
|
|
|
if ( directives.length ) {
|
|
|
|
props.__directives = directives.reduce(
|
|
|
|
( obj, [ name, ns, value ] ) => {
|
|
|
|
const [ , prefix, suffix = 'default' ] =
|
|
|
|
directiveParser.exec( name );
|
|
|
|
if ( ! obj[ prefix ] ) obj[ prefix ] = [];
|
|
|
|
obj[ prefix ].push( {
|
|
|
|
namespace: ns ?? namespace,
|
|
|
|
value,
|
|
|
|
suffix,
|
|
|
|
} );
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
}
|
2023-01-26 11:39:25 +00:00
|
|
|
|
2023-12-22 00:34:28 +00:00
|
|
|
if ( node.localName === 'template' ) {
|
|
|
|
props.content = [ ...node.content.childNodes ].map( ( childNode ) =>
|
|
|
|
toVdom( childNode )
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
let child = treeWalker.firstChild();
|
|
|
|
if ( child ) {
|
|
|
|
while ( child ) {
|
|
|
|
const [ vnode, nextChild ] = walk( child );
|
|
|
|
if ( vnode ) children.push( vnode );
|
|
|
|
child = nextChild || treeWalker.nextSibling();
|
|
|
|
}
|
|
|
|
treeWalker.parentNode();
|
2023-06-27 10:22:12 +00:00
|
|
|
}
|
2023-01-26 11:39:25 +00:00
|
|
|
}
|
2023-06-27 10:22:12 +00:00
|
|
|
|
|
|
|
return [ h( node.localName, props, children ) ];
|
2023-01-26 11:39:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 10:22:12 +00:00
|
|
|
return walk( treeWalker.currentNode );
|
2023-01-26 11:39:25 +00:00
|
|
|
}
|