Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(editor): Fix mapping with special characters #5837

Merged
merged 8 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ export default Vue.extend({
segment.kind === 'plaintext'
? segment.plaintext.length
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
(segment.resolved as any).toString().length;
segment.resolved
? (segment.resolved as any).toString().length
: 0;

segment.to = cursor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export default Vue.extend({
segment.kind === 'plaintext'
? segment.plaintext.length
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
(segment.resolved as any).toString().length;
segment.resolved
? (segment.resolved as any).toString().length
: 0;
segment.to = cursor;
return segment;
})
Expand Down
29 changes: 26 additions & 3 deletions packages/editor-ui/src/utils/__tests__/mappingUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,30 @@ describe('Mapping Utils', () => {
};
const result = getMappedExpression(input);
expect(result).toBe(
'{{ $node.nodeName.json.sample["path with-space"]["path-with-hyphen"] }}',
"{{ $node.nodeName.json.sample['path with-space']['path-with-hyphen'] }}",
);
});

it('should handle paths with special characters', () => {
const input = {
nodeName: 'nodeName',
distanceFromActive: 2,
path: [
'sample',
'"Execute"',
'`Execute`',
"'Execute'",
'[Execute]',
'{Execute}',
'execute?',
'test,',
'test:',
'path.',
],
};
const result = getMappedExpression(input);
expect(result).toBe(
"{{ $node.nodeName.json.sample['\"Execute\"']['`Execute`']['\\'Execute\\'']['[Execute]']['{Execute}']['execute?']['test,']['test:']['path.'] }}",
);
});

Expand All @@ -239,7 +262,7 @@ describe('Mapping Utils', () => {
path: ['propertyName', 'capitalizedName', 'hyphen-prop'],
};
const result = getMappedExpression(input);
expect(result).toBe('{{ $json.propertyName.capitalizedName["hyphen-prop"] }}');
expect(result).toBe("{{ $json.propertyName.capitalizedName['hyphen-prop'] }}");
});

it('should generate a mapped expression with a complex path', () => {
Expand All @@ -250,7 +273,7 @@ describe('Mapping Utils', () => {
};
const result = getMappedExpression(input);
expect(result).toBe(
'{{ $json.propertyName.capitalizedName.stringVal["some-value"].capitalizedProp }}',
"{{ $json.propertyName.capitalizedName.stringVal['some-value'].capitalizedProp }}",
);
});
});
Expand Down
7 changes: 5 additions & 2 deletions packages/editor-ui/src/utils/mappingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ export function generatePath(root: string, path: Array<string | number>): string
return `${accu}[${part}]`;
}

if (part.includes('-') || part.includes(' ') || part.includes('.')) {
return `${accu}["${part}"]`;
const special = ['-', ' ', '.', "'", '"', '`', '[', ']', '{', '}', '(', ')', ':', ',', '?'];
const hasSpecial = !!special.find((s) => part.includes(s));
if (hasSpecial) {
const escaped = part.replaceAll("'", "\\'");
return `${accu}['${escaped}']`;
}

return `${accu}.${part}`;
Expand Down