123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606 |
- "use strict";
- exports.__esModule = true;
- exports.default = void 0;
- var _cssSyntaxError = _interopRequireDefault(require("./css-syntax-error"));
- var _stringifier = _interopRequireDefault(require("./stringifier"));
- var _stringify = _interopRequireDefault(require("./stringify"));
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- 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;
- 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(defaults) {
- if (defaults === void 0) {
- defaults = {};
- }
- this.raws = {};
- if (process.env.NODE_ENV !== 'production') {
- if (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];
- }
- }
-
- var _proto = Node.prototype;
- _proto.error = function error(message, opts) {
- if (opts === void 0) {
- opts = {};
- }
- if (this.source) {
- var pos = this.positionBy(opts);
- return this.source.input.error(message, pos.line, pos.column, opts);
- }
- return new _cssSyntaxError.default(message);
- }
-
- ;
- _proto.warn = function warn(result, text, opts) {
- var data = {
- node: this
- };
- for (var i in opts) {
- data[i] = opts[i];
- }
- return result.warn(text, data);
- }
-
- ;
- _proto.remove = function remove() {
- if (this.parent) {
- this.parent.removeChild(this);
- }
- this.parent = undefined;
- return this;
- }
-
- ;
- _proto.toString = function toString(stringifier) {
- if (stringifier === void 0) {
- stringifier = _stringify.default;
- }
- if (stringifier.stringify) stringifier = stringifier.stringify;
- var result = '';
- stringifier(this, function (i) {
- result += i;
- });
- return result;
- }
-
- ;
- _proto.clone = function clone(overrides) {
- if (overrides === void 0) {
- overrides = {};
- }
- var cloned = cloneNode(this);
- for (var name in overrides) {
- cloned[name] = overrides[name];
- }
- return cloned;
- }
-
- ;
- _proto.cloneBefore = function cloneBefore(overrides) {
- if (overrides === void 0) {
- overrides = {};
- }
- var cloned = this.clone(overrides);
- this.parent.insertBefore(this, cloned);
- return cloned;
- }
-
- ;
- _proto.cloneAfter = function cloneAfter(overrides) {
- if (overrides === void 0) {
- overrides = {};
- }
- var cloned = this.clone(overrides);
- this.parent.insertAfter(this, cloned);
- return cloned;
- }
-
- ;
- _proto.replaceWith = function replaceWith() {
- if (this.parent) {
- for (var _len = arguments.length, nodes = new Array(_len), _key = 0; _key < _len; _key++) {
- nodes[_key] = arguments[_key];
- }
- for (var _i = 0, _nodes = nodes; _i < _nodes.length; _i++) {
- var node = _nodes[_i];
- this.parent.insertBefore(this, node);
- }
- this.remove();
- }
- return this;
- }
-
- ;
- _proto.next = function next() {
- if (!this.parent) return undefined;
- var index = this.parent.index(this);
- return this.parent.nodes[index + 1];
- }
-
- ;
- _proto.prev = function prev() {
- if (!this.parent) return undefined;
- var index = this.parent.index(this);
- return this.parent.nodes[index - 1];
- }
-
- ;
- _proto.before = function before(add) {
- this.parent.insertBefore(this, add);
- return this;
- }
-
- ;
- _proto.after = function after(add) {
- this.parent.insertAfter(this, add);
- return this;
- };
- _proto.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 === 'object' && i.toJSON) {
- return i.toJSON();
- } else {
- return i;
- }
- });
- } else if (typeof value === 'object' && value.toJSON) {
- fixed[name] = value.toJSON();
- } else {
- fixed[name] = value;
- }
- }
- return fixed;
- }
-
- ;
- _proto.raw = function raw(prop, defaultType) {
- var str = new _stringifier.default();
- return str.raw(this, prop, defaultType);
- }
-
- ;
- _proto.root = function root() {
- var result = this;
- while (result.parent) {
- result = result.parent;
- }
- return result;
- }
-
- ;
- _proto.cleanRaws = function cleanRaws(keepBetween) {
- delete this.raws.before;
- delete this.raws.after;
- if (!keepBetween) delete this.raws.between;
- };
- _proto.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
- };
- };
- _proto.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;
- }
-
-
-
-
|