Skip to content

Commit 7f154a2

Browse files
pfaffeDevtools-frontend LUCI CQ
authored andcommitted
Avoid regex in font matcher
Fixed: 402532185 Change-Id: I17f62cb350eb1cd2cfbe1e9d5c4c2588d8d16639 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6349242 Auto-Submit: Philip Pfaffe <[email protected]> Reviewed-by: Ergün Erdoğmuş <[email protected]> Commit-Queue: Philip Pfaffe <[email protected]>
1 parent d7d0c15 commit 7f154a2

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

front_end/core/sdk/CSSMetadata.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -362,19 +362,6 @@ export class CSSMetadata {
362362
}
363363
}
364364

365-
// The following regexes are used within in the StylesSidebarPropertyRenderer class
366-
// and will parse both invalid and valid values. They both match full strings.
367-
// [^- ][a-zA-Z-]+ matches property key values (e.g. smaller, x-large, initial)
368-
// -?\+?(?:[0-9]+\.[0-9]+|\.[0-9]+|[0-9]+) matches numeric property values (e.g. -.23, 3.3, 55)
369-
// [a-zA-Z%]{0,4} matches the units of numeric property values (e.g. px, vmin, or blank units)
370-
export const FontPropertiesRegex = /^[^- ][a-zA-Z-]+$|^-?\+?(?:[0-9]+\.[0-9]+|\.[0-9]+|[0-9]+)[a-zA-Z%]{0,4}$/;
371-
372-
// "[\w \,-]+",? ? matches double quoted values and the trailing comma/space (e.g. "Tahoma", )
373-
// ('[\w \,-]+',? ?) matches single quoted values and the trailing comma/space (e.g. 'Segoe UI', )
374-
// ([\w \,-]+,? ?) matches non quoted values and the trailing comma/space (e.g. Helvetica)
375-
// (?: ...)+ will match 1 or more of the groups above such that it would match a value with fallbacks (e.g. "Tahoma", 'Segoe UI', Helvetica)
376-
export const FontFamilyRegex = /^("[\w \,-]+"?, ?|'[\w \,-]+',? ?|[\w \-]+,? ?)+$/;
377-
378365
export const CubicBezierKeywordValues = new Map([
379366
['linear', 'cubic-bezier(0, 0, 1, 1)'],
380367
['ease', 'cubic-bezier(0.25, 0.1, 0.25, 1)'],

front_end/core/sdk/CSSPropertyParserMatchers.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import {
1212
cssMetadata,
1313
type CSSWideKeyword,
1414
CubicBezierKeywordValues,
15-
FontFamilyRegex,
16-
FontPropertiesRegex
1715
} from './CSSMetadata.js';
1816
import type {CSSProperty} from './CSSProperty.js';
1917
import {
@@ -662,13 +660,18 @@ export class FontMatcher extends matcherBase(FontMatch) {
662660
if (node.name !== 'Declaration') {
663661
return null;
664662
}
665-
const regex = matching.ast.propertyName === 'font-family' ? FontFamilyRegex : FontPropertiesRegex;
666663
const valueNodes = ASTUtils.siblings(ASTUtils.declValue(node));
667664
if (valueNodes.length === 0) {
668665
return null;
669666
}
667+
const validNodes = matching.ast.propertyName === 'font-family' ? ['ValueName', 'StringLiteral', 'Comment', ','] :
668+
['Comment', 'ValueName', 'NumberLiteral'];
669+
670+
if (valueNodes.some(node => !validNodes.includes(node.name))) {
671+
return null;
672+
}
670673
const valueText = matching.ast.textRange(valueNodes[0], valueNodes[valueNodes.length - 1]);
671-
return regex.test(valueText) ? new FontMatch(valueText, node) : null;
674+
return new FontMatch(valueText, node);
672675
}
673676
}
674677

0 commit comments

Comments
 (0)