Skip to content

Commit

Permalink
Support for url conversion in the blockdef style section
Browse files Browse the repository at this point in the history
  • Loading branch information
bago committed May 19, 2022
1 parent a30228f commit 3709d4e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
27 changes: 2 additions & 25 deletions src/js/converter/declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var domutils = require("./domutils.js");
var _declarationValueLookup = function(declarations, propertyname, templateUrlConverter) {
for (var i = declarations.length - 1; i >= 0; i--) {
if (declarations[i].type == 'property' && declarations[i].name == propertyname) {
return _declarationValueUrlPrefixer(declarations[i].value, templateUrlConverter);
return converterUtils.declarationValueUrlPrefixer(declarations[i].value, templateUrlConverter);
}
}
return null;
Expand All @@ -24,29 +24,6 @@ var _propToCamelCase = function(propName) {
});
};

var _declarationValueUrlPrefixer = function(value, templateUrlConverter) {
if (value.match(/url\(.*\)/)) {
var replaced = value.replace(/(url\()([^\)]*)(\))/g, function(matched, prefix, url, postfix) {
var trimmed = url.trim();
var apice = url.trim().charAt(0);
if (apice == '\'' || apice == '"') {
trimmed = trimmed.substr(1, trimmed.length - 2);
} else {
apice = '';
}
var newUrl = templateUrlConverter(trimmed);
if (newUrl !== null) {
return prefix + apice + newUrl + apice + postfix;
} else {
return matched;
}
});
return replaced;
} else {
return value;
}
};

var elaborateDeclarations = function(style, declarations, templateUrlConverter, bindingProvider, element, basicBindings, removeDisplayNone) {
var newBindings = typeof basicBindings == 'object' && basicBindings !== null ? basicBindings : {};
var newStyle = null;
Expand Down Expand Up @@ -201,7 +178,7 @@ var elaborateDeclarations = function(style, declarations, templateUrlConverter,

} else {
// prefixing urls
var replacedValue = _declarationValueUrlPrefixer(declarations[i].value, templateUrlConverter);
var replacedValue = converterUtils.declarationValueUrlPrefixer(declarations[i].value, templateUrlConverter);
if (replacedValue != declarations[i].value) {
if (newStyle === null && typeof style !== 'undefined') newStyle = style;
if (newStyle !== null) {
Expand Down
6 changes: 3 additions & 3 deletions src/js/converter/stylesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var _removeOptionalQuotes = function(str) {
return str;
};

var _processStyleSheetRules_processBlockDef = function(blockDefsUpdater, rules) {
var _processStyleSheetRules_processBlockDef = function(blockDefsUpdater, rules, templateUrlConverter) {
var properties, namedProps, decls;
// name, contextName, globalStyle, themeOverride, extend, min, max, widget, options, category, variant, help, blockDescription, version,
for (var i = 0; i < rules.length; i++) {
Expand Down Expand Up @@ -76,7 +76,7 @@ var _processStyleSheetRules_processBlockDef = function(blockDefsUpdater, rules)
else if (decls[k].name == 'properties') properties = val;
else if (decls[k].name == 'theme') namedProps.globalStyle = '_theme_.' + val;
else if (decls[k].name == 'themeOverride') namedProps.themeOverride = String(val).toLowerCase() == 'true';
else namedProps[decls[k].name] = val;
else namedProps[decls[k].name] = converterUtils.declarationValueUrlPrefixer(val, templateUrlConverter);
// NOTE in past we detected unsupported properties, while now we simple push every declaration in a namedProperty.
// This make it harder to spot errors in declarations.
// Named properties we supported were extend, min, max, options, widget, category, variant, help, blockDescription, version
Expand Down Expand Up @@ -124,7 +124,7 @@ var processStylesheetRules = function(style, rules, localWithBindingProvider, bl

for (var i = rules.length - 1; i >= 0; i--) {
if (rules[i].type == 'supports' && rules[i].name == '-ko-blockdefs') {
_processStyleSheetRules_processBlockDef(blockDefsUpdater, rules[i].rules);
_processStyleSheetRules_processBlockDef(blockDefsUpdater, rules[i].rules, templateUrlConverter);
newStyle = converterUtils.removeStyle(newStyle, rules[i].position.start, lastStart, 0, 0, 0, '');
/* temporary experimental code not used
} else if (rules[i].type == 'supports' && rules[i].name == '-ko-themes') {
Expand Down
26 changes: 25 additions & 1 deletion src/js/converter/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,33 @@ var expressionBinding = function(expression, bindingProvider, defaultValue) {
}
};

var declarationValueUrlPrefixer = function(value, templateUrlConverter) {
if (value.match(/url\(.*\)/)) {
var replaced = value.replace(/(url\()([^\)]*)(\))/g, function(matched, prefix, url, postfix) {
var trimmed = url.trim();
var apice = url.trim().charAt(0);
if (apice == '\'' || apice == '"') {
trimmed = trimmed.substr(1, trimmed.length - 2);
} else {
apice = '';
}
var newUrl = templateUrlConverter(trimmed);
if (newUrl !== null) {
return prefix + apice + newUrl + apice + postfix;
} else {
return matched;
}
});
return replaced;
} else {
return value;
}
};

module.exports = {
addSlashes: addSlashes,
removeStyle: removeStyle,
conditionBinding: conditionBinding,
expressionBinding: expressionBinding
expressionBinding: expressionBinding,
declarationValueUrlPrefixer: declarationValueUrlPrefixer
};

0 comments on commit 3709d4e

Please sign in to comment.