PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/gulp-autoprefixer/node_modules/postcss/lib/node.js

https://gitlab.com/jeaster12/fabby
JavaScript | 390 lines | 332 code | 54 blank | 4 comment | 118 complexity | 59b1c13a6c12e564a9cf074c0acdb02d MD5 | raw file
  1. 'use strict';
  2. exports.__esModule = true;
  3. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  4. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  5. var _cssSyntaxError = require('./css-syntax-error');
  6. var _cssSyntaxError2 = _interopRequireDefault(_cssSyntaxError);
  7. var defaultStyle = {
  8. colon: ': ',
  9. indent: ' ',
  10. beforeDecl: '\n',
  11. beforeRule: '\n',
  12. beforeOpen: ' ',
  13. beforeClose: '\n',
  14. beforeComment: '\n',
  15. after: '\n',
  16. emptyBody: '',
  17. commentLeft: ' ',
  18. commentRight: ' '
  19. };
  20. var cloneNode = function cloneNode(obj, parent) {
  21. var cloned = new obj.constructor();
  22. for (var i in obj) {
  23. if (!obj.hasOwnProperty(i)) continue;
  24. var value = obj[i];
  25. var type = typeof value;
  26. if (i === 'parent' && type === 'object') {
  27. if (parent) cloned[i] = parent;
  28. } else if (i === 'source') {
  29. cloned[i] = value;
  30. } else if (value instanceof Array) {
  31. cloned[i] = value.map(function (j) {
  32. return cloneNode(j, cloned);
  33. });
  34. } else if (i !== 'before' && i !== 'after' && i !== 'between' && i !== 'semicolon') {
  35. if (type === 'object') value = cloneNode(value);
  36. cloned[i] = value;
  37. }
  38. }
  39. return cloned;
  40. };
  41. var _default = (function () {
  42. var _class = function _default() {
  43. var defaults = arguments[0] === undefined ? {} : arguments[0];
  44. _classCallCheck(this, _class);
  45. for (var _name in defaults) {
  46. this[_name] = defaults[_name];
  47. }
  48. };
  49. _class.prototype.error = function error(message) {
  50. var opts = arguments[1] === undefined ? {} : arguments[1];
  51. if (this.source) {
  52. var pos = this.source.start;
  53. return this.source.input.error(message, pos.line, pos.column, opts);
  54. } else {
  55. return new _cssSyntaxError2['default'](message);
  56. }
  57. };
  58. _class.prototype.removeSelf = function removeSelf() {
  59. if (this.parent) {
  60. this.parent.remove(this);
  61. }
  62. this.parent = undefined;
  63. return this;
  64. };
  65. _class.prototype.replace = function replace(nodes) {
  66. this.parent.insertBefore(this, nodes);
  67. this.parent.remove(this);
  68. return this;
  69. };
  70. _class.prototype.toString = function toString() {
  71. var result = '';
  72. var builder = function builder(str) {
  73. return result += str;
  74. };
  75. this.stringify(builder);
  76. return result;
  77. };
  78. _class.prototype.clone = function clone() {
  79. var overrides = arguments[0] === undefined ? {} : arguments[0];
  80. var cloned = cloneNode(this);
  81. for (var _name2 in overrides) {
  82. cloned[_name2] = overrides[_name2];
  83. }
  84. return cloned;
  85. };
  86. _class.prototype.cloneBefore = function cloneBefore() {
  87. var overrides = arguments[0] === undefined ? {} : arguments[0];
  88. var cloned = this.clone(overrides);
  89. this.parent.insertBefore(this, cloned);
  90. return cloned;
  91. };
  92. _class.prototype.cloneAfter = function cloneAfter() {
  93. var overrides = arguments[0] === undefined ? {} : arguments[0];
  94. var cloned = this.clone(overrides);
  95. this.parent.insertAfter(this, cloned);
  96. return cloned;
  97. };
  98. _class.prototype.replaceWith = function replaceWith(node) {
  99. this.parent.insertBefore(this, node);
  100. this.removeSelf();
  101. return this;
  102. };
  103. _class.prototype.moveTo = function moveTo(container) {
  104. this.cleanStyles(this.root() === container.root());
  105. this.removeSelf();
  106. container.append(this);
  107. return this;
  108. };
  109. _class.prototype.moveBefore = function moveBefore(node) {
  110. this.cleanStyles(this.root() === node.root());
  111. this.removeSelf();
  112. node.parent.insertBefore(node, this);
  113. return this;
  114. };
  115. _class.prototype.moveAfter = function moveAfter(node) {
  116. this.cleanStyles(this.root() === node.root());
  117. this.removeSelf();
  118. node.parent.insertAfter(node, this);
  119. return this;
  120. };
  121. _class.prototype.next = function next() {
  122. var index = this.parent.index(this);
  123. return this.parent.nodes[index + 1];
  124. };
  125. _class.prototype.prev = function prev() {
  126. var index = this.parent.index(this);
  127. return this.parent.nodes[index - 1];
  128. };
  129. _class.prototype.toJSON = function toJSON() {
  130. var fixed = {};
  131. for (var _name3 in this) {
  132. if (!this.hasOwnProperty(_name3)) continue;
  133. if (_name3 === 'parent') continue;
  134. var value = this[_name3];
  135. if (value instanceof Array) {
  136. fixed[_name3] = value.map(function (i) {
  137. if (typeof i === 'object' && i.toJSON) {
  138. return i.toJSON();
  139. } else {
  140. return i;
  141. }
  142. });
  143. } else if (typeof value === 'object' && value.toJSON) {
  144. fixed[_name3] = value.toJSON();
  145. } else {
  146. fixed[_name3] = value;
  147. }
  148. }
  149. return fixed;
  150. };
  151. _class.prototype.style = function style(own, detect) {
  152. var value = undefined;
  153. if (!detect) detect = own;
  154. // Already had
  155. if (own) {
  156. value = this[own];
  157. if (typeof value !== 'undefined') return value;
  158. }
  159. var parent = this.parent;
  160. // Hack for first rule in CSS
  161. if (detect === 'before') {
  162. if (!parent || parent.type === 'root' && parent.first === this) {
  163. return '';
  164. }
  165. }
  166. // Floating child without parent
  167. if (!parent) return defaultStyle[detect];
  168. // Detect style by other nodes
  169. var root = this.root();
  170. if (!root.styleCache) root.styleCache = {};
  171. if (typeof root.styleCache[detect] !== 'undefined') {
  172. return root.styleCache[detect];
  173. }
  174. if (detect === 'semicolon') {
  175. root.eachInside(function (i) {
  176. if (i.nodes && i.nodes.length && i.last.type === 'decl') {
  177. value = i.semicolon;
  178. if (typeof value !== 'undefined') return false;
  179. }
  180. });
  181. } else if (detect === 'emptyBody') {
  182. root.eachInside(function (i) {
  183. if (i.nodes && i.nodes.length === 0) {
  184. value = i.after;
  185. if (typeof value !== 'undefined') return false;
  186. }
  187. });
  188. } else if (detect === 'indent') {
  189. root.eachInside(function (i) {
  190. var p = i.parent;
  191. if (p && p !== root && p.parent && p.parent === root) {
  192. if (typeof i.before !== 'undefined') {
  193. var parts = i.before.split('\n');
  194. value = parts[parts.length - 1];
  195. value = value.replace(/[^\s]/g, '');
  196. return false;
  197. }
  198. }
  199. });
  200. } else if (detect === 'beforeComment') {
  201. root.eachComment(function (i) {
  202. if (typeof i.before !== 'undefined') {
  203. value = i.before;
  204. if (value.indexOf('\n') !== -1) {
  205. value = value.replace(/[^\n]+$/, '');
  206. }
  207. return false;
  208. }
  209. });
  210. if (typeof value === 'undefined') {
  211. value = this.style(null, 'beforeDecl');
  212. }
  213. } else if (detect === 'beforeDecl') {
  214. root.eachDecl(function (i) {
  215. if (typeof i.before !== 'undefined') {
  216. value = i.before;
  217. if (value.indexOf('\n') !== -1) {
  218. value = value.replace(/[^\n]+$/, '');
  219. }
  220. return false;
  221. }
  222. });
  223. if (typeof value === 'undefined') {
  224. value = this.style(null, 'beforeRule');
  225. }
  226. } else if (detect === 'beforeRule') {
  227. root.eachInside(function (i) {
  228. if (i.nodes && (i.parent !== root || root.first !== i)) {
  229. if (typeof i.before !== 'undefined') {
  230. value = i.before;
  231. if (value.indexOf('\n') !== -1) {
  232. value = value.replace(/[^\n]+$/, '');
  233. }
  234. return false;
  235. }
  236. }
  237. });
  238. } else if (detect === 'beforeClose') {
  239. root.eachInside(function (i) {
  240. if (i.nodes && i.nodes.length > 0) {
  241. if (typeof i.after !== 'undefined') {
  242. value = i.after;
  243. if (value.indexOf('\n') !== -1) {
  244. value = value.replace(/[^\n]+$/, '');
  245. }
  246. return false;
  247. }
  248. }
  249. });
  250. } else if (detect === 'before' || detect === 'after') {
  251. if (this.type === 'decl') {
  252. value = this.style(null, 'beforeDecl');
  253. } else if (this.type === 'comment') {
  254. value = this.style(null, 'beforeComment');
  255. } else if (detect === 'before') {
  256. value = this.style(null, 'beforeRule');
  257. } else {
  258. value = this.style(null, 'beforeClose');
  259. }
  260. var node = this.parent;
  261. var depth = 0;
  262. while (node && node.type !== 'root') {
  263. depth += 1;
  264. node = node.parent;
  265. }
  266. if (value.indexOf('\n') !== -1) {
  267. var indent = this.style(null, 'indent');
  268. if (indent.length) {
  269. for (var step = 0; step < depth; step++) {
  270. value += indent;
  271. }
  272. }
  273. }
  274. return value;
  275. } else if (detect === 'colon') {
  276. root.eachDecl(function (i) {
  277. if (typeof i.between !== 'undefined') {
  278. value = i.between.replace(/[^\s:]/g, '');
  279. return false;
  280. }
  281. });
  282. } else if (detect === 'beforeOpen') {
  283. root.eachInside(function (i) {
  284. if (i.type !== 'decl') {
  285. value = i.between;
  286. if (typeof value !== 'undefined') return false;
  287. }
  288. });
  289. } else {
  290. root.eachInside(function (i) {
  291. value = i[own];
  292. if (typeof value !== 'undefined') return false;
  293. });
  294. }
  295. if (typeof value === 'undefined') value = defaultStyle[detect];
  296. root.styleCache[detect] = value;
  297. return value;
  298. };
  299. _class.prototype.root = function root() {
  300. var result = this;
  301. while (result.parent) result = result.parent;
  302. return result;
  303. };
  304. _class.prototype.cleanStyles = function cleanStyles(keepBetween) {
  305. delete this.before;
  306. delete this.after;
  307. if (!keepBetween) delete this.between;
  308. if (this.nodes) {
  309. for (var _iterator = this.nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  310. var _ref;
  311. if (_isArray) {
  312. if (_i >= _iterator.length) break;
  313. _ref = _iterator[_i++];
  314. } else {
  315. _i = _iterator.next();
  316. if (_i.done) break;
  317. _ref = _i.value;
  318. }
  319. var node = _ref;
  320. node.cleanStyles(keepBetween);
  321. }
  322. }
  323. };
  324. _class.prototype.stringifyRaw = function stringifyRaw(prop) {
  325. var value = this[prop];
  326. var raw = this['_' + prop];
  327. if (raw && raw.value === value) {
  328. return raw.raw;
  329. } else {
  330. return value;
  331. }
  332. };
  333. return _class;
  334. })();
  335. exports['default'] = _default;
  336. module.exports = exports['default'];