From 555398d3f522b788e253b61c31d70ba82191c3c3 Mon Sep 17 00:00:00 2001 From: Yanzhen Yu Date: Sat, 21 Nov 2020 11:05:08 +0800 Subject: [PATCH] close #38, update data uri regexp --- src/snapshot.ts | 2 +- test/snapshot.test.ts | 42 +++++++++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/snapshot.ts b/src/snapshot.ts index d13c48d0..ae3e5ad4 100644 --- a/src/snapshot.ts +++ b/src/snapshot.ts @@ -63,7 +63,7 @@ function extractOrigin(url: string): string { const URL_IN_CSS_REF = /url\((?:(')([^']*)'|(")([^"]*)"|([^)]*))\)/gm; const RELATIVE_PATH = /^(?!www\.|(?:http|ftp)s?:\/\/|[A-Za-z]:\\|\/\/).*/; -const DATA_URI = /^(data:)([\w\/\+\-]+);(charset=[\w-]+|base64|utf-?8).*,(.*)/i; +const DATA_URI = /^(data:)([^,]*),(.*)/i; export function absoluteToStylesheet( cssText: string | null, href: string, diff --git a/test/snapshot.test.ts b/test/snapshot.test.ts index 5c3000d0..025fac6c 100644 --- a/test/snapshot.test.ts +++ b/test/snapshot.test.ts @@ -74,11 +74,21 @@ describe('absolute url to stylesheet', () => { it('preserves quotes around inline svgs with spaces', () => { expect( - absoluteToStylesheet("url(\"data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath fill='%2328a745' d='M3'/%3E%3C/svg%3E\")", href), - ).to.equal("url(\"data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath fill='%2328a745' d='M3'/%3E%3C/svg%3E\")"); + absoluteToStylesheet( + "url(\"data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath fill='%2328a745' d='M3'/%3E%3C/svg%3E\")", + href, + ), + ).to.equal( + "url(\"data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath fill='%2328a745' d='M3'/%3E%3C/svg%3E\")", + ); expect( - absoluteToStylesheet('url(\'data:image/svg+xml;utf8,\')', href), - ).to.equal('url(\'data:image/svg+xml;utf8,\')'); + absoluteToStylesheet( + 'url(\'data:image/svg+xml;utf8,\')', + href, + ), + ).to.equal( + 'url(\'data:image/svg+xml;utf8,\')', + ); }); it('can handle empty path', () => { expect(absoluteToStylesheet(`url('')`, href)).to.equal(`url('')`); @@ -87,24 +97,26 @@ describe('absolute url to stylesheet', () => { describe('isBlockedElement()', () => { const subject = (html: string, opt: any = {}) => - _isBlockedElement(render(html), 'rr-block', opt.blockSelector) + _isBlockedElement(render(html), 'rr-block', opt.blockSelector); const render = (html: string): HTMLElement => - JSDOM.fragment(html).querySelector('div')! + JSDOM.fragment(html).querySelector('div')!; it('can handle empty elements', () => { - expect(subject('
')).to.equal(false) - }) + expect(subject('
')).to.equal(false); + }); it('blocks prohibited className', () => { - expect(subject('
')).to.equal(true) - }) + expect(subject('
')).to.equal(true); + }); it('does not block random data selector', () => { - expect(subject('
')).to.equal(false) - }) + expect(subject('
')).to.equal(false); + }); it('blocks blocked selector', () => { - expect(subject('
', { blockSelector: '[data-rr-block]' })).to.equal(true) - }) -}) + expect( + subject('
', { blockSelector: '[data-rr-block]' }), + ).to.equal(true); + }); +});