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.

397 lines
9.8 KiB

  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _node = _interopRequireDefault(require("./node"));
  5. var types = _interopRequireWildcard(require("./types"));
  6. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  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. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  11. var Container =
  12. /*#__PURE__*/
  13. function (_Node) {
  14. _inheritsLoose(Container, _Node);
  15. function Container(opts) {
  16. var _this;
  17. _this = _Node.call(this, opts) || this;
  18. if (!_this.nodes) {
  19. _this.nodes = [];
  20. }
  21. return _this;
  22. }
  23. var _proto = Container.prototype;
  24. _proto.append = function append(selector) {
  25. selector.parent = this;
  26. this.nodes.push(selector);
  27. return this;
  28. };
  29. _proto.prepend = function prepend(selector) {
  30. selector.parent = this;
  31. this.nodes.unshift(selector);
  32. return this;
  33. };
  34. _proto.at = function at(index) {
  35. return this.nodes[index];
  36. };
  37. _proto.index = function index(child) {
  38. if (typeof child === 'number') {
  39. return child;
  40. }
  41. return this.nodes.indexOf(child);
  42. };
  43. _proto.removeChild = function removeChild(child) {
  44. child = this.index(child);
  45. this.at(child).parent = undefined;
  46. this.nodes.splice(child, 1);
  47. var index;
  48. for (var id in this.indexes) {
  49. index = this.indexes[id];
  50. if (index >= child) {
  51. this.indexes[id] = index - 1;
  52. }
  53. }
  54. return this;
  55. };
  56. _proto.removeAll = function removeAll() {
  57. for (var _iterator = this.nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  58. var _ref;
  59. if (_isArray) {
  60. if (_i >= _iterator.length) break;
  61. _ref = _iterator[_i++];
  62. } else {
  63. _i = _iterator.next();
  64. if (_i.done) break;
  65. _ref = _i.value;
  66. }
  67. var node = _ref;
  68. node.parent = undefined;
  69. }
  70. this.nodes = [];
  71. return this;
  72. };
  73. _proto.empty = function empty() {
  74. return this.removeAll();
  75. };
  76. _proto.insertAfter = function insertAfter(oldNode, newNode) {
  77. newNode.parent = this;
  78. var oldIndex = this.index(oldNode);
  79. this.nodes.splice(oldIndex + 1, 0, newNode);
  80. newNode.parent = this;
  81. var index;
  82. for (var id in this.indexes) {
  83. index = this.indexes[id];
  84. if (oldIndex <= index) {
  85. this.indexes[id] = index + 1;
  86. }
  87. }
  88. return this;
  89. };
  90. _proto.insertBefore = function insertBefore(oldNode, newNode) {
  91. newNode.parent = this;
  92. var oldIndex = this.index(oldNode);
  93. this.nodes.splice(oldIndex, 0, newNode);
  94. newNode.parent = this;
  95. var index;
  96. for (var id in this.indexes) {
  97. index = this.indexes[id];
  98. if (index <= oldIndex) {
  99. this.indexes[id] = index + 1;
  100. }
  101. }
  102. return this;
  103. };
  104. _proto._findChildAtPosition = function _findChildAtPosition(line, col) {
  105. var found = undefined;
  106. this.each(function (node) {
  107. if (node.atPosition) {
  108. var foundChild = node.atPosition(line, col);
  109. if (foundChild) {
  110. found = foundChild;
  111. return false;
  112. }
  113. } else if (node.isAtPosition(line, col)) {
  114. found = node;
  115. return false;
  116. }
  117. });
  118. return found;
  119. }
  120. /**
  121. * Return the most specific node at the line and column number given.
  122. * The source location is based on the original parsed location, locations aren't
  123. * updated as selector nodes are mutated.
  124. *
  125. * Note that this location is relative to the location of the first character
  126. * of the selector, and not the location of the selector in the overall document
  127. * when used in conjunction with postcss.
  128. *
  129. * If not found, returns undefined.
  130. * @param {number} line The line number of the node to find. (1-based index)
  131. * @param {number} col The column number of the node to find. (1-based index)
  132. */
  133. ;
  134. _proto.atPosition = function atPosition(line, col) {
  135. if (this.isAtPosition(line, col)) {
  136. return this._findChildAtPosition(line, col) || this;
  137. } else {
  138. return undefined;
  139. }
  140. };
  141. _proto._inferEndPosition = function _inferEndPosition() {
  142. if (this.last && this.last.source && this.last.source.end) {
  143. this.source = this.source || {};
  144. this.source.end = this.source.end || {};
  145. Object.assign(this.source.end, this.last.source.end);
  146. }
  147. };
  148. _proto.each = function each(callback) {
  149. if (!this.lastEach) {
  150. this.lastEach = 0;
  151. }
  152. if (!this.indexes) {
  153. this.indexes = {};
  154. }
  155. this.lastEach++;
  156. var id = this.lastEach;
  157. this.indexes[id] = 0;
  158. if (!this.length) {
  159. return undefined;
  160. }
  161. var index, result;
  162. while (this.indexes[id] < this.length) {
  163. index = this.indexes[id];
  164. result = callback(this.at(index), index);
  165. if (result === false) {
  166. break;
  167. }
  168. this.indexes[id] += 1;
  169. }
  170. delete this.indexes[id];
  171. if (result === false) {
  172. return false;
  173. }
  174. };
  175. _proto.walk = function walk(callback) {
  176. return this.each(function (node, i) {
  177. var result = callback(node, i);
  178. if (result !== false && node.length) {
  179. result = node.walk(callback);
  180. }
  181. if (result === false) {
  182. return false;
  183. }
  184. });
  185. };
  186. _proto.walkAttributes = function walkAttributes(callback) {
  187. var _this2 = this;
  188. return this.walk(function (selector) {
  189. if (selector.type === types.ATTRIBUTE) {
  190. return callback.call(_this2, selector);
  191. }
  192. });
  193. };
  194. _proto.walkClasses = function walkClasses(callback) {
  195. var _this3 = this;
  196. return this.walk(function (selector) {
  197. if (selector.type === types.CLASS) {
  198. return callback.call(_this3, selector);
  199. }
  200. });
  201. };
  202. _proto.walkCombinators = function walkCombinators(callback) {
  203. var _this4 = this;
  204. return this.walk(function (selector) {
  205. if (selector.type === types.COMBINATOR) {
  206. return callback.call(_this4, selector);
  207. }
  208. });
  209. };
  210. _proto.walkComments = function walkComments(callback) {
  211. var _this5 = this;
  212. return this.walk(function (selector) {
  213. if (selector.type === types.COMMENT) {
  214. return callback.call(_this5, selector);
  215. }
  216. });
  217. };
  218. _proto.walkIds = function walkIds(callback) {
  219. var _this6 = this;
  220. return this.walk(function (selector) {
  221. if (selector.type === types.ID) {
  222. return callback.call(_this6, selector);
  223. }
  224. });
  225. };
  226. _proto.walkNesting = function walkNesting(callback) {
  227. var _this7 = this;
  228. return this.walk(function (selector) {
  229. if (selector.type === types.NESTING) {
  230. return callback.call(_this7, selector);
  231. }
  232. });
  233. };
  234. _proto.walkPseudos = function walkPseudos(callback) {
  235. var _this8 = this;
  236. return this.walk(function (selector) {
  237. if (selector.type === types.PSEUDO) {
  238. return callback.call(_this8, selector);
  239. }
  240. });
  241. };
  242. _proto.walkTags = function walkTags(callback) {
  243. var _this9 = this;
  244. return this.walk(function (selector) {
  245. if (selector.type === types.TAG) {
  246. return callback.call(_this9, selector);
  247. }
  248. });
  249. };
  250. _proto.walkUniversals = function walkUniversals(callback) {
  251. var _this10 = this;
  252. return this.walk(function (selector) {
  253. if (selector.type === types.UNIVERSAL) {
  254. return callback.call(_this10, selector);
  255. }
  256. });
  257. };
  258. _proto.split = function split(callback) {
  259. var _this11 = this;
  260. var current = [];
  261. return this.reduce(function (memo, node, index) {
  262. var split = callback.call(_this11, node);
  263. current.push(node);
  264. if (split) {
  265. memo.push(current);
  266. current = [];
  267. } else if (index === _this11.length - 1) {
  268. memo.push(current);
  269. }
  270. return memo;
  271. }, []);
  272. };
  273. _proto.map = function map(callback) {
  274. return this.nodes.map(callback);
  275. };
  276. _proto.reduce = function reduce(callback, memo) {
  277. return this.nodes.reduce(callback, memo);
  278. };
  279. _proto.every = function every(callback) {
  280. return this.nodes.every(callback);
  281. };
  282. _proto.some = function some(callback) {
  283. return this.nodes.some(callback);
  284. };
  285. _proto.filter = function filter(callback) {
  286. return this.nodes.filter(callback);
  287. };
  288. _proto.sort = function sort(callback) {
  289. return this.nodes.sort(callback);
  290. };
  291. _proto.toString = function toString() {
  292. return this.map(String).join('');
  293. };
  294. _createClass(Container, [{
  295. key: "first",
  296. get: function get() {
  297. return this.at(0);
  298. }
  299. }, {
  300. key: "last",
  301. get: function get() {
  302. return this.at(this.length - 1);
  303. }
  304. }, {
  305. key: "length",
  306. get: function get() {
  307. return this.nodes.length;
  308. }
  309. }]);
  310. return Container;
  311. }(_node.default);
  312. exports.default = Container;
  313. module.exports = exports.default;