You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
16 KiB

4 years ago
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _path = _interopRequireDefault(require("path"));
  5. var _cssSyntaxError = _interopRequireDefault(require("./css-syntax-error"));
  6. var _previousMap = _interopRequireDefault(require("./previous-map"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  9. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  10. var sequence = 0;
  11. /**
  12. * Represents the source CSS.
  13. *
  14. * @example
  15. * const root = postcss.parse(css, { from: file })
  16. * const input = root.source.input
  17. */
  18. var Input =
  19. /*#__PURE__*/
  20. function () {
  21. /**
  22. * @param {string} css Input CSS source.
  23. * @param {object} [opts] {@link Processor#process} options.
  24. */
  25. function Input(css, opts) {
  26. if (opts === void 0) {
  27. opts = {};
  28. }
  29. if (css === null || typeof css === 'object' && !css.toString) {
  30. throw new Error("PostCSS received " + css + " instead of CSS string");
  31. }
  32. /**
  33. * Input CSS source
  34. *
  35. * @type {string}
  36. *
  37. * @example
  38. * const input = postcss.parse('a{}', { from: file }).input
  39. * input.css //=> "a{}"
  40. */
  41. this.css = css.toString();
  42. if (this.css[0] === "\uFEFF" || this.css[0] === "\uFFFE") {
  43. this.hasBOM = true;
  44. this.css = this.css.slice(1);
  45. } else {
  46. this.hasBOM = false;
  47. }
  48. if (opts.from) {
  49. if (/^\w+:\/\//.test(opts.from)) {
  50. /**
  51. * The absolute path to the CSS source file defined
  52. * with the `from` option.
  53. *
  54. * @type {string}
  55. *
  56. * @example
  57. * const root = postcss.parse(css, { from: 'a.css' })
  58. * root.source.input.file //=> '/home/ai/a.css'
  59. */
  60. this.file = opts.from;
  61. } else {
  62. this.file = _path.default.resolve(opts.from);
  63. }
  64. }
  65. var map = new _previousMap.default(this.css, opts);
  66. if (map.text) {
  67. /**
  68. * The input source map passed from a compilation step before PostCSS
  69. * (for example, from Sass compiler).
  70. *
  71. * @type {PreviousMap}
  72. *
  73. * @example
  74. * root.source.input.map.consumer().sources //=> ['a.sass']
  75. */
  76. this.map = map;
  77. var file = map.consumer().file;
  78. if (!this.file && file) this.file = this.mapResolve(file);
  79. }
  80. if (!this.file) {
  81. sequence += 1;
  82. /**
  83. * The unique ID of the CSS source. It will be created if `from` option
  84. * is not provided (because PostCSS does not know the file path).
  85. *
  86. * @type {string}
  87. *
  88. * @example
  89. * const root = postcss.parse(css)
  90. * root.source.input.file //=> undefined
  91. * root.source.input.id //=> "<input css 1>"
  92. */
  93. this.id = '<input css ' + sequence + '>';
  94. }
  95. if (this.map) this.map.file = this.from;
  96. }
  97. var _proto = Input.prototype;
  98. _proto.error = function error(message, line, column, opts) {
  99. if (opts === void 0) {
  100. opts = {};
  101. }
  102. var result;
  103. var origin = this.origin(line, column);
  104. if (origin) {
  105. result = new _cssSyntaxError.default(message, origin.line, origin.column, origin.source, origin.file, opts.plugin);
  106. } else {
  107. result = new _cssSyntaxError.default(message, line, column, this.css, this.file, opts.plugin);
  108. }
  109. result.input = {
  110. line: line,
  111. column: column,
  112. source: this.css
  113. };
  114. if (this.file) result.input.file = this.file;
  115. return result;
  116. }
  117. /**
  118. * Reads the input source map and returns a symbol position
  119. * in the input source (e.g., in a Sass file that was compiled
  120. * to CSS before being passed to PostCSS).
  121. *
  122. * @param {number} line Line in input CSS.
  123. * @param {number} column Column in input CSS.
  124. *
  125. * @return {filePosition} Position in input source.
  126. *
  127. * @example
  128. * root.source.input.origin(1, 1) //=> { file: 'a.css', line: 3, column: 1 }
  129. */
  130. ;
  131. _proto.origin = function origin(line, column) {
  132. if (!this.map) return false;
  133. var consumer = this.map.consumer();
  134. var from = consumer.originalPositionFor({
  135. line: line,
  136. column: column
  137. });
  138. if (!from.source) return false;
  139. var result = {
  140. file: this.mapResolve(from.source),
  141. line: from.line,
  142. column: from.column
  143. };
  144. var source = consumer.sourceContentFor(from.source);
  145. if (source) result.source = source;
  146. return result;
  147. };
  148. _proto.mapResolve = function mapResolve(file) {
  149. if (/^\w+:\/\//.test(file)) {
  150. return file;
  151. }
  152. return _path.default.resolve(this.map.consumer().sourceRoot || '.', file);
  153. }
  154. /**
  155. * The CSS source identifier. Contains {@link Input#file} if the user
  156. * set the `from` option, or {@link Input#id} if they did not.
  157. *
  158. * @type {string}
  159. *
  160. * @example
  161. * const root = postcss.parse(css, { from: 'a.css' })
  162. * root.source.input.from //=> "/home/ai/a.css"
  163. *
  164. * const root = postcss.parse(css)
  165. * root.source.input.from //=> "<input css 1>"
  166. */
  167. ;
  168. _createClass(Input, [{
  169. key: "from",
  170. get: function get() {
  171. return this.file || this.id;
  172. }
  173. }]);
  174. return Input;
  175. }();
  176. var _default = Input;
  177. /**
  178. * @typedef {object} filePosition
  179. * @property {string} file Path to file.
  180. * @property {number} line Source line in file.
  181. * @property {number} column Source column in file.
  182. */
  183. exports.default = _default;
  184. module.exports = exports.default;
  185. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImlucHV0LmVzNiJdLCJuYW1lcyI6WyJzZXF1ZW5jZSIsIklucHV0IiwiY3NzIiwib3B0cyIsInRvU3RyaW5nIiwiRXJyb3IiLCJoYXNCT00iLCJzbGljZSIsImZyb20iLCJ0ZXN0IiwiZmlsZSIsInBhdGgiLCJyZXNvbHZlIiwibWFwIiwiUHJldmlvdXNNYXAiLCJ0ZXh0IiwiY29uc3VtZXIiLCJtYXBSZXNvbHZlIiwiaWQiLCJlcnJvciIsIm1lc3NhZ2UiLCJsaW5lIiwiY29sdW1uIiwicmVzdWx0Iiwib3JpZ2luIiwiQ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