mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-08 20:58:24 +08:00
All of these were Closure-specific. R=szuend@chromium.org No-Presubmit: True Bug: 1011811 Change-Id: Ia580e21cd3b06f532bb5a63a770ccfa3d7c796ee Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2569761 Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org> Reviewed-by: Simon Zünd <szuend@chromium.org>
81 lines
2.3 KiB
JavaScript
81 lines
2.3 KiB
JavaScript
|
|
// Copyright 2020 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
const globalValues = new Set(['inherit', 'initial', 'unset']);
|
|
|
|
const tagRegexp = /[\x20-\x7E]{4}/;
|
|
const numRegexp = /[+-]?(?:\d*\.)?\d+(?:[eE]\d+)?/;
|
|
const fontVariationSettingsRegexp =
|
|
new RegExp(`(?:'(${tagRegexp.source})')|(?:"(${tagRegexp.source})")\\s+(${numRegexp.source})`);
|
|
|
|
/**
|
|
* Extracts information about font variation settings assuming
|
|
* value is valid according to the spec: https://drafts.csswg.org/css-fonts-4/#font-variation-settings-def
|
|
*
|
|
* @param {string} value A valid CSS font-variation-settings property value
|
|
* @return {!Array<!{tag:string, value:number}>}
|
|
*/
|
|
export function parseFontVariationSettings(value) {
|
|
if (globalValues.has(value.trim()) || value.trim() === 'normal') {
|
|
return [];
|
|
}
|
|
const results = [];
|
|
for (const setting of splitByComma(stripComments(value))) {
|
|
const match = setting.match(fontVariationSettingsRegexp);
|
|
if (match) {
|
|
results.push({
|
|
tag: match[1] || match[2], // Tag name match.
|
|
value: parseFloat(match[3]), // Tag value.
|
|
});
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
// "str" or 'str'
|
|
const fontFamilyRegexp = /^"(.+)"|'(.+)'$/;
|
|
|
|
/**
|
|
* Extracts font families assuming the value is valid according to
|
|
* the spec: https://drafts.csswg.org/css-fonts-4/#font-family-prop
|
|
*
|
|
* @param {string} value A valid CSS font-family property value
|
|
* @return {!Array<string>}
|
|
*/
|
|
export function parseFontFamily(value) {
|
|
if (globalValues.has(value.trim())) {
|
|
return [];
|
|
}
|
|
const results = [];
|
|
for (const family of splitByComma(stripComments(value))) {
|
|
const match = family.match(fontFamilyRegexp);
|
|
if (match) {
|
|
// Either the 1st or 2nd group matches if the value is in quotes
|
|
results.push(match[1] || match[2]);
|
|
} else {
|
|
// Value without without quotes.
|
|
results.push(family);
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
/**
|
|
* Splits a list of values by comma and trims parts
|
|
* @param {string} value
|
|
* @return {!Array<string>}
|
|
*/
|
|
export function splitByComma(value) {
|
|
return value.split(',').map(part => part.trim());
|
|
}
|
|
|
|
/**
|
|
* @param {string} value
|
|
* @return {string}
|
|
*/
|
|
export function stripComments(value) {
|
|
return value.replaceAll(/(\/\*(?:.|\s)*?\*\/)/g, '');
|
|
}
|