123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- 'use strict';
- exports.__esModule = true;
- 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; };
- var _cssSyntaxError = require('./css-syntax-error');
- var _cssSyntaxError2 = _interopRequireDefault(_cssSyntaxError);
- var _stringifier = require('./stringifier');
- var _stringifier2 = _interopRequireDefault(_stringifier);
- var _stringify = require('./stringify');
- var _stringify2 = _interopRequireDefault(_stringify);
- var _warnOnce = require('./warn-once');
- var _warnOnce2 = _interopRequireDefault(_warnOnce);
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
- var cloneNode = function cloneNode(obj, parent) {
- var cloned = new obj.constructor();
- for (var i in obj) {
- if (!obj.hasOwnProperty(i)) continue;
- var value = obj[i];
- var type = typeof value === 'undefined' ? 'undefined' : _typeof(value);
- if (i === 'parent' && type === 'object') {
- if (parent) cloned[i] = parent;
- } else if (i === 'source') {
- cloned[i] = value;
- } else if (value instanceof Array) {
- cloned[i] = value.map(function (j) {
- return cloneNode(j, cloned);
- });
- } else {
- if (type === 'object' && value !== null) value = cloneNode(value);
- cloned[i] = value;
- }
- }
- return cloned;
- };
- var Node = function () {
-
- function Node() {
- var defaults = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- _classCallCheck(this, Node);
- this.raws = {};
- if ((typeof defaults === 'undefined' ? 'undefined' : _typeof(defaults)) !== 'object' && typeof defaults !== 'undefined') {
- throw new Error('PostCSS nodes constructor accepts object, not ' + JSON.stringify(defaults));
- }
- for (var name in defaults) {
- this[name] = defaults[name];
- }
- }
-
- Node.prototype.error = function error(message) {
- var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
- if (this.source) {
- var pos = this.positionBy(opts);
- return this.source.input.error(message, pos.line, pos.column, opts);
- } else {
- return new _cssSyntaxError2.default(message);
- }
- };
-
- Node.prototype.warn = function warn(result, text, opts) {
- var data = { node: this };
- for (var i in opts) {
- data[i] = opts[i];
- }return result.warn(text, data);
- };
-
- Node.prototype.remove = function remove() {
- if (this.parent) {
- this.parent.removeChild(this);
- }
- this.parent = undefined;
- return this;
- };
-
- Node.prototype.toString = function toString() {
- var stringifier = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _stringify2.default;
- if (stringifier.stringify) stringifier = stringifier.stringify;
- var result = '';
- stringifier(this, function (i) {
- result += i;
- });
- return result;
- };
-
- Node.prototype.clone = function clone() {
- var overrides = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- var cloned = cloneNode(this);
- for (var name in overrides) {
- cloned[name] = overrides[name];
- }
- return cloned;
- };
-
- Node.prototype.cloneBefore = function cloneBefore() {
- var overrides = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- var cloned = this.clone(overrides);
- this.parent.insertBefore(this, cloned);
- return cloned;
- };
-
- Node.prototype.cloneAfter = function cloneAfter() {
- var overrides = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- var cloned = this.clone(overrides);
- this.parent.insertAfter(this, cloned);
- return cloned;
- };
-
- Node.prototype.replaceWith = function replaceWith() {
- if (this.parent) {
- for (var _len = arguments.length, nodes = Array(_len), _key = 0; _key < _len; _key++) {
- nodes[_key] = arguments[_key];
- }
- for (var _iterator = nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
- var _ref;
- if (_isArray) {
- if (_i >= _iterator.length) break;
- _ref = _iterator[_i++];
- } else {
- _i = _iterator.next();
- if (_i.done) break;
- _ref = _i.value;
- }
- var node = _ref;
- this.parent.insertBefore(this, node);
- }
- this.remove();
- }
- return this;
- };
- Node.prototype.moveTo = function moveTo(newParent) {
- (0, _warnOnce2.default)('Node#moveTo was deprecated. Use Container#append.');
- this.cleanRaws(this.root() === newParent.root());
- this.remove();
- newParent.append(this);
- return this;
- };
- Node.prototype.moveBefore = function moveBefore(otherNode) {
- (0, _warnOnce2.default)('Node#moveBefore was deprecated. Use Node#before.');
- this.cleanRaws(this.root() === otherNode.root());
- this.remove();
- otherNode.parent.insertBefore(otherNode, this);
- return this;
- };
- Node.prototype.moveAfter = function moveAfter(otherNode) {
- (0, _warnOnce2.default)('Node#moveAfter was deprecated. Use Node#after.');
- this.cleanRaws(this.root() === otherNode.root());
- this.remove();
- otherNode.parent.insertAfter(otherNode, this);
- return this;
- };
-
- Node.prototype.next = function next() {
- if (!this.parent) return undefined;
- var index = this.parent.index(this);
- return this.parent.nodes[index + 1];
- };
-
- Node.prototype.prev = function prev() {
- if (!this.parent) return undefined;
- var index = this.parent.index(this);
- return this.parent.nodes[index - 1];
- };
-
- Node.prototype.before = function before(add) {
- this.parent.insertBefore(this, add);
- return this;
- };
-
- Node.prototype.after = function after(add) {
- this.parent.insertAfter(this, add);
- return this;
- };
- Node.prototype.toJSON = function toJSON() {
- var fixed = {};
- for (var name in this) {
- if (!this.hasOwnProperty(name)) continue;
- if (name === 'parent') continue;
- var value = this[name];
- if (value instanceof Array) {
- fixed[name] = value.map(function (i) {
- if ((typeof i === 'undefined' ? 'undefined' : _typeof(i)) === 'object' && i.toJSON) {
- return i.toJSON();
- } else {
- return i;
- }
- });
- } else if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && value.toJSON) {
- fixed[name] = value.toJSON();
- } else {
- fixed[name] = value;
- }
- }
- return fixed;
- };
-
- Node.prototype.raw = function raw(prop, defaultType) {
- var str = new _stringifier2.default();
- return str.raw(this, prop, defaultType);
- };
-
- Node.prototype.root = function root() {
- var result = this;
- while (result.parent) {
- result = result.parent;
- }return result;
- };
- Node.prototype.cleanRaws = function cleanRaws(keepBetween) {
- delete this.raws.before;
- delete this.raws.after;
- if (!keepBetween) delete this.raws.between;
- };
- Node.prototype.positionInside = function positionInside(index) {
- var string = this.toString();
- var column = this.source.start.column;
- var line = this.source.start.line;
- for (var i = 0; i < index; i++) {
- if (string[i] === '\n') {
- column = 1;
- line += 1;
- } else {
- column += 1;
- }
- }
- return { line: line, column: column };
- };
- Node.prototype.positionBy = function positionBy(opts) {
- var pos = this.source.start;
- if (opts.index) {
- pos = this.positionInside(opts.index);
- } else if (opts.word) {
- var index = this.toString().indexOf(opts.word);
- if (index !== -1) pos = this.positionInside(index);
- }
- return pos;
- };
-
-
-
-
- return Node;
- }();
- exports.default = Node;
- module.exports = exports['default'];
|