Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[node-build-scripts] fix(css-variables): string quotes for all values #5304

Merged
merged 1 commit into from
May 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions packages/node-build-scripts/node-sass-json-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ function getJsonValueFromSassValue(value, options) {
try {
if (typeof value !== "undefined" && "getValue" in value) {
resolvedValue = value.getValue();
if (value instanceof sass.types.String && stringHasWrappingQuotes(value)) {
// We want to preserve quotes around string elements - if we don't do this,
// we lose those quotes during the JSON conversion process.
resolvedValue = `"${resolvedValue}"`;
}
} else {
resolvedValue = null;
}
Expand All @@ -98,6 +103,20 @@ function getJsonValueFromSassValue(value, options) {
return resolvedValue;
}

/**
* HACKHACK: the legacy Sass value API has no way of distinguishing between quoted and unquoted strings
* (see documentation of types.String from the "sass" package), but there's a hacky way to parse that out
* from the string output by the value's .toString() method. A better solution would involve migrating to
* the async API.
*
* @param {sass.type.String} value dart-sass legacy String value
* @returns {boolean} true if the string contains quotes as the first and last characters
*/
function stringHasWrappingQuotes(value) {
const strValue = value.dartValue.toString();
return strValue.charAt(0) === '"' && strValue.charAt(strValue.length - 1) === '"';
}

/**
* @param {sass.types.List} list
* @param {Options} options
Expand All @@ -108,33 +127,12 @@ function listToArray(list, options) {
const data = [];
const hasSeparator = list.getSeparator();
for (const index of Array.from({ length }).keys()) {
const sassValue = list.getValue(index);
let value = getJsonValueFromSassValue(sassValue, options);
if (hasSeparator) {
// for lists with comma separators (true lists), we want to preserve quotes around string elements
// if we don't do this, we lose those quotes during the JSON conversion process
if (sassValue instanceof sass.types.String && hasWrappingQuotes(sassValue)) {
value = `"${value}"`;
}
}
const value = getJsonValueFromSassValue(list.getValue(index), options);
data.push(value);
}
return hasSeparator ? data : data.join(" ");
}

/**
* @param {sass.type.String} value dart-sass legacy String value
* @returns {boolean} true if the string contains quotes as the first and last characters
*/
function hasWrappingQuotes(value) {
// HACKHACK: the legacy Sass value API has no way of distinguishing between quoted and unquoted strings
// (see documentation of types.String from the "sass" package), but there's a hacky way to parse that out
// from the string output by the dart-sass value's .toString() method. A better solution would involve
// migrating to the async API.
const strValue = value.dartValue.toString();
return strValue.charAt(0) === '"' && strValue.charAt(strValue.length - 1) === '"';
}

/**
* @param {sass.types.Map} map
* @param {Options} options
Expand Down