Skip to content

Commit

Permalink
[React DevTools] Fix regex for formateWithStyles function (#24486)
Browse files Browse the repository at this point in the history
The previous regex to detect string substitutions is not quite right, this PR fixes it by:

Check to make sure we are starting either at the beginning of the line or we match a character that's not % to make sure we capture all the % in a row.
Make sure there are an odd number of % (the first X pairs are escaped % characters. The odd % followed by a letter is the string substitution)
  • Loading branch information
lunaruan committed May 3, 2022
1 parent 6cbf0f7 commit c7e494b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/react-devtools-shared/src/backend/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/**
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
Expand Down Expand Up @@ -188,7 +189,7 @@ export function formatWithStyles(
}

// Matches any of %(o|O|d|i|s|f), but not %%(o|O|d|i|s|f)
const REGEXP = /([^%]|^)(%([oOdisf]))/g;
const REGEXP = /([^%]|^)((%%)*)(%([oOdisf]))/g;
if (inputArgs[0].match(REGEXP)) {
return [`%c${inputArgs[0]}`, style, ...inputArgs.slice(1)];
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-devtools-shared/src/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export function installHook(target: any): DevToolsHook | null {
}

// Matches any of %(o|O|d|i|s|f), but not %%(o|O|d|i|s|f)
const REGEXP = /([^%]|^)(%([oOdisf]))/g;
const REGEXP = /([^%]|^)((%%)*)(%([oOdisf]))/g;
if (inputArgs[0].match(REGEXP)) {
return [`%c${inputArgs[0]}`, style, ...inputArgs.slice(1)];
} else {
Expand Down

0 comments on commit c7e494b

Please sign in to comment.