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.

869 lines
65 KiB

4 years ago
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _declaration = _interopRequireDefault(require("./declaration"));
  5. var _comment = _interopRequireDefault(require("./comment"));
  6. var _node = _interopRequireDefault(require("./node"));
  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. function cleanSource(nodes) {
  12. return nodes.map(function (i) {
  13. if (i.nodes) i.nodes = cleanSource(i.nodes);
  14. delete i.source;
  15. return i;
  16. });
  17. }
  18. /**
  19. * The {@link Root}, {@link AtRule}, and {@link Rule} container nodes
  20. * inherit some common methods to help work with their children.
  21. *
  22. * Note that all containers can store any content. If you write a rule inside
  23. * a rule, PostCSS will parse it.
  24. *
  25. * @extends Node
  26. * @abstract
  27. */
  28. var Container =
  29. /*#__PURE__*/
  30. function (_Node) {
  31. _inheritsLoose(Container, _Node);
  32. function Container() {
  33. return _Node.apply(this, arguments) || this;
  34. }
  35. var _proto = Container.prototype;
  36. _proto.push = function push(child) {
  37. child.parent = this;
  38. this.nodes.push(child);
  39. return this;
  40. }
  41. /**
  42. * Iterates through the containers immediate children,
  43. * calling `callback` for each child.
  44. *
  45. * Returning `false` in the callback will break iteration.
  46. *
  47. * This method only iterates through the containers immediate children.
  48. * If you need to recursively iterate through all the containers descendant
  49. * nodes, use {@link Container#walk}.
  50. *
  51. * Unlike the for `{}`-cycle or `Array#forEach` this iterator is safe
  52. * if you are mutating the array of child nodes during iteration.
  53. * PostCSS will adjust the current index to match the mutations.
  54. *
  55. * @param {childIterator} callback Iterator receives each node and index.
  56. *
  57. * @return {false|undefined} Returns `false` if iteration was broke.
  58. *
  59. * @example
  60. * const root = postcss.parse('a { color: black; z-index: 1 }')
  61. * const rule = root.first
  62. *
  63. * for (const decl of rule.nodes) {
  64. * decl.cloneBefore({ prop: '-webkit-' + decl.prop })
  65. * // Cycle will be infinite, because cloneBefore moves the current node
  66. * // to the next index
  67. * }
  68. *
  69. * rule.each(decl => {
  70. * decl.cloneBefore({ prop: '-webkit-' + decl.prop })
  71. * // Will be executed only for color and z-index
  72. * })
  73. */
  74. ;
  75. _proto.each = function each(callback) {
  76. if (!this.lastEach) this.lastEach = 0;
  77. if (!this.indexes) this.indexes = {};
  78. this.lastEach += 1;
  79. var id = this.lastEach;
  80. this.indexes[id] = 0;
  81. if (!this.nodes) return undefined;
  82. var index, result;
  83. while (this.indexes[id] < this.nodes.length) {
  84. index = this.indexes[id];
  85. result = callback(this.nodes[index], index);
  86. if (result === false) break;
  87. this.indexes[id] += 1;
  88. }
  89. delete this.indexes[id];
  90. return result;
  91. }
  92. /**
  93. * Traverses the containers descendant nodes, calling callback
  94. * for each node.
  95. *
  96. * Like container.each(), this method is safe to use
  97. * if you are mutating arrays during iteration.
  98. *
  99. * If you only need to iterate through the containers immediate children,
  100. * use {@link Container#each}.
  101. *
  102. * @param {childIterator} callback Iterator receives each node and index.
  103. *
  104. * @return {false|undefined} Returns `false` if iteration was broke.
  105. *
  106. * @example
  107. * root.walk(node => {
  108. * // Traverses all descendant nodes.
  109. * })
  110. */
  111. ;
  112. _proto.walk = function walk(callback) {
  113. return this.each(function (child, i) {
  114. var result;
  115. try {
  116. result = callback(child, i);
  117. } catch (e) {
  118. e.postcssNode = child;
  119. if (e.stack && child.source && /\n\s{4}at /.test(e.stack)) {
  120. var s = child.source;
  121. e.stack = e.stack.replace(/\n\s{4}at /, "$&" + s.input.from + ":" + s.start.line + ":" + s.start.column + "$&");
  122. }
  123. throw e;
  124. }
  125. if (result !== false && child.walk) {
  126. result = child.walk(callback);
  127. }
  128. return result;
  129. });
  130. }
  131. /**
  132. * Traverses the containers descendant nodes, calling callback
  133. * for each declaration node.
  134. *
  135. * If you pass a filter, iteration will only happen over declarations
  136. * with matching properties.
  137. *
  138. * Like {@link Container#each}, this method is safe
  139. * to use if you are mutating arrays during iteration.
  140. *
  141. * @param {string|RegExp} [prop] String or regular expression
  142. * to filter declarations by property name.
  143. * @param {childIterator} callback Iterator receives each node and index.
  144. *
  145. * @return {false|undefined} Returns `false` if iteration was broke.
  146. *
  147. * @example
  148. * root.walkDecls(decl => {
  149. * checkPropertySupport(decl.prop)
  150. * })
  151. *
  152. * root.walkDecls('border-radius', decl => {
  153. * decl.remove()
  154. * })
  155. *
  156. * root.walkDecls(/^background/, decl => {
  157. * decl.value = takeFirstColorFromGradient(decl.value)
  158. * })
  159. */
  160. ;
  161. _proto.walkDecls = function walkDecls(prop, callback) {
  162. if (!callback) {
  163. callback = prop;
  164. return this.walk(function (child, i) {
  165. if (child.type === 'decl') {
  166. return callback(child, i);
  167. }
  168. });
  169. }
  170. if (prop instanceof RegExp) {
  171. return this.walk(function (child, i) {
  172. if (child.type === 'decl' && prop.test(child.prop)) {
  173. return callback(child, i);
  174. }
  175. });
  176. }
  177. return this.walk(function (child, i) {
  178. if (child.type === 'decl' && child.prop === prop) {
  179. return callback(child, i);
  180. }
  181. });
  182. }
  183. /**
  184. * Traverses the containers descendant nodes, calling callback
  185. * for each rule node.
  186. *
  187. * If you pass a filter, iteration will only happen over rules
  188. * with matching selectors.
  189. *
  190. * Like {@link Container#each}, this method is safe
  191. * to use if you are mutating arrays during iteration.
  192. *
  193. * @param {string|RegExp} [selector] String or regular expression
  194. * to filter rules by selector.
  195. * @param {childIterator} callback Iterator receives each node and index.
  196. *
  197. * @return {false|undefined} returns `false` if iteration was broke.
  198. *
  199. * @example
  200. * const selectors = []
  201. * root.walkRules(rule => {
  202. * selectors.push(rule.selector)
  203. * })
  204. * console.log(`Your CSS uses ${ selectors.length } selectors`)
  205. */
  206. ;
  207. _proto.walkRules = function walkRules(selector, callback) {
  208. if (!callback) {
  209. callback = selector;
  210. return this.walk(function (child, i) {
  211. if (child.type === 'rule') {
  212. return callback(child, i);
  213. }
  214. });
  215. }
  216. if (selector instanceof RegExp) {
  217. return this.walk(function (child, i) {
  218. if (child.type === 'rule' && selector.test(child.selector)) {
  219. return callback(child, i);
  220. }
  221. });
  222. }
  223. return this.walk(function (child, i) {
  224. if (child.type === 'rule' && child.selector === selector) {
  225. return callback(child, i);
  226. }
  227. });
  228. }
  229. /**
  230. * Traverses the containers descendant nodes, calling callback
  231. * for each at-rule node.
  232. *
  233. * If you pass a filter, iteration will only happen over at-rules
  234. * that have matching names.
  235. *
  236. * Like {@link Container#each}, this method is safe
  237. * to use if you are mutating arrays during iteration.
  238. *
  239. * @param {string|RegExp} [name] String or regular expression
  240. * to filter at-rules by name.
  241. * @param {childIterator} callback Iterator receives each node and index.
  242. *
  243. * @return {false|undefined} Returns `false` if iteration was broke.
  244. *
  245. * @example
  246. * root.walkAtRules(rule => {
  247. * if (isOld(rule.name)) rule.remove()
  248. * })
  249. *
  250. * let first = false
  251. * root.walkAtRules('charset', rule => {
  252. * if (!first) {
  253. * first = true
  254. * } else {
  255. * rule.remove()
  256. * }
  257. * })
  258. */
  259. ;
  260. _proto.walkAtRules = function walkAtRules(name, callback) {
  261. if (!callback) {
  262. callback = name;
  263. return this.walk(function (child, i) {
  264. if (child.type === 'atrule') {
  265. return callback(child, i);
  266. }
  267. });
  268. }
  269. if (name instanceof RegExp) {
  270. return this.walk(function (child, i) {
  271. if (child.type === 'atrule' && name.test(child.name)) {
  272. return callback(child, i);
  273. }
  274. });
  275. }
  276. return this.walk(function (child, i) {
  277. if (child.type === 'atrule' && child.name === name) {
  278. return callback(child, i);
  279. }
  280. });
  281. }
  282. /**
  283. * Traverses the containers descendant nodes, calling callback
  284. * for each comment node.
  285. *
  286. * Like {@link Container#each}, this method is safe
  287. * to use if you are mutating arrays during iteration.
  288. *
  289. * @param {childIterator} callback Iterator receives each node and index.
  290. *
  291. * @return {false|undefined} Returns `false` if iteration was broke.
  292. *
  293. * @example
  294. * root.walkComments(comment => {
  295. * comment.remove()
  296. * })
  297. */
  298. ;
  299. _proto.walkComments = function walkComments(callback) {
  300. return this.walk(function (child, i) {
  301. if (child.type === 'comment') {
  302. return callback(child, i);
  303. }
  304. });
  305. }
  306. /**
  307. * Inserts new nodes to the end of the container.
  308. *
  309. * @param {...(Node|object|string|Node[])} children New nodes.
  310. *
  311. * @return {Node} This node for methods chain.
  312. *
  313. * @example
  314. * const decl1 = postcss.decl({ prop: 'color', value: 'black' })
  315. * const decl2 = postcss.decl({ prop: 'background-color', value: 'white' })
  316. * rule.append(decl1, decl2)
  317. *
  318. * root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
  319. * root.append({ selector: 'a' }) // rule
  320. * rule.append({ prop: 'color', value: 'black' }) // declaration
  321. * rule.append({ text: 'Comment' }) // comment
  322. *
  323. * root.append('a {}')
  324. * root.first.append('color: black; z-index: 1')
  325. */
  326. ;
  327. _proto.append = function append() {
  328. for (var _len = arguments.length, children = new Array(_len), _key = 0; _key < _len; _key++) {
  329. children[_key] = arguments[_key];
  330. }
  331. for (var _i = 0, _children = children; _i < _children.length; _i++) {
  332. var child = _children[_i];
  333. var nodes = this.normalize(child, this.last);
  334. for (var _iterator = nodes, _isArray = Array.isArray(_iterator), _i2 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  335. var _ref;
  336. if (_isArray) {
  337. if (_i2 >= _iterator.length) break;
  338. _ref = _iterator[_i2++];
  339. } else {
  340. _i2 = _iterator.next();
  341. if (_i2.done) break;
  342. _ref = _i2.value;
  343. }
  344. var node = _ref;
  345. this.nodes.push(node);
  346. }
  347. }
  348. return this;
  349. }
  350. /**
  351. * Inserts new nodes to the start of the container.
  352. *
  353. * @param {...(Node|object|string|Node[])} children New nodes.
  354. *
  355. * @return {Node} This node for methods chain.
  356. *
  357. * @example
  358. * const decl1 = postcss.decl({ prop: 'color', value: 'black' })
  359. * const decl2 = postcss.decl({ prop: 'background-color', value: 'white' })
  360. * rule.prepend(decl1, decl2)
  361. *
  362. * root.append({ name: 'charset', params: '"UTF-8"' }) // at-rule
  363. * root.append({ selector: 'a' }) // rule
  364. * rule.append({ prop: 'color', value: 'black' }) // declaration
  365. * rule.append({ text: 'Comment' }) // comment
  366. *
  367. * root.append('a {}')
  368. * root.first.append('color: black; z-index: 1')
  369. */
  370. ;
  371. _proto.prepend = function prepend() {
  372. for (var _len2 = arguments.length, children = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  373. children[_key2] = arguments[_key2];
  374. }
  375. children = children.reverse();
  376. for (var _iterator2 = children, _isArray2 = Array.isArray(_iterator2), _i3 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  377. var _ref2;
  378. if (_isArray2) {
  379. if (_i3 >= _iterator2.length) break;
  380. _ref2 = _iterator2[_i3++];
  381. } else {
  382. _i3 = _iterator2.next();
  383. if (_i3.done) break;
  384. _ref2 = _i3.value;
  385. }
  386. var child = _ref2;
  387. var nodes = this.normalize(child, this.first, 'prepend').reverse();
  388. for (var _iterator3 = nodes, _isArray3 = Array.isArray(_iterator3), _i4 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
  389. var _ref3;
  390. if (_isArray3) {
  391. if (_i4 >= _iterator3.length) break;
  392. _ref3 = _iterator3[_i4++];
  393. } else {
  394. _i4 = _iterator3.next();
  395. if (_i4.done) break;
  396. _ref3 = _i4.value;
  397. }
  398. var node = _ref3;
  399. this.nodes.unshift(node);
  400. }
  401. for (var id in this.indexes) {
  402. this.indexes[id] = this.indexes[id] + nodes.length;
  403. }
  404. }
  405. return this;
  406. };
  407. _proto.cleanRaws = function cleanRaws(keepBetween) {
  408. _Node.prototype.cleanRaws.call(this, keepBetween);
  409. if (this.nodes) {
  410. for (var _iterator4 = this.nodes, _isArray4 = Array.isArray(_iterator4), _i5 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
  411. var _ref4;
  412. if (_isArray4) {
  413. if (_i5 >= _iterator4.length) break;
  414. _ref4 = _iterator4[_i5++];
  415. } else {
  416. _i5 = _iterator4.next();
  417. if (_i5.done) break;
  418. _ref4 = _i5.value;
  419. }
  420. var node = _ref4;
  421. node.cleanRaws(keepBetween);
  422. }
  423. }
  424. }
  425. /**
  426. * Insert new node before old node within the container.
  427. *
  428. * @param {Node|number} exist Child or childs index.
  429. * @param {Node|object|string|Node[]} add New node.
  430. *
  431. * @return {Node} This node for methods chain.
  432. *
  433. * @example
  434. * rule.insertBefore(decl, decl.clone({ prop: '-webkit-' + decl.prop }))
  435. */
  436. ;
  437. _proto.insertBefore = function insertBefore(exist, add) {
  438. exist = this.index(exist);
  439. var type = exist === 0 ? 'prepend' : false;
  440. var nodes = this.normalize(add, this.nodes[exist], type).reverse();
  441. for (var _iterator5 = nodes, _isArray5 = Array.isArray(_iterator5), _i6 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
  442. var _ref5;
  443. if (_isArray5) {
  444. if (_i6 >= _iterator5.length) break;
  445. _ref5 = _iterator5[_i6++];
  446. } else {
  447. _i6 = _iterator5.next();
  448. if (_i6.done) break;
  449. _ref5 = _i6.value;
  450. }
  451. var node = _ref5;
  452. this.nodes.splice(exist, 0, node);
  453. }
  454. var index;
  455. for (var id in this.indexes) {
  456. index = this.indexes[id];
  457. if (exist <= index) {
  458. this.indexes[id] = index + nodes.length;
  459. }
  460. }
  461. return this;
  462. }
  463. /**
  464. * Insert new node after old node within the container.
  465. *
  466. * @param {Node|number} exist Child or childs index.
  467. * @param {Node|object|string|Node[]} add New node.
  468. *
  469. * @return {Node} This node for methods chain.
  470. */
  471. ;
  472. _proto.insertAfter = function insertAfter(exist, add) {
  473. exist = this.index(exist);
  474. var nodes = this.normalize(add, this.nodes[exist]).reverse();
  475. for (var _iterator6 = nodes, _isArray6 = Array.isArray(_iterator6), _i7 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
  476. var _ref6;
  477. if (_isArray6) {
  478. if (_i7 >= _iterator6.length) break;
  479. _ref6 = _iterator6[_i7++];
  480. } else {
  481. _i7 = _iterator6.next();
  482. if (_i7.done) break;
  483. _ref6 = _i7.value;
  484. }
  485. var node = _ref6;
  486. this.nodes.splice(exist + 1, 0, node);
  487. }
  488. var index;
  489. for (var id in this.indexes) {
  490. index = this.indexes[id];
  491. if (exist < index) {
  492. this.indexes[id] = index + nodes.length;
  493. }
  494. }
  495. return this;
  496. }
  497. /**
  498. * Removes node from the container and cleans the parent properties
  499. * from the node and its children.
  500. *
  501. * @param {Node|number} child Child or childs index.
  502. *
  503. * @return {Node} This node for methods chain
  504. *
  505. * @example
  506. * rule.nodes.length //=> 5
  507. * rule.removeChild(decl)
  508. * rule.nodes.length //=> 4
  509. * decl.parent //=> undefined
  510. */
  511. ;
  512. _proto.removeChild = function removeChild(child) {
  513. child = this.index(child);
  514. this.nodes[child].parent = undefined;
  515. this.nodes.splice(child, 1);
  516. var index;
  517. for (var id in this.indexes) {
  518. index = this.indexes[id];
  519. if (index >= child) {
  520. this.indexes[id] = index - 1;
  521. }
  522. }
  523. return this;
  524. }
  525. /**
  526. * Removes all children from the container
  527. * and cleans their parent properties.
  528. *
  529. * @return {Node} This node for methods chain.
  530. *
  531. * @example
  532. * rule.removeAll()
  533. * rule.nodes.length //=> 0
  534. */
  535. ;
  536. _proto.removeAll = function removeAll() {
  537. for (var _iterator7 = this.nodes, _isArray7 = Array.isArray(_iterator7), _i8 = 0, _iterator7 = _isArray7 ? _iterator7 : _iterator7[Symbol.iterator]();;) {
  538. var _ref7;
  539. if (_isArray7) {
  540. if (_i8 >= _iterator7.length) break;
  541. _ref7 = _iterator7[_i8++];
  542. } else {
  543. _i8 = _iterator7.next();
  544. if (_i8.done) break;
  545. _ref7 = _i8.value;
  546. }
  547. var node = _ref7;
  548. node.parent = undefined;
  549. }
  550. this.nodes = [];
  551. return this;
  552. }
  553. /**
  554. * Passes all declaration values within the container that match pattern
  555. * through callback, replacing those values with the returned result
  556. * of callback.
  557. *
  558. * This method is useful if you are using a custom unit or function
  559. * and need to iterate through all values.
  560. *
  561. * @param {string|RegExp} pattern Replace pattern.
  562. * @param {object} opts Options to speed up the search.
  563. * @param {string|string[]} opts.props An array of property names.
  564. * @param {string} opts.fast String thats used to narrow down
  565. * values and speed up the regexp search.
  566. * @param {function|string} callback String to replace pattern or callback
  567. * that returns a new value. The callback
  568. * will receive the same arguments
  569. * as those passed to a function parameter
  570. * of `String#replace`.
  571. *
  572. * @return {Node} This node for methods chain.
  573. *
  574. * @example
  575. * root.replaceValues(/\d+rem/, { fast: 'rem' }, string => {
  576. * return 15 * parseInt(string) + 'px'
  577. * })
  578. */
  579. ;
  580. _proto.replaceValues = function replaceValues(pattern, opts, callback) {
  581. if (!callback) {
  582. callback = opts;
  583. opts = {};
  584. }
  585. this.walkDecls(function (decl) {
  586. if (opts.props && opts.props.indexOf(decl.prop) === -1) return;
  587. if (opts.fast && decl.value.indexOf(opts.fast) === -1) return;
  588. decl.value = decl.value.replace(pattern, callback);
  589. });
  590. return this;
  591. }
  592. /**
  593. * Returns `true` if callback returns `true`
  594. * for all of the containers children.
  595. *
  596. * @param {childCondition} condition Iterator returns true or false.
  597. *
  598. * @return {boolean} Is every child pass condition.
  599. *
  600. * @example
  601. * const noPrefixes = rule.every(i => i.prop[0] !== '-')
  602. */
  603. ;
  604. _proto.every = function every(condition) {
  605. return this.nodes.every(condition);
  606. }
  607. /**
  608. * Returns `true` if callback returns `true` for (at least) one
  609. * of the containers children.
  610. *
  611. * @param {childCondition} condition Iterator returns true or false.
  612. *
  613. * @return {boolean} Is some child pass condition.
  614. *
  615. * @example
  616. * const hasPrefix = rule.some(i => i.prop[0] === '-')
  617. */
  618. ;
  619. _proto.some = function some(condition) {
  620. return this.nodes.some(condition);
  621. }
  622. /**
  623. * Returns a `child`s index within the {@link Container#nodes} array.
  624. *
  625. * @param {Node} child Child of the current container.
  626. *
  627. * @return {number} Child index.
  628. *
  629. * @example
  630. * rule.index( rule.nodes[2] ) //=> 2
  631. */
  632. ;
  633. _proto.index = function index(child) {
  634. if (typeof child === 'number') {
  635. return child;
  636. }
  637. return this.nodes.indexOf(child);
  638. }
  639. /**
  640. * The containers first child.
  641. *
  642. * @type {Node}
  643. *
  644. * @example
  645. * rule.first === rules.nodes[0]
  646. */
  647. ;
  648. _proto.normalize = function normalize(nodes, sample) {
  649. var _this = this;
  650. if (typeof nodes === 'string') {
  651. var parse = require('./parse');
  652. nodes = cleanSource(parse(nodes).nodes);
  653. } else if (Array.isArray(nodes)) {
  654. nodes = nodes.slice(0);
  655. for (var _iterator8 = nodes, _isArray8 = Array.isArray(_iterator8), _i9 = 0, _iterator8 = _isArray8 ? _iterator8 : _iterator8[Symbol.iterator]();;) {
  656. var _ref8;
  657. if (_isArray8) {
  658. if (_i9 >= _iterator8.length) break;
  659. _ref8 = _iterator8[_i9++];
  660. } else {
  661. _i9 = _iterator8.next();
  662. if (_i9.done) break;
  663. _ref8 = _i9.value;
  664. }
  665. var i = _ref8;
  666. if (i.parent) i.parent.removeChild(i, 'ignore');
  667. }
  668. } else if (nodes.type === 'root') {
  669. nodes = nodes.nodes.slice(0);
  670. for (var _iterator9 = nodes, _isArray9 = Array.isArray(_iterator9), _i10 = 0, _iterator9 = _isArray9 ? _iterator9 : _iterator9[Symbol.iterator]();;) {
  671. var _ref9;
  672. if (_isArray9) {
  673. if (_i10 >= _iterator9.length) break;
  674. _ref9 = _iterator9[_i10++];
  675. } else {
  676. _i10 = _iterator9.next();
  677. if (_i10.done) break;
  678. _ref9 = _i10.value;
  679. }
  680. var _i11 = _ref9;
  681. if (_i11.parent) _i11.parent.removeChild(_i11, 'ignore');
  682. }
  683. } else if (nodes.type) {
  684. nodes = [nodes];
  685. } else if (nodes.prop) {
  686. if (typeof nodes.value === 'undefined') {
  687. throw new Error('Value field is missed in node creation');
  688. } else if (typeof nodes.value !== 'string') {
  689. nodes.value = String(nodes.value);
  690. }
  691. nodes = [new _declaration.default(nodes)];
  692. } else if (nodes.selector) {
  693. var Rule = require('./rule');
  694. nodes = [new Rule(nodes)];
  695. } else if (nodes.name) {
  696. var AtRule = require('./at-rule');
  697. nodes = [new AtRule(nodes)];
  698. } else if (nodes.text) {
  699. nodes = [new _comment.default(nodes)];
  700. } else {
  701. throw new Error('Unknown node type in node creation');
  702. }
  703. var processed = nodes.map(function (i) {
  704. if (i.parent) i.parent.removeChild(i);
  705. if (typeof i.raws.before === 'undefined') {
  706. if (sample && typeof sample.raws.before !== 'undefined') {
  707. i.raws.before = sample.raws.before.replace(/[^\s]/g, '');
  708. }
  709. }
  710. i.parent = _this;
  711. return i;
  712. });
  713. return processed;
  714. }
  715. /**
  716. * @memberof Container#
  717. * @member {Node[]} nodes An array containing the containers children.
  718. *
  719. * @example
  720. * const root = postcss.parse('a { color: black }')
  721. * root.nodes.length //=> 1
  722. * root.nodes[0].selector //=> 'a'
  723. * root.nodes[0].nodes[0].prop //=> 'color'
  724. */
  725. ;
  726. _createClass(Container, [{
  727. key: "first",
  728. get: function get() {
  729. if (!this.nodes) return undefined;
  730. return this.nodes[0];
  731. }
  732. /**
  733. * The containers last child.
  734. *
  735. * @type {Node}
  736. *
  737. * @example
  738. * rule.last === rule.nodes[rule.nodes.length - 1]
  739. */
  740. }, {
  741. key: "last",
  742. get: function get() {
  743. if (!this.nodes) return undefined;
  744. return this.nodes[this.nodes.length - 1];
  745. }
  746. }]);
  747. return Container;
  748. }(_node.default);
  749. var _default = Container;
  750. /**
  751. * @callback childCondition
  752. * @param {Node} node Container child.
  753. * @param {number} index Child index.
  754. * @param {Node[]} nodes All container children.
  755. * @return {boolean}
  756. */
  757. /**
  758. * @callback childIterator
  759. * @param {Node} node Container child.
  760. * @param {number} index Child index.
  761. * @return {false|undefined} Returning `false` will break iteration.
  762. */
  763. exports.default = _default;
  764. module.exports = exports.default;
  765. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNvbnRhaW5lci5lczYiXSwibmFtZXMiOlsiY2xlYW5Tb3VyY2UiLCJub2RlcyIsIm1hcCIsImkiLCJzb3VyY2UiLCJDb250YWluZXIiLCJwdXNoIiwiY2hpbGQiLCJwYXJlbnQiLCJlYWNoIiwiY2FsbGJhY2siLCJsYXN0RWFjaCIsImluZGV4ZXMiLCJpZCIsInVuZGVmaW5lZCIsImluZGV4IiwicmVzdWx0IiwibGVuZ3RoIiwid2FsayIsImUiLCJwb3N0Y3NzTm9kZSIsInN0YWNrIiwidGVzdCIsInMiLCJyZXBsYWNlIiwiaW5wdXQiLCJmcm9tIiwic3RhcnQiLCJsaW5lIiwiY29sdW1uIiwid2Fsa0RlY2xzIiwicHJvcCIsInR5cGUiLCJSZWdFeHAiLCJ3YWxrUnVsZXMiLCJzZWxlY3RvciIsIndhbGtBdFJ1bGVzIiwibmFtZSIsIndhbGtDb21tZW50cyIsImFwcGVuZCIsImNoaWxkcmVuIiwibm9ybWFsaXplIiwibGFzdCIsIm5vZGUiLCJwcmVwZW5kIiwicmV2ZXJzZSIsImZpcnN0IiwidW5zaGlmdCIsImNsZWFuUmF3cyIsImtlZXBCZXR3ZWVuIiwiaW5zZXJ0QmVmb3JlIiwiZXhpc3QiLCJhZGQiLCJzcGxpY2UiLCJpbnNlcnRBZnRlciIsInJlbW92ZUNoaWxkIiwicmVtb3ZlQWxsIiwicmVwbGFjZVZhbHVlcyIsInBhdHRlcm4iLCJvcHRzIiwiZGVjbCIsInByb3BzIiwiaW5kZXhPZiIsImZhc3QiLCJ2YWx1ZSIsImV2ZXJ5IiwiY29uZGl0aW9uIiwic29tZSIsInNhbXBsZSIsInB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