From ef70988027598d591481c533100cd9613ea642af Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 25 Mar 2023 13:25:44 +0100 Subject: [PATCH] Reduce duplication in the `validateCSSFont` helper function Currently we're *virtually* duplicating the same code, for validating quotation marks, twice in this helper function. The size decrease is quite small (107 bytes) and this makes the code slightly harder to reader, hence I completely understand if this patch is rejected. --- src/core/core_utils.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/core/core_utils.js b/src/core/core_utils.js index 89fad2b6c..7a2121043 100644 --- a/src/core/core_utils.js +++ b/src/core/core_utils.js @@ -470,14 +470,11 @@ function validateCSSFont(cssFontInfo) { const { fontFamily, fontWeight, italicAngle } = cssFontInfo; // See https://developer.mozilla.org/en-US/docs/Web/CSS/string. - if (/^".*"$/.test(fontFamily)) { - if (/[^\\]"/.test(fontFamily.slice(1, -1))) { - warn(`XFA - FontFamily contains some unescaped ": ${fontFamily}.`); - return false; - } - } else if (/^'.*'$/.test(fontFamily)) { - if (/[^\\]'/.test(fontFamily.slice(1, -1))) { - warn(`XFA - FontFamily contains some unescaped ': ${fontFamily}.`); + const m = /^("|').*("|')$/.exec(fontFamily); + if (m && m[1] === m[2]) { + const re = new RegExp(`[^\\\\]${m[1]}`); + if (re.test(fontFamily.slice(1, -1))) { + warn(`XFA - FontFamily contains unescaped ${m[1]}: ${fontFamily}.`); return false; } } else { @@ -485,7 +482,7 @@ function validateCSSFont(cssFontInfo) { for (const ident of fontFamily.split(/[ \t]+/)) { if (/^(\d|(-(\d|-)))/.test(ident) || !/^[\w-\\]+$/.test(ident)) { warn( - `XFA - FontFamily contains some invalid : ${fontFamily}.` + `XFA - FontFamily contains invalid : ${fontFamily}.` ); return false; }