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.

215 lines
7.7 KiB

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