node.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = void 0;
  4. var _util = require("../util");
  5. 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); } }
  6. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  7. var cloneNode = function cloneNode(obj, parent) {
  8. if (typeof obj !== 'object' || obj === null) {
  9. return obj;
  10. }
  11. var cloned = new obj.constructor();
  12. for (var i in obj) {
  13. if (!obj.hasOwnProperty(i)) {
  14. continue;
  15. }
  16. var value = obj[i];
  17. var type = typeof value;
  18. if (i === 'parent' && type === 'object') {
  19. if (parent) {
  20. cloned[i] = parent;
  21. }
  22. } else if (value instanceof Array) {
  23. cloned[i] = value.map(function (j) {
  24. return cloneNode(j, cloned);
  25. });
  26. } else {
  27. cloned[i] = cloneNode(value, cloned);
  28. }
  29. }
  30. return cloned;
  31. };
  32. var Node = /*#__PURE__*/function () {
  33. function Node(opts) {
  34. if (opts === void 0) {
  35. opts = {};
  36. }
  37. Object.assign(this, opts);
  38. this.spaces = this.spaces || {};
  39. this.spaces.before = this.spaces.before || '';
  40. this.spaces.after = this.spaces.after || '';
  41. }
  42. var _proto = Node.prototype;
  43. _proto.remove = function remove() {
  44. if (this.parent) {
  45. this.parent.removeChild(this);
  46. }
  47. this.parent = undefined;
  48. return this;
  49. };
  50. _proto.replaceWith = function replaceWith() {
  51. if (this.parent) {
  52. for (var index in arguments) {
  53. this.parent.insertBefore(this, arguments[index]);
  54. }
  55. this.remove();
  56. }
  57. return this;
  58. };
  59. _proto.next = function next() {
  60. return this.parent.at(this.parent.index(this) + 1);
  61. };
  62. _proto.prev = function prev() {
  63. return this.parent.at(this.parent.index(this) - 1);
  64. };
  65. _proto.clone = function clone(overrides) {
  66. if (overrides === void 0) {
  67. overrides = {};
  68. }
  69. var cloned = cloneNode(this);
  70. for (var name in overrides) {
  71. cloned[name] = overrides[name];
  72. }
  73. return cloned;
  74. }
  75. /**
  76. * Some non-standard syntax doesn't follow normal escaping rules for css.
  77. * This allows non standard syntax to be appended to an existing property
  78. * by specifying the escaped value. By specifying the escaped value,
  79. * illegal characters are allowed to be directly inserted into css output.
  80. * @param {string} name the property to set
  81. * @param {any} value the unescaped value of the property
  82. * @param {string} valueEscaped optional. the escaped value of the property.
  83. */;
  84. _proto.appendToPropertyAndEscape = function appendToPropertyAndEscape(name, value, valueEscaped) {
  85. if (!this.raws) {
  86. this.raws = {};
  87. }
  88. var originalValue = this[name];
  89. var originalEscaped = this.raws[name];
  90. this[name] = originalValue + value; // this may trigger a setter that updates raws, so it has to be set first.
  91. if (originalEscaped || valueEscaped !== value) {
  92. this.raws[name] = (originalEscaped || originalValue) + valueEscaped;
  93. } else {
  94. delete this.raws[name]; // delete any escaped value that was created by the setter.
  95. }
  96. }
  97. /**
  98. * Some non-standard syntax doesn't follow normal escaping rules for css.
  99. * This allows the escaped value to be specified directly, allowing illegal
  100. * characters to be directly inserted into css output.
  101. * @param {string} name the property to set
  102. * @param {any} value the unescaped value of the property
  103. * @param {string} valueEscaped the escaped value of the property.
  104. */;
  105. _proto.setPropertyAndEscape = function setPropertyAndEscape(name, value, valueEscaped) {
  106. if (!this.raws) {
  107. this.raws = {};
  108. }
  109. this[name] = value; // this may trigger a setter that updates raws, so it has to be set first.
  110. this.raws[name] = valueEscaped;
  111. }
  112. /**
  113. * When you want a value to passed through to CSS directly. This method
  114. * deletes the corresponding raw value causing the stringifier to fallback
  115. * to the unescaped value.
  116. * @param {string} name the property to set.
  117. * @param {any} value The value that is both escaped and unescaped.
  118. */;
  119. _proto.setPropertyWithoutEscape = function setPropertyWithoutEscape(name, value) {
  120. this[name] = value; // this may trigger a setter that updates raws, so it has to be set first.
  121. if (this.raws) {
  122. delete this.raws[name];
  123. }
  124. }
  125. /**
  126. *
  127. * @param {number} line The number (starting with 1)
  128. * @param {number} column The column number (starting with 1)
  129. */;
  130. _proto.isAtPosition = function isAtPosition(line, column) {
  131. if (this.source && this.source.start && this.source.end) {
  132. if (this.source.start.line > line) {
  133. return false;
  134. }
  135. if (this.source.end.line < line) {
  136. return false;
  137. }
  138. if (this.source.start.line === line && this.source.start.column > column) {
  139. return false;
  140. }
  141. if (this.source.end.line === line && this.source.end.column < column) {
  142. return false;
  143. }
  144. return true;
  145. }
  146. return undefined;
  147. };
  148. _proto.stringifyProperty = function stringifyProperty(name) {
  149. return this.raws && this.raws[name] || this[name];
  150. };
  151. _proto.valueToString = function valueToString() {
  152. return String(this.stringifyProperty("value"));
  153. };
  154. _proto.toString = function toString() {
  155. return [this.rawSpaceBefore, this.valueToString(), this.rawSpaceAfter].join('');
  156. };
  157. _createClass(Node, [{
  158. key: "rawSpaceBefore",
  159. get: function get() {
  160. var rawSpace = this.raws && this.raws.spaces && this.raws.spaces.before;
  161. if (rawSpace === undefined) {
  162. rawSpace = this.spaces && this.spaces.before;
  163. }
  164. return rawSpace || "";
  165. },
  166. set: function set(raw) {
  167. (0, _util.ensureObject)(this, "raws", "spaces");
  168. this.raws.spaces.before = raw;
  169. }
  170. }, {
  171. key: "rawSpaceAfter",
  172. get: function get() {
  173. var rawSpace = this.raws && this.raws.spaces && this.raws.spaces.after;
  174. if (rawSpace === undefined) {
  175. rawSpace = this.spaces.after;
  176. }
  177. return rawSpace || "";
  178. },
  179. set: function set(raw) {
  180. (0, _util.ensureObject)(this, "raws", "spaces");
  181. this.raws.spaces.after = raw;
  182. }
  183. }]);
  184. return Node;
  185. }();
  186. exports["default"] = Node;
  187. module.exports = exports.default;