contexts.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. var contexts = {};
  2. module.exports = contexts;
  3. var copyFromOriginal = function copyFromOriginal(original, destination, propertiesToCopy) {
  4. if (!original) { return; }
  5. for (var i = 0; i < propertiesToCopy.length; i++) {
  6. if (original.hasOwnProperty(propertiesToCopy[i])) {
  7. destination[propertiesToCopy[i]] = original[propertiesToCopy[i]];
  8. }
  9. }
  10. };
  11. /*
  12. parse is used whilst parsing
  13. */
  14. var parseCopyProperties = [
  15. // options
  16. 'paths', // option - unmodified - paths to search for imports on
  17. 'relativeUrls', // option - whether to adjust URL's to be relative
  18. 'rootpath', // option - rootpath to append to URL's
  19. 'strictImports', // option -
  20. 'insecure', // option - whether to allow imports from insecure ssl hosts
  21. 'dumpLineNumbers', // option - whether to dump line numbers
  22. 'compress', // option - whether to compress
  23. 'syncImport', // option - whether to import synchronously
  24. 'chunkInput', // option - whether to chunk input. more performant but causes parse issues.
  25. 'mime', // browser only - mime type for sheet import
  26. 'useFileCache', // browser only - whether to use the per file session cache
  27. // context
  28. 'processImports', // option & context - whether to process imports. if false then imports will not be imported.
  29. // Used by the import manager to stop multiple import visitors being created.
  30. 'pluginManager' // Used as the plugin manager for the session
  31. ];
  32. contexts.Parse = function(options) {
  33. copyFromOriginal(options, this, parseCopyProperties);
  34. if (typeof this.paths === "string") { this.paths = [this.paths]; }
  35. };
  36. var evalCopyProperties = [
  37. 'paths', // additional include paths
  38. 'compress', // whether to compress
  39. 'ieCompat', // whether to enforce IE compatibility (IE8 data-uri)
  40. 'strictMath', // whether math has to be within parenthesis
  41. 'strictUnits', // whether units need to evaluate correctly
  42. 'sourceMap', // whether to output a source map
  43. 'importMultiple', // whether we are currently importing multiple copies
  44. 'urlArgs', // whether to add args into url tokens
  45. 'javascriptEnabled',// option - whether JavaScript is enabled. if undefined, defaults to true
  46. 'pluginManager', // Used as the plugin manager for the session
  47. 'importantScope' // used to bubble up !important statements
  48. ];
  49. contexts.Eval = function(options, frames) {
  50. copyFromOriginal(options, this, evalCopyProperties);
  51. if (typeof this.paths === "string") { this.paths = [this.paths]; }
  52. this.frames = frames || [];
  53. this.importantScope = this.importantScope || [];
  54. };
  55. contexts.Eval.prototype.inParenthesis = function () {
  56. if (!this.parensStack) {
  57. this.parensStack = [];
  58. }
  59. this.parensStack.push(true);
  60. };
  61. contexts.Eval.prototype.outOfParenthesis = function () {
  62. this.parensStack.pop();
  63. };
  64. contexts.Eval.prototype.isMathOn = function () {
  65. return this.strictMath ? (this.parensStack && this.parensStack.length) : true;
  66. };
  67. contexts.Eval.prototype.isPathRelative = function (path) {
  68. return !/^(?:[a-z-]+:|\/|#)/i.test(path);
  69. };
  70. contexts.Eval.prototype.normalizePath = function( path ) {
  71. var
  72. segments = path.split("/").reverse(),
  73. segment;
  74. path = [];
  75. while (segments.length !== 0 ) {
  76. segment = segments.pop();
  77. switch( segment ) {
  78. case ".":
  79. break;
  80. case "..":
  81. if ((path.length === 0) || (path[path.length - 1] === "..")) {
  82. path.push( segment );
  83. } else {
  84. path.pop();
  85. }
  86. break;
  87. default:
  88. path.push( segment );
  89. break;
  90. }
  91. }
  92. return path.join("/");
  93. };
  94. //todo - do the same for the toCSS ?