Skip to content

Commit

Permalink
fix: escaping the URL passed to clip-path to handle parentheses (chan…
Browse files Browse the repository at this point in the history
…ind#217)

Fixing a bug where if the current page has parentheses in the URL, it would cause hanzi-writer to break. Thanks @ cqiangcode for finding this!
  • Loading branch information
chanind authored Feb 1, 2021
1 parent 009be80 commit 9b33224
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
20 changes: 20 additions & 0 deletions jest-jsdom-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Hack to allow us to modify jsdom from within tests
* from https://github.com/facebook/jest/issues/5124#issuecomment-352749005
* */

const JSDOMEnvironment = require('jest-environment-jsdom');

module.exports = class JSDOMEnvironmentGlobal extends JSDOMEnvironment {
constructor(config) {
super(config);

this.global.jsdom = this.dom;
}

teardown() {
this.global.jsdom = null;

return super.teardown();
}
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"rootDir": "src",
"coverageDirectory": "../coverage/",
"testURL": "https://test.com/url#tag",
"collectCoverage": true
"collectCoverage": true,
"testEnvironment": "<rootDir>/../jest-jsdom-env"
}
}
2 changes: 1 addition & 1 deletion src/renderers/svg/__tests__/StrokeRenderer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('StrokeRenderer', () => {
'rgba(12,101,20,0.3)',
);
expect((target.svg.childNodes[1] as Element).getAttribute('clip-path')).toBe(
`url(https://test.com/url#${maskId})`,
`url("https://test.com/url#${maskId}")`,
);

expect(maskPath).toMatchSnapshot();
Expand Down
24 changes: 24 additions & 0 deletions src/renderers/svg/__tests__/svgUtils-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { urlIdRef } from '../svgUtils';

describe('urlIdRef', () => {
it('prepends the ID with the current window location', () => {
(global as any).jsdom.reconfigure({
url: 'https://test.com',
});
expect(urlIdRef('123-blah')).toEqual('url("https://test.com/#123-blah")');
});

it('escapes " characters in the URL', () => {
(global as any).jsdom.reconfigure({
url: 'https://test-"(ok).com',
});
expect(urlIdRef('123-blah')).toEqual('url("https://test-%22(ok).com/#123-blah")');
});

it('strips everything after # in the URL', () => {
(global as any).jsdom.reconfigure({
url: 'https://test.com/path#existing-hash',
});
expect(urlIdRef('123-blah')).toEqual('url("https://test.com/path#123-blah")');
});
});
4 changes: 2 additions & 2 deletions src/renderers/svg/svgUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export function attrs(elm: Element, attrsMap: Record<string, string>) {
export function urlIdRef(id: string) {
let prefix = '';
if (window.location && window.location.href) {
prefix = window.location.href.replace(/#[^#]*$/, '');
prefix = window.location.href.replace(/#[^#]*$/, '').replace(/"/gi, '%22');
}
return `url(${prefix}#${id})`;
return `url("${prefix}#${id}")`;
}

export function removeElm(elm: Element | undefined) {
Expand Down

0 comments on commit 9b33224

Please sign in to comment.