From 639fccae7f814eaa2714354aaa516a85cc8c4ebf Mon Sep 17 00:00:00 2001 From: Hyeseong Kim Date: Fri, 3 Sep 2021 15:46:17 +0900 Subject: [PATCH] fix(server): fix collect to ignore empty class (#832) --- packages/server/__tests__/collect.test.ts | 17 +++++++++++++++++ packages/server/src/collect.ts | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/server/__tests__/collect.test.ts b/packages/server/__tests__/collect.test.ts index ba5208e68..18195fca2 100644 --- a/packages/server/__tests__/collect.test.ts +++ b/packages/server/__tests__/collect.test.ts @@ -233,3 +233,20 @@ describe('include atrule once', () => { test('critical', () => expect(prettyPrint(critical)).toMatchSnapshot()); test('other', () => expect(prettyPrint(other)).toMatchSnapshot()); }); + +describe('ignore empty class attribute', () => { + const html = dedent` +
+
+
+
+
+ `; + + const css = dedent` + .not-exist {} + `; + + const { critical } = collect(html, css); + test('critical should be empty', () => expect(critical).toEqual('')); +}); diff --git a/packages/server/src/collect.ts b/packages/server/src/collect.ts index 5d2b19715..2859b38a1 100644 --- a/packages/server/src/collect.ts +++ b/packages/server/src/collect.ts @@ -97,7 +97,7 @@ export default function collect(html: string, css: string): CollectResult { const extractClassesFromHtml = (html: string): RegExp => { const htmlClasses: string[] = []; - const regex = /\s+class="([^"]*)"/gm; + const regex = /\s+class="([^"]+)"/gm; let match = regex.exec(html); while (match !== null) {