UNPKG

stylesheet-loader

Version:
99 lines 4.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var camelCase = require("camelcase"); var chalk_1 = require("chalk"); var normalizeColor_1 = require("./normalizeColor"); var particular_1 = require("./particular"); var Validation_1 = require("./Validation"); var promptMessage_1 = require("./promptMessage"); var colorProperties_1 = require("./colorProperties"); var QUOTES_REG = /[\\'|\\"]/g; var VAR_REGEX = /^var\(\-\-(.*)\)$/g; exports.default = { sanitizeSelector: function (selector, transformDescendantCombinator, position, log) { if (transformDescendantCombinator === void 0) { transformDescendantCombinator = false; } if (position === void 0) { position = { start: { line: 0, column: 0 } }; } if (log === void 0) { log = false; } // tag selector suffix @ if (/^[a-zA-Z]/.test(selector)) { selector = "@" + selector; } // filter multiple extend selectors if (log && !transformDescendantCombinator && !/^[.|@|#][a-zA-Z0-9_:\-]+$/.test(selector)) { var message = "line: " + position.start.line + ", column: " + position.start.column + " - \"" + selector + "\" is not a valid selector (e.g. \".abc\u3001.abcBcd\u3001.abc_bcd\")"; console.error(chalk_1.default.red.bold(message)); promptMessage_1.pushErrorMessage(message); return null; } return selector.replace(/\s/gi, '_').replace(/[\.]/g, ''); }, convertProp: function (prop) { var result = camelCase(prop); // -webkit/-uc/-o to Webkit/Uc/O if (/^\-\w/.test(prop)) { result = result.replace(/^(\w)/, function ($1) { return $1.substring(0, 1).toUpperCase(); }); } return result; }, convertValue: function (property, value) { var result = value; if (typeof value === 'string' && value.search(VAR_REGEX) > -1) { // var(--test-var) result = value; return result; } if (!Number.isNaN(Number(result))) { result = Number(result); } if (colorProperties_1.default[property]) { result = normalizeColor_1.default(value); } return result; }, convertCSSVariableValue: function (value) { return value.replace(/\-\-/, '').replace(/\-(\w)/g, function (all, letter) { return letter.toUpperCase(); }); }, convert: function (rule, query) { var _this = this; if (query === void 0) { query = {}; } var style = {}; var _a = query, log = _a.log, theme = _a.theme; if (rule.tagName === 'text') { return; } rule.declarations.forEach(function (declaration) { var _a; if (declaration.type !== 'declaration') { return; } declaration.value = declaration.value.replace(QUOTES_REG, ''); var camelCaseProperty = _this.convertProp(declaration.property); var value = _this.convertValue(camelCaseProperty, declaration.value); style[camelCaseProperty] = value; if (typeof value === 'string' && value.search(VAR_REGEX) > -1 && theme) { // var(--test-var) Object.assign(style, (_a = {}, _a[camelCaseProperty] = _this.convertCSSVariableValue(value), _a)); } else { Validation_1.default.validate(camelCaseProperty, declaration.property, declaration.value, rule.selectors.join(', '), declaration.position, log); if (particular_1.default[camelCaseProperty]) { var particularResult = particular_1.default[camelCaseProperty](value); if (particularResult.isDeleted) { style[camelCaseProperty] = null; delete style[camelCaseProperty]; delete particularResult.isDeleted; } Object.assign(style, particularResult); } } }); return style; }, }; //# sourceMappingURL=transformer.js.map