UNPKG

4.38 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var contexts = {};
5exports.default = contexts;
6var Constants = tslib_1.__importStar(require("./constants"));
7var copyFromOriginal = function copyFromOriginal(original, destination, propertiesToCopy) {
8 if (!original) {
9 return;
10 }
11 for (var i = 0; i < propertiesToCopy.length; i++) {
12 if (original.hasOwnProperty(propertiesToCopy[i])) {
13 destination[propertiesToCopy[i]] = original[propertiesToCopy[i]];
14 }
15 }
16};
17/*
18 parse is used whilst parsing
19 */
20var parseCopyProperties = [
21 // options
22 'paths',
23 'rewriteUrls',
24 'rootpath',
25 'strictImports',
26 'insecure',
27 'dumpLineNumbers',
28 'compress',
29 'syncImport',
30 'chunkInput',
31 'mime',
32 'useFileCache',
33 // context
34 'processImports',
35 // Used by the import manager to stop multiple import visitors being created.
36 'pluginManager' // Used as the plugin manager for the session
37];
38contexts.Parse = function (options) {
39 copyFromOriginal(options, this, parseCopyProperties);
40 if (typeof this.paths === 'string') {
41 this.paths = [this.paths];
42 }
43};
44var evalCopyProperties = [
45 'paths',
46 'compress',
47 'math',
48 'strictUnits',
49 'sourceMap',
50 'importMultiple',
51 'urlArgs',
52 'javascriptEnabled',
53 'pluginManager',
54 'importantScope',
55 'rewriteUrls' // option - whether to adjust URL's to be relative
56];
57contexts.Eval = function (options, frames) {
58 copyFromOriginal(options, this, evalCopyProperties);
59 if (typeof this.paths === 'string') {
60 this.paths = [this.paths];
61 }
62 this.frames = frames || [];
63 this.importantScope = this.importantScope || [];
64};
65contexts.Eval.prototype.enterCalc = function () {
66 if (!this.calcStack) {
67 this.calcStack = [];
68 }
69 this.calcStack.push(true);
70 this.inCalc = true;
71};
72contexts.Eval.prototype.exitCalc = function () {
73 this.calcStack.pop();
74 if (!this.calcStack.length) {
75 this.inCalc = false;
76 }
77};
78contexts.Eval.prototype.inParenthesis = function () {
79 if (!this.parensStack) {
80 this.parensStack = [];
81 }
82 this.parensStack.push(true);
83};
84contexts.Eval.prototype.outOfParenthesis = function () {
85 this.parensStack.pop();
86};
87contexts.Eval.prototype.inCalc = false;
88contexts.Eval.prototype.mathOn = true;
89contexts.Eval.prototype.isMathOn = function (op) {
90 if (!this.mathOn) {
91 return false;
92 }
93 if (op === '/' && this.math !== Constants.Math.ALWAYS && (!this.parensStack || !this.parensStack.length)) {
94 return false;
95 }
96 if (this.math > Constants.Math.PARENS_DIVISION) {
97 return this.parensStack && this.parensStack.length;
98 }
99 return true;
100};
101contexts.Eval.prototype.pathRequiresRewrite = function (path) {
102 var isRelative = this.rewriteUrls === Constants.RewriteUrls.LOCAL ? isPathLocalRelative : isPathRelative;
103 return isRelative(path);
104};
105contexts.Eval.prototype.rewritePath = function (path, rootpath) {
106 var newPath;
107 rootpath = rootpath || '';
108 newPath = this.normalizePath(rootpath + path);
109 // If a path was explicit relative and the rootpath was not an absolute path
110 // we must ensure that the new path is also explicit relative.
111 if (isPathLocalRelative(path) &&
112 isPathRelative(rootpath) &&
113 isPathLocalRelative(newPath) === false) {
114 newPath = "./" + newPath;
115 }
116 return newPath;
117};
118contexts.Eval.prototype.normalizePath = function (path) {
119 var segments = path.split('/').reverse();
120 var segment;
121 path = [];
122 while (segments.length !== 0) {
123 segment = segments.pop();
124 switch (segment) {
125 case '.':
126 break;
127 case '..':
128 if ((path.length === 0) || (path[path.length - 1] === '..')) {
129 path.push(segment);
130 }
131 else {
132 path.pop();
133 }
134 break;
135 default:
136 path.push(segment);
137 break;
138 }
139 }
140 return path.join('/');
141};
142function isPathRelative(path) {
143 return !/^(?:[a-z-]+:|\/|#)/i.test(path);
144}
145function isPathLocalRelative(path) {
146 return path.charAt(0) === '.';
147}
148// todo - do the same for the toCSS ?
149//# sourceMappingURL=contexts.js.map
\No newline at end of file