UNPKG

4.25 kBJavaScriptView Raw
1"use strict";
2exports.__esModule = true;
3exports.convertScssOrCss = void 0;
4var suf_regex_1 = require("suf-regex");
5var utility_1 = require("../utility");
6var format_property_1 = require("./format.property");
7var logger_1 = require("../logger");
8/** converts scss/css to sass. */
9function convertScssOrCss(text, STATE) {
10 var isMultiple = suf_regex_1.isMoreThanOneClassOrId(text);
11 var lastSelector = STATE.CONTEXT.convert.lastSelector;
12 // if NOT interpolated class, id or partial
13 if (!/[\t ]*[#.%]\{.*?}/.test(text)) {
14 if (lastSelector && new RegExp('^.*' + suf_regex_1.escapeRegExp(lastSelector)).test(text)) {
15 /*istanbul ignore if */
16 if (STATE.CONFIG.debug)
17 logger_1.SetConvertData({ type: 'LAST SELECTOR', text: text });
18 var newText = text.replace(lastSelector, '');
19 // TODO figure out what the commented code below does
20 // if (isPseudoWithParenthesis(text)) {
21 // newText = newText.split('(')[0].trim() + '(&' + ')';
22 // } else if (text.trim().startsWith(lastSelector)) {
23 // } else {
24 // newText = newText.replace(/ /g, '') + ' &';
25 // }
26 newText = text.replace(lastSelector, '&');
27 return {
28 lastSelector: lastSelector,
29 increaseTabSize: true,
30 text: '\n'.concat(utility_1.replaceWithOffset(removeInvalidChars(newText).trimEnd(), STATE.CONFIG.tabSize, STATE))
31 };
32 }
33 else if (suf_regex_1.isCssOneLiner(text)) {
34 /*istanbul ignore if */
35 if (STATE.CONFIG.debug)
36 logger_1.SetConvertData({ type: 'ONE LINER', text: text });
37 var split = text.split('{');
38 var properties = split[1].split(';');
39 // Set isProp to true so that it Sets the property space.
40 STATE.LOCAL_CONTEXT.isProp = true;
41 var selector = split[0].trim();
42 return {
43 increaseTabSize: false,
44 lastSelector: selector,
45 text: selector.concat('\n', properties
46 .map(function (v) {
47 return utility_1.replaceWithOffset(format_property_1.setPropertyValueSpaces(STATE, removeInvalidChars(v)).trim(), STATE.CONFIG.tabSize, STATE);
48 })
49 .join('\n')).trimEnd()
50 };
51 }
52 else if (suf_regex_1.isCssPseudo(text) && !isMultiple) {
53 /*istanbul ignore if */
54 if (STATE.CONFIG.debug)
55 logger_1.SetConvertData({ type: 'PSEUDO', text: text });
56 return {
57 increaseTabSize: false,
58 lastSelector: lastSelector,
59 text: removeInvalidChars(text).trimEnd()
60 };
61 }
62 else if (suf_regex_1.isCssSelector(text)) {
63 /*istanbul ignore if */
64 if (STATE.CONFIG.debug)
65 logger_1.SetConvertData({ type: 'SELECTOR', text: text });
66 lastSelector = removeInvalidChars(text).trimEnd();
67 return { text: lastSelector, increaseTabSize: false, lastSelector: lastSelector };
68 }
69 }
70 /*istanbul ignore if */
71 if (STATE.CONFIG.debug)
72 logger_1.SetConvertData({ type: 'DEFAULT', text: text });
73 return { text: removeInvalidChars(text).trimEnd(), increaseTabSize: false, lastSelector: lastSelector };
74}
75exports.convertScssOrCss = convertScssOrCss;
76function removeInvalidChars(text) {
77 var newText = '';
78 var isInQuotes = false;
79 var isInComment = false;
80 var isInInterpolation = false;
81 for (var i = 0; i < text.length; i++) {
82 var char = text[i];
83 if (!isInQuotes && char === '/' && text[i + 1] === '/') {
84 isInComment = true;
85 }
86 else if (/['"]/.test(char)) {
87 isInQuotes = !isInQuotes;
88 }
89 else if (/#/.test(char) && /{/.test(text[i + 1])) {
90 isInInterpolation = true;
91 }
92 else if (isInInterpolation && /}/.test(text[i - 1])) {
93 isInInterpolation = false;
94 }
95 if (!/[;\{\}]/.test(char) || isInQuotes || isInComment || isInInterpolation) {
96 newText += char;
97 }
98 }
99 return newText;
100}