Skip to content

Commit

Permalink
Interactivity API: Turn named capturing groups back into numbered one…
Browse files Browse the repository at this point in the history
…s inside `toVdom` (WordPress#61728)

* Back to numbered capturing groups

* Change attrValue to attributeValue

* Move `attributeValue` initialization

* Change `namespaceAndPath` to `regexResult`


Co-authored-by: DAreRodz <darerodz@git.wordpress.org>
Co-authored-by: sirreal <jonsurrell@git.wordpress.org>
  • Loading branch information
3 people authored and bph committed May 20, 2024
1 parent 4046041 commit 6d49f5c
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions packages/interactivity/src/vdom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const directiveParser = new RegExp(
// 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 = /^(?<namespace>[\w_\/-]+)::(?<value>.+)$/;
const nsPathRegExp = /^([\w_\/-]+)::(.+)$/;

export const hydratedIslands = new WeakSet();

Expand Down Expand Up @@ -88,19 +88,17 @@ export function toVdom( root: Node ): Array< ComponentChild > {

for ( let i = 0; i < attributes.length; i++ ) {
const attributeName = attributes[ i ].name;
const attributeValue = attributes[ i ].value;
if (
attributeName[ fullPrefix.length ] &&
attributeName.slice( 0, fullPrefix.length ) === fullPrefix
) {
if ( attributeName === ignoreAttr ) {
ignore = true;
} else {
const regexCaptureGroups = nsPathRegExp.exec(
attributes[ i ].value
)?.groups;
const namespace = regexCaptureGroups?.namespace ?? null;
let value: any =
regexCaptureGroups?.value ?? attributes[ i ].value;
const regexResult = nsPathRegExp.exec( attributeValue );
const namespace = regexResult?.[ 1 ] ?? null;
let value: any = regexResult?.[ 2 ] ?? attributeValue;
try {
value = value && JSON.parse( value );
} catch ( e ) {}
Expand All @@ -121,7 +119,7 @@ export function toVdom( root: Node ): Array< ComponentChild > {
} else if ( attributeName === 'ref' ) {
continue;
}
props[ attributeName ] = attributes[ i ].value;
props[ attributeName ] = attributeValue;
}

if ( ignore && ! island ) {
Expand Down

0 comments on commit 6d49f5c

Please sign in to comment.