contexts.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var contexts = {};
  5. exports.default = contexts;
  6. var Constants = tslib_1.__importStar(require("./constants"));
  7. var copyFromOriginal = function copyFromOriginal(original, destination, propertiesToCopy) {
  8. if (!original) {
  9. return;
  10. }
  11. for (var i = 0; i < propertiesToCopy.length; i++) {
  12. if (Object.prototype.hasOwnProperty.call(original, propertiesToCopy[i])) {
  13. destination[propertiesToCopy[i]] = original[propertiesToCopy[i]];
  14. }
  15. }
  16. };
  17. /*
  18. parse is used whilst parsing
  19. */
  20. var 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. ];
  38. contexts.Parse = function (options) {
  39. copyFromOriginal(options, this, parseCopyProperties);
  40. if (typeof this.paths === 'string') {
  41. this.paths = [this.paths];
  42. }
  43. };
  44. var 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. ];
  57. contexts.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. };
  65. contexts.Eval.prototype.enterCalc = function () {
  66. if (!this.calcStack) {
  67. this.calcStack = [];
  68. }
  69. this.calcStack.push(true);
  70. this.inCalc = true;
  71. };
  72. contexts.Eval.prototype.exitCalc = function () {
  73. this.calcStack.pop();
  74. if (!this.calcStack.length) {
  75. this.inCalc = false;
  76. }
  77. };
  78. contexts.Eval.prototype.inParenthesis = function () {
  79. if (!this.parensStack) {
  80. this.parensStack = [];
  81. }
  82. this.parensStack.push(true);
  83. };
  84. contexts.Eval.prototype.outOfParenthesis = function () {
  85. this.parensStack.pop();
  86. };
  87. contexts.Eval.prototype.inCalc = false;
  88. contexts.Eval.prototype.mathOn = true;
  89. contexts.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. };
  101. contexts.Eval.prototype.pathRequiresRewrite = function (path) {
  102. var isRelative = this.rewriteUrls === Constants.RewriteUrls.LOCAL ? isPathLocalRelative : isPathRelative;
  103. return isRelative(path);
  104. };
  105. contexts.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. };
  118. contexts.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. };
  142. function isPathRelative(path) {
  143. return !/^(?:[a-z-]+:|\/|#)/i.test(path);
  144. }
  145. function isPathLocalRelative(path) {
  146. return path.charAt(0) === '.';
  147. }
  148. // todo - do the same for the toCSS ?
  149. //# sourceMappingURL=contexts.js.map