Skip to content

Commit

Permalink
[Fix] Do horrible things with regex to fix ID selectors.
Browse files Browse the repository at this point in the history
Fixes #495.
  • Loading branch information
ljharb committed Nov 9, 2016
1 parent 9c634b8 commit f979d99
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -58,7 +58,8 @@
"lodash": "^4.16.4",
"object-is": "^1.0.1",
"object.assign": "^4.0.4",
"object.values": "^1.0.3"
"object.values": "^1.0.3",
"uuid": "^2.0.3"
},
"devDependencies": {
"babel-cli": "^6.18.0",
Expand Down
20 changes: 19 additions & 1 deletion src/Utils.js
Expand Up @@ -2,6 +2,7 @@
import isEqual from 'lodash/isEqual';
import React from 'react';
import is from 'object-is';
import uuid from 'uuid';
import functionName from 'function.prototype.name';
import {
isDOMComponent,
Expand Down Expand Up @@ -171,7 +172,24 @@ export function withSetStateAllowed(fn) {
}

export function splitSelector(selector) {
return selector.split(/(?=\.|\[.*])|(?=#|\[#.*])/);
// step 1: make a map of all quoted strings with a uuid
const quotedSegments = selector.split(/[^" ]+|("[^"]*")|.*/g)
.filter(Boolean)
.reduce((obj, match) => ({ ...obj, [match]: uuid.v4() }), {});

return selector
// step 2: replace all quoted strings with the uuid, so we don't have to properly parse them
.replace(/[^" ]+|("[^"]*")|.*/g, x => quotedSegments[x] || x)
// step 3: split as best we can without a proper parser
.split(/(?=\.|\[.*])|(?=#|\[#.*])/)
// step 4: restore the quoted strings by swapping back the uuid's for the original segments
.map((selectorSegment) => {
let restoredSegment = selectorSegment;
Object.entries(quotedSegments).forEach(([k, v]) => {
restoredSegment = restoredSegment.replace(v, k);
});
return restoredSegment;
});
}


Expand Down

0 comments on commit f979d99

Please sign in to comment.