Skip to content

Commit

Permalink
Fixed a truly embarrassing correctness bug. Don't really know how I t…
Browse files Browse the repository at this point in the history
…hought the previous version would have worked.
  • Loading branch information
aickin committed Dec 4, 2017
1 parent f3dae68 commit 105e925
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
26 changes: 10 additions & 16 deletions packages/react/src/ReactChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,16 @@ const escapeRegex = /[=:]/g;
function escape(key: string): string {
const str = '' + key;

const match = escapeRegex.exec(str);

// shortcut optimization for keys that don't have ":" or "="
if (!match) {
return '$' + str;
}

let escapeChar;
let result = '$';
let index = 0;
let lastIndex = 0;

for (index = match.index; index < str.length; index++) {
switch (str.charCodeAt(index)) {
for (
let match = escapeRegex.exec(str);
match;
match = escapeRegex.exec(str)
) {
switch (str.charCodeAt(match.index)) {
case 61: // =
escapeChar = '=0';
break;
Expand All @@ -56,15 +52,13 @@ function escape(key: string): string {
continue;
}

if (lastIndex !== index) {
result += str.substring(lastIndex, index);
if (lastIndex !== match.index) {
result += str.substring(lastIndex, match.index);
}
lastIndex = index + 1;
lastIndex = match.index + 1;
result += escapeChar;
}
return lastIndex !== index
? result + str.substring(lastIndex, index)
: result;
return result + str.substring(lastIndex);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions packages/react/src/__tests__/ReactChildren-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -911,10 +911,20 @@ describe('ReactChildren', () => {
it('should escape keys', () => {
const zero = <div key="1" />;
const one = <div key="1=::=2" />;
const two = <div key="=foo" />;
const three = <div key="foo=" />;
const four = <div key=":foo" />;
const five = <div key="foo:" />;
const six = <div key="foo" />;
const instance = (
<div>
{zero}
{one}
{two}
{three}
{four}
{five}
{six}
</div>
);
const mappedChildren = React.Children.map(
Expand All @@ -924,6 +934,11 @@ describe('ReactChildren', () => {
expect(mappedChildren).toEqual([
<div key=".$1" />,
<div key=".$1=0=2=2=02" />,
<div key=".$=0foo" />,
<div key=".$foo=0" />,
<div key=".$=2foo" />,
<div key=".$foo=2" />,
<div key=".$foo" />,
]);
});

Expand Down

0 comments on commit 105e925

Please sign in to comment.