Skip to content

Commit 28bacb1

Browse files
committed
fix: look up quoted names with brackets, semicolons or quotes as written
Quoted names containing a bracket, a semicolon, a caret, an apostrophe (inside double quotes), a leading double quote or at sign, or a hash followed by a digit were not looked up as written. toPathArray splits the expression at those characters before the name is read, so $['cd[DataLayer]'], $["it's"], $['x;y'] and $['@id'] found nothing or threw, and the result paths for such properties could not be fed back into a query. A single quoted name with one of those characters is now kept intact in toPathArray, with a leading backtick (the existing escape) so that it is looked up as it is. Other names take the same route as before. Fixes #167, fixes #203, fixes #225 and fixes #258. Also covers the quoted forms in #173.
1 parent 970bdb2 commit 28bacb1

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

‎src/jsonpath.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,22 @@ JSONPath.toPathArray = function (expr) {
12511251
return '[#' + (subx.push($1) - 1) + ']';
12521252
}
12531253
)
1254+
// Quoted property names containing characters which would otherwise
1255+
// be read as path syntax (e.g., `['a[b]']`), kept intact with a
1256+
// leading backtick escape so they are looked up as they are
1257+
.replaceAll(
1258+
/\[\s*(?:'([^']*)'|"([^"]*)")\s*\]/gv,
1259+
function ($0, single, double) {
1260+
const name = single ?? double;
1261+
if (!(/(?:^[@"])|[\[\];^']|#\d/v).test(name)) {
1262+
return $0;
1263+
}
1264+
const escaped = name[0] === '`' ? name : '`' + name;
1265+
// eslint-disable-next-line @stylistic/max-len -- Long
1266+
// eslint-disable-next-line unicorn/no-return-array-push -- Optimization
1267+
return '[#' + (subx.push(escaped) - 1) + ']';
1268+
}
1269+
)
12541270
// Escape periods and tildes within properties
12551271
.replaceAll(/\[['"]([^'\]]*)['"]\]/gv, function ($0, prop) {
12561272
return "['" + prop

‎test/test.properties.js‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,55 @@ checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) {
9191
}
9292
});
9393

94+
it('Quoted property names with path syntax characters', () => {
95+
const names = [
96+
'cd[DataLayer]', '[', ']', 'phoneNumbers [1]', 'x;y', 'a^b',
97+
"it's", "a'", "'lastName'", '"lastName"', 'say "hi"', '@id'
98+
];
99+
for (const name of names) {
100+
const data = {[name]: {y: 'Y'}, x: {[name]: 'N'}, z: 1};
101+
/**
102+
* @param {string|string[]} path
103+
* @param {"value"|"path"} [resultType]
104+
* @param {boolean} [wrap]
105+
* @returns {unknown}
106+
*/
107+
const query = (path, resultType, wrap = true) => jsonpath({
108+
json: data, path, resultType, wrap
109+
});
110+
const quotedNames = /** @type {string[]} */ ([]);
111+
if (!name.includes("'")) {
112+
quotedNames.push(`'${name}'`);
113+
}
114+
if (!name.includes('"')) {
115+
quotedNames.push(`"${name}"`);
116+
}
117+
for (const quoted of quotedNames) {
118+
const msg = quoted;
119+
assert.deepEqual(query(`$[${quoted}]`), [{y: 'Y'}], msg);
120+
assert.deepEqual(query(`$[${quoted}].y`), ['Y'], msg);
121+
assert.deepEqual(query(`$.x[${quoted}]`), ['N'], msg);
122+
assert.deepEqual(
123+
query(`$..[${quoted}]`), [{y: 'Y'}, 'N'], msg
124+
);
125+
// A single name is not an array expression
126+
assert.strictEqual(
127+
query(`$[${quoted}].y`, 'value', false), 'Y', msg
128+
);
129+
}
130+
// Result paths and array paths find the name again
131+
const [path] = /** @type {string[]} */ (
132+
query('$.*.y', 'path')
133+
);
134+
assert.deepEqual(query(path), ['Y'], path);
135+
assert.deepEqual(query(['$', name, 'y']), ['Y'], name);
136+
}
137+
// A leading backtick still escapes the rest of the name
138+
assert.deepEqual(jsonpath({
139+
json: {'a[b]': 1, '`a[b]': 2}, path: "$['`a[b]']"
140+
}), [1]);
141+
});
142+
94143
it('At signs within properties', () => {
95144
let result = jsonpath({json, path: "$.datafield[?(@.tag=='035')]", wrap: false});
96145
assert.deepEqual(result, [json.datafield[0]]);

0 commit comments

Comments
 (0)