| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | exports.__esModule = true;
|
|---|
| 4 | exports["default"] = void 0;
|
|---|
| 5 | var _util = require("../util");
|
|---|
| 6 | 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); } }
|
|---|
| 7 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|---|
| 8 | var cloneNode = function cloneNode(obj, parent) {
|
|---|
| 9 | if (typeof obj !== 'object' || obj === null) {
|
|---|
| 10 | return obj;
|
|---|
| 11 | }
|
|---|
| 12 | var cloned = new obj.constructor();
|
|---|
| 13 | for (var i in obj) {
|
|---|
| 14 | if (!obj.hasOwnProperty(i)) {
|
|---|
| 15 | continue;
|
|---|
| 16 | }
|
|---|
| 17 | var value = obj[i];
|
|---|
| 18 | var type = typeof value;
|
|---|
| 19 | if (i === 'parent' && type === 'object') {
|
|---|
| 20 | if (parent) {
|
|---|
| 21 | cloned[i] = parent;
|
|---|
| 22 | }
|
|---|
| 23 | } else if (value instanceof Array) {
|
|---|
| 24 | cloned[i] = value.map(function (j) {
|
|---|
| 25 | return cloneNode(j, cloned);
|
|---|
| 26 | });
|
|---|
| 27 | } else {
|
|---|
| 28 | cloned[i] = cloneNode(value, cloned);
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 | return cloned;
|
|---|
| 32 | };
|
|---|
| 33 | var Node = /*#__PURE__*/function () {
|
|---|
| 34 | function Node(opts) {
|
|---|
| 35 | if (opts === void 0) {
|
|---|
| 36 | opts = {};
|
|---|
| 37 | }
|
|---|
| 38 | Object.assign(this, opts);
|
|---|
| 39 | this.spaces = this.spaces || {};
|
|---|
| 40 | this.spaces.before = this.spaces.before || '';
|
|---|
| 41 | this.spaces.after = this.spaces.after || '';
|
|---|
| 42 | }
|
|---|
| 43 | var _proto = Node.prototype;
|
|---|
| 44 | _proto.remove = function remove() {
|
|---|
| 45 | if (this.parent) {
|
|---|
| 46 | this.parent.removeChild(this);
|
|---|
| 47 | }
|
|---|
| 48 | this.parent = undefined;
|
|---|
| 49 | return this;
|
|---|
| 50 | };
|
|---|
| 51 | _proto.replaceWith = function replaceWith() {
|
|---|
| 52 | if (this.parent) {
|
|---|
| 53 | for (var index in arguments) {
|
|---|
| 54 | this.parent.insertBefore(this, arguments[index]);
|
|---|
| 55 | }
|
|---|
| 56 | this.remove();
|
|---|
| 57 | }
|
|---|
| 58 | return this;
|
|---|
| 59 | };
|
|---|
| 60 | _proto.next = function next() {
|
|---|
| 61 | return this.parent.at(this.parent.index(this) + 1);
|
|---|
| 62 | };
|
|---|
| 63 | _proto.prev = function prev() {
|
|---|
| 64 | return this.parent.at(this.parent.index(this) - 1);
|
|---|
| 65 | };
|
|---|
| 66 | _proto.clone = function clone(overrides) {
|
|---|
| 67 | if (overrides === void 0) {
|
|---|
| 68 | overrides = {};
|
|---|
| 69 | }
|
|---|
| 70 | var cloned = cloneNode(this);
|
|---|
| 71 | for (var name in overrides) {
|
|---|
| 72 | cloned[name] = overrides[name];
|
|---|
| 73 | }
|
|---|
| 74 | return cloned;
|
|---|
| 75 | }
|
|---|
| 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 | _proto.appendToPropertyAndEscape = function appendToPropertyAndEscape(name, value, valueEscaped) {
|
|---|
| 87 | if (!this.raws) {
|
|---|
| 88 | this.raws = {};
|
|---|
| 89 | }
|
|---|
| 90 | var originalValue = this[name];
|
|---|
| 91 | var originalEscaped = this.raws[name];
|
|---|
| 92 | this[name] = originalValue + value; // this may trigger a setter that updates raws, so it has to be set first.
|
|---|
| 93 | if (originalEscaped || valueEscaped !== value) {
|
|---|
| 94 | this.raws[name] = (originalEscaped || originalValue) + valueEscaped;
|
|---|
| 95 | } else {
|
|---|
| 96 | delete this.raws[name]; // delete any escaped value that was created by the setter.
|
|---|
| 97 | }
|
|---|
| 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 | _proto.setPropertyAndEscape = function setPropertyAndEscape(name, value, valueEscaped) {
|
|---|
| 109 | if (!this.raws) {
|
|---|
| 110 | this.raws = {};
|
|---|
| 111 | }
|
|---|
| 112 | this[name] = value; // this may trigger a setter that updates raws, so it has to be set first.
|
|---|
| 113 | this.raws[name] = valueEscaped;
|
|---|
| 114 | }
|
|---|
| 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 | _proto.setPropertyWithoutEscape = function setPropertyWithoutEscape(name, value) {
|
|---|
| 124 | this[name] = value; // this may trigger a setter that updates raws, so it has to be set first.
|
|---|
| 125 | if (this.raws) {
|
|---|
| 126 | delete this.raws[name];
|
|---|
| 127 | }
|
|---|
| 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 | _proto.isAtPosition = function isAtPosition(line, column) {
|
|---|
| 136 | if (this.source && this.source.start && this.source.end) {
|
|---|
| 137 | if (this.source.start.line > line) {
|
|---|
| 138 | return false;
|
|---|
| 139 | }
|
|---|
| 140 | if (this.source.end.line < line) {
|
|---|
| 141 | return false;
|
|---|
| 142 | }
|
|---|
| 143 | if (this.source.start.line === line && this.source.start.column > column) {
|
|---|
| 144 | return false;
|
|---|
| 145 | }
|
|---|
| 146 | if (this.source.end.line === line && this.source.end.column < column) {
|
|---|
| 147 | return false;
|
|---|
| 148 | }
|
|---|
| 149 | return true;
|
|---|
| 150 | }
|
|---|
| 151 | return undefined;
|
|---|
| 152 | };
|
|---|
| 153 | _proto.stringifyProperty = function stringifyProperty(name) {
|
|---|
| 154 | return this.raws && this.raws[name] || this[name];
|
|---|
| 155 | };
|
|---|
| 156 | _proto.valueToString = function valueToString() {
|
|---|
| 157 | return String(this.stringifyProperty("value"));
|
|---|
| 158 | };
|
|---|
| 159 | _proto.toString = function toString() {
|
|---|
| 160 | return [this.rawSpaceBefore, this.valueToString(), this.rawSpaceAfter].join('');
|
|---|
| 161 | };
|
|---|
| 162 | _createClass(Node, [{
|
|---|
| 163 | key: "rawSpaceBefore",
|
|---|
| 164 | get: function get() {
|
|---|
| 165 | var rawSpace = this.raws && this.raws.spaces && this.raws.spaces.before;
|
|---|
| 166 | if (rawSpace === undefined) {
|
|---|
| 167 | rawSpace = this.spaces && this.spaces.before;
|
|---|
| 168 | }
|
|---|
| 169 | return rawSpace || "";
|
|---|
| 170 | },
|
|---|
| 171 | set: function set(raw) {
|
|---|
| 172 | (0, _util.ensureObject)(this, "raws", "spaces");
|
|---|
| 173 | this.raws.spaces.before = raw;
|
|---|
| 174 | }
|
|---|
| 175 | }, {
|
|---|
| 176 | key: "rawSpaceAfter",
|
|---|
| 177 | get: function get() {
|
|---|
| 178 | var rawSpace = this.raws && this.raws.spaces && this.raws.spaces.after;
|
|---|
| 179 | if (rawSpace === undefined) {
|
|---|
| 180 | rawSpace = this.spaces.after;
|
|---|
| 181 | }
|
|---|
| 182 | return rawSpace || "";
|
|---|
| 183 | },
|
|---|
| 184 | set: function set(raw) {
|
|---|
| 185 | (0, _util.ensureObject)(this, "raws", "spaces");
|
|---|
| 186 | this.raws.spaces.after = raw;
|
|---|
| 187 | }
|
|---|
| 188 | }]);
|
|---|
| 189 | return Node;
|
|---|
| 190 | }();
|
|---|
| 191 | exports["default"] = Node;
|
|---|
| 192 | module.exports = exports.default; |
|---|