web 3d图形渲染器
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.

240 lines
6.5 KiB

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