Skip to content

Commit

Permalink
Hooks: Allow slashes in the namespace (#47)
Browse files Browse the repository at this point in the history
* Hooks: Allow slashes in the namespace

* Hooks: Add test ensuring backlash is not allowed inside namespace
  • Loading branch information
gziolo committed Nov 28, 2017
1 parent 058ca22 commit 2d1f478
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
30 changes: 20 additions & 10 deletions packages/hooks/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,7 @@ test( 'cannot add filters with empty-string namespaces', () => {
test( 'cannot add filters with invalid namespaces', () => {
addFilter( 'hook_name', 'invalid_%&name', () => null );
expect( console.error ).toHaveBeenCalledWith(
'The namespace can only contain numbers, letters, dashes, periods and underscores.'
);
} );

test( 'cannot add filters with namespaces missing a functionDescription', () => {
addFilter( 'hook_name', 'invalid_name/', () => null );
expect( console.error ).toHaveBeenCalledWith(
'The namespace can only contain numbers, letters, dashes, periods and underscores.'
'The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.'
);
} );

Expand All @@ -164,7 +157,17 @@ test( 'Can add filters with dashes in namespaces', () => {
} );

test( 'Can add filters with capitals in namespaces', () => {
addFilter( 'hook_name', 'my_name-OhNoaction', () => null );
addFilter( 'hook_name', 'My_Name-OhNoaction', () => null );
expect( console.error ).toHaveBeenCalledTimes( 0 );
} );

test( 'Can add filters with slashes in namespaces', () => {
addFilter( 'hook_name', 'my/name/action', () => null );
expect( console.error ).toHaveBeenCalledTimes( 0 );
} );

test( 'Can add filters with periods in namespaces', () => {
addFilter( 'hook_name', 'my.name.action', () => null );
expect( console.error ).toHaveBeenCalledTimes( 0 );
} );

Expand All @@ -186,7 +189,14 @@ test( 'Can add filters with periods in hookName', () => {
test( 'cannot add filters with invalid namespaces', () => {
addFilter( 'hook_name', '/invalid_name', () => null );
expect( console.error ).toHaveBeenCalledWith(
'The namespace can only contain numbers, letters, dashes, periods and underscores.'
'The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.'
);
} );

test( 'cannot add filters with namespace containing backslash', () => {
addFilter( 'hook_name', 'i\n\v\a\l\i\d\n\a\m\e', () => null );
expect( console.error ).toHaveBeenCalledWith(
'The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.'
);
} );

Expand Down
4 changes: 2 additions & 2 deletions packages/hooks/src/validateNamespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function validateNamespace( namespace ) {
return false;
}

if ( ! /^[a-zA-Z][a-zA-Z0-9_.\-]*$/.test( namespace ) ) {
console.error( 'The namespace can only contain numbers, letters, dashes, periods and underscores.' );
if ( ! /^[a-zA-Z][a-zA-Z0-9_.\-\/]*$/.test( namespace ) ) {
console.error( 'The namespace can only contain numbers, letters, dashes, periods, underscores and slashes.' );
return false;
}

Expand Down

0 comments on commit 2d1f478

Please sign in to comment.