1 | "use strict";
|
2 | exports.__esModule = true;
|
3 | exports.convertScssOrCss = void 0;
|
4 | var suf_regex_1 = require("suf-regex");
|
5 | var utility_1 = require("../utility");
|
6 | var format_property_1 = require("./format.property");
|
7 | var logger_1 = require("../logger");
|
8 |
|
9 | function convertScssOrCss(text, STATE) {
|
10 | var isMultiple = suf_regex_1.isMoreThanOneClassOrId(text);
|
11 | var lastSelector = STATE.CONTEXT.convert.lastSelector;
|
12 |
|
13 | if (!/[\t ]*[#.%]\{.*?}/.test(text)) {
|
14 | if (lastSelector && new RegExp('^.*' + suf_regex_1.escapeRegExp(lastSelector)).test(text)) {
|
15 |
|
16 | if (STATE.CONFIG.debug)
|
17 | logger_1.SetConvertData({ type: 'LAST SELECTOR', text: text });
|
18 | var newText = text.replace(lastSelector, '');
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
71 | if (STATE.CONFIG.debug)
|
72 | logger_1.SetConvertData({ type: 'DEFAULT', text: text });
|
73 | return { text: removeInvalidChars(text).trimEnd(), increaseTabSize: false, lastSelector: lastSelector };
|
74 | }
|
75 | exports.convertScssOrCss = convertScssOrCss;
|
76 | function 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 | }
|