Skip to content

Commit cf548d5

Browse files
committed
fix: display of regex strings as special chars
1 parent 1f06de3 commit cf548d5

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

js/Conditions/conditions.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,25 @@ const App = () => {
4040
const field = document.getElementById( 'feed-post-filters-conditions' );
4141
if ( field && field.value ) {
4242
const parsedConditions = JSON.parse( field.value );
43-
setConditions( parsedConditions && parsedConditions.conditions ? parsedConditions : { conditions: [], match: 'all' } );
43+
if ( parsedConditions && parsedConditions.conditions ) {
44+
parsedConditions.conditions = parsedConditions.conditions.map( condition => {
45+
// We do all these schananigans to make sure we JS doesn't confuse regex for special characters.
46+
if ( typeof condition.value === 'string' ) {
47+
condition.value = condition.value
48+
.replace( /\u0008/g, '\\b' )
49+
.replace( /\u000C/g, '\\f' )
50+
.replace( /\n/g, '\\n' )
51+
.replace( /\r/g, '\\r' )
52+
.replace( /\t/g, '\\t' );
53+
}
54+
return condition;
55+
} );
56+
setConditions( parsedConditions );
57+
} else {
58+
setConditions( { conditions: [], match: 'all' } );
59+
}
60+
} else {
61+
setConditions( { conditions: [], match: 'all' } );
4462
}
4563
}, [] );
4664

0 commit comments

Comments
 (0)