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.

238 lines
6.5 KiB

2 years ago
  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); 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. ;
  85. _proto.appendToPropertyAndEscape = function appendToPropertyAndEscape(name, value, valueEscaped) {
  86. if (!this.raws) {
  87. this.raws = {};
  88. }
  89. var originalValue = this[name];
  90. var originalEscaped = this.raws[name];
  91. this[name] = originalValue + value; // this may trigger a setter that updates raws, so it has to be set first.
  92. if (originalEscaped || valueEscaped !== value) {
  93. this.raws[name] = (originalEscaped || originalValue) + valueEscaped;
  94. } else {
  95. delete this.raws[name]; // delete any escaped value that was created by the setter.
  96. }
  97. }
  98. /**
  99. * Some non-standard syntax doesn't follow normal escaping rules for css.
  100. * This allows the escaped value to be specified directly, allowing illegal
  101. * characters to be directly inserted into css output.
  102. * @param {string} name the property to set
  103. * @param {any} value the unescaped value of the property
  104. * @param {string} valueEscaped the escaped value of the property.
  105. */
  106. ;
  107. _proto.setPropertyAndEscape = function setPropertyAndEscape(name, value, valueEscaped) {
  108. if (!this.raws) {
  109. this.raws = {};
  110. }
  111. this[name] = value; // this may trigger a setter that updates raws, so it has to be set first.
  112. this.raws[name] = valueEscaped;
  113. }
  114. /**
  115. * When you want a value to passed through to CSS directly. This method
  116. * deletes the corresponding raw value causing the stringifier to fallback
  117. * to the unescaped value.
  118. * @param {string} name the property to set.
  119. * @param {any} value The value that is both escaped and unescaped.
  120. */
  121. ;
  122. _proto.setPropertyWithoutEscape = function setPropertyWithoutEscape(name, value) {
  123. this[name] = value; // this may trigger a setter that updates raws, so it has to be set first.
  124. if (this.raws) {
  125. delete this.raws[name];
  126. }
  127. }
  128. /**
  129. *
  130. * @param {number} line The number (starting with 1)
  131. * @param {number} column The column number (starting with 1)
  132. */
  133. ;
  134. _proto.isAtPosition = function isAtPosition(line, column) {
  135. if (this.source && this.source.start && this.source.end) {
  136. if (this.source.start.line > line) {
  137. return false;
  138. }
  139. if (this.source.end.line < line) {
  140. return false;
  141. }
  142. if (this.source.start.line === line && this.source.start.column > column) {
  143. return false;
  144. }
  145. if (this.source.end.line === line && this.source.end.column < column) {
  146. return false;
  147. }
  148. return true;
  149. }
  150. return undefined;
  151. };
  152. _proto.stringifyProperty = function stringifyProperty(name) {
  153. return this.raws && this.raws[name] || this[name];
  154. };
  155. _proto.valueToString = function valueToString() {
  156. return String(this.stringifyProperty("value"));
  157. };
  158. _proto.toString = function toString() {
  159. return [this.rawSpaceBefore, this.valueToString(), this.rawSpaceAfter].join('');
  160. };
  161. _createClass(Node, [{
  162. key: "rawSpaceBefore",
  163. get: function get() {
  164. var rawSpace = this.raws && this.raws.spaces && this.raws.spaces.before;
  165. if (rawSpace === undefined) {
  166. rawSpace = this.spaces && this.spaces.before;
  167. }
  168. return rawSpace || "";
  169. },
  170. set: function set(raw) {
  171. (0, _util.ensureObject)(this, "raws", "spaces");
  172. this.raws.spaces.before = raw;
  173. }
  174. }, {
  175. key: "rawSpaceAfter",
  176. get: function get() {
  177. var rawSpace = this.raws && this.raws.spaces && this.raws.spaces.after;
  178. if (rawSpace === undefined) {
  179. rawSpace = this.spaces.after;
  180. }
  181. return rawSpace || "";
  182. },
  183. set: function set(raw) {
  184. (0, _util.ensureObject)(this, "raws", "spaces");
  185. this.raws.spaces.after = raw;
  186. }
  187. }]);
  188. return Node;
  189. }();
  190. exports["default"] = Node;
  191. module.exports = exports.default;