Skip to content

Commit

Permalink
fix: backport style_parser fixes
Browse files Browse the repository at this point in the history
This commit backport style parser fixes from angular/angular#49460
  • Loading branch information
alan-agius4 committed Mar 29, 2023
1 parent bfc9114 commit dc2f473
Showing 1 changed file with 3 additions and 31 deletions.
34 changes: 3 additions & 31 deletions lib/style_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
* found in the LICENSE file at https://angular.io/license
*/


// The below is a compiled copy of https://github.com/angular/angular/blob/92e41e9cb417223d9888a4c23b4c0e73188f87d0/packages/compiler/src/render3/view/style_parser.ts

Object.defineProperty(exports, "__esModule", { value: true });
exports.hyphenate = exports.stripUnnecessaryQuotes = exports.parse = void 0;
exports.hyphenate = exports.parse = void 0;
/**
* Parses string representation of a style and converts it into object literal.
*
Expand All @@ -32,7 +31,6 @@ function parse(value) {
let valueStart = 0;
let propStart = 0;
let currentProp = null;
let valueHasQuotes = false;
while (i < value.length) {
const token = value.charCodeAt(i++);
switch (token) {
Expand All @@ -45,7 +43,6 @@ function parse(value) {
case 39 /* Char.QuoteSingle */:
// valueStart needs to be there since prop values don't
// have quotes in CSS
valueHasQuotes = valueHasQuotes || valueStart > 0;
if (quote === 0 /* Char.QuoteNone */) {
quote = 39 /* Char.QuoteSingle */;
} else if (
Expand All @@ -57,7 +54,6 @@ function parse(value) {
break;
case 34 /* Char.QuoteDouble */:
// same logic as above
valueHasQuotes = valueHasQuotes || valueStart > 0;
if (quote === 0 /* Char.QuoteNone */) {
quote = 34 /* Char.QuoteDouble */;
} else if (
Expand Down Expand Up @@ -85,45 +81,21 @@ function parse(value) {
quote === 0 /* Char.QuoteNone */
) {
const styleVal = value.substring(valueStart, i - 1).trim();
styles.push(
currentProp,
valueHasQuotes ? stripUnnecessaryQuotes(styleVal) : styleVal
);
styles.push(currentProp, styleVal);
propStart = i;
valueStart = 0;
currentProp = null;
valueHasQuotes = false;
}
break;
}
}
if (currentProp && valueStart) {
const styleVal = value.slice(valueStart).trim();
styles.push(
currentProp,
valueHasQuotes ? stripUnnecessaryQuotes(styleVal) : styleVal
);
styles.push(currentProp, styleVal);
}
return styles;
}
exports.parse = parse;
function stripUnnecessaryQuotes(value) {
const qS = value.charCodeAt(0);
const qE = value.charCodeAt(value.length - 1);
if (
qS == qE &&
(qS == 39 /* Char.QuoteSingle */ || qS == 34) /* Char.QuoteDouble */
) {
const tempValue = value.substring(1, value.length - 1);
// special case to avoid using a multi-quoted string that was just chomped
// (e.g. `font-family: "Verdana", "sans-serif"`)
if (tempValue.indexOf("'") == -1 && tempValue.indexOf('"') == -1) {
value = tempValue;
}
}
return value;
}
exports.stripUnnecessaryQuotes = stripUnnecessaryQuotes;
function hyphenate(value) {
return value
.replace(/[a-z][A-Z]/g, (v) => {
Expand Down

0 comments on commit dc2f473

Please sign in to comment.