gradient.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. 'use strict';
  2. function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  5. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }
  6. var OldValue = require('../old-value');
  7. var Value = require('../value');
  8. var utils = require('../utils');
  9. var parser = require('postcss-value-parser');
  10. var range = require('normalize-range');
  11. var isDirection = /top|left|right|bottom/gi;
  12. var Gradient = function (_Value) {
  13. _inherits(Gradient, _Value);
  14. function Gradient() {
  15. var _temp, _this, _ret;
  16. _classCallCheck(this, Gradient);
  17. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  18. args[_key] = arguments[_key];
  19. }
  20. return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Value.call.apply(_Value, [this].concat(args))), _this), Object.defineProperty(_this, 'directions', {
  21. enumerable: true,
  22. writable: true,
  23. value: {
  24. top: 'bottom',
  25. left: 'right',
  26. bottom: 'top',
  27. right: 'left'
  28. }
  29. }), Object.defineProperty(_this, 'oldDirections', {
  30. enumerable: true,
  31. writable: true,
  32. value: {
  33. 'top': 'left bottom, left top',
  34. 'left': 'right top, left top',
  35. 'bottom': 'left top, left bottom',
  36. 'right': 'left top, right top',
  37. 'top right': 'left bottom, right top',
  38. 'top left': 'right bottom, left top',
  39. 'right top': 'left bottom, right top',
  40. 'right bottom': 'left top, right bottom',
  41. 'bottom right': 'left top, right bottom',
  42. 'bottom left': 'right top, left bottom',
  43. 'left top': 'right bottom, left top',
  44. 'left bottom': 'right top, left bottom'
  45. }
  46. }), _temp), _possibleConstructorReturn(_this, _ret);
  47. }
  48. // Direction to replace
  49. // Direction to replace
  50. /**
  51. * Change degrees for webkit prefix
  52. */
  53. Gradient.prototype.replace = function replace(string, prefix) {
  54. var ast = parser(string);
  55. for (var _iterator = ast.nodes, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
  56. var _ref;
  57. if (_isArray) {
  58. if (_i >= _iterator.length) break;
  59. _ref = _iterator[_i++];
  60. } else {
  61. _i = _iterator.next();
  62. if (_i.done) break;
  63. _ref = _i.value;
  64. }
  65. var node = _ref;
  66. if (node.type === 'function' && node.value === this.name) {
  67. node.nodes = this.newDirection(node.nodes);
  68. node.nodes = this.normalize(node.nodes);
  69. if (prefix === '-webkit- old') {
  70. var changes = this.oldWebkit(node);
  71. if (!changes) {
  72. return false;
  73. }
  74. } else {
  75. node.nodes = this.convertDirection(node.nodes);
  76. node.value = prefix + node.value;
  77. }
  78. }
  79. }
  80. return ast.toString();
  81. };
  82. /**
  83. * Replace first token
  84. */
  85. Gradient.prototype.replaceFirst = function replaceFirst(params) {
  86. for (var _len2 = arguments.length, words = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
  87. words[_key2 - 1] = arguments[_key2];
  88. }
  89. var prefix = words.map(function (i) {
  90. if (i === ' ') {
  91. return { type: 'space', value: i };
  92. } else {
  93. return { type: 'word', value: i };
  94. }
  95. });
  96. return prefix.concat(params.slice(1));
  97. };
  98. /**
  99. * Convert angle unit to deg
  100. */
  101. Gradient.prototype.normalizeUnit = function normalizeUnit(str, full) {
  102. var num = parseFloat(str);
  103. var deg = num / full * 360;
  104. return deg + 'deg';
  105. };
  106. /**
  107. * Normalize angle
  108. */
  109. Gradient.prototype.normalize = function normalize(nodes) {
  110. if (!nodes[0]) return nodes;
  111. if (/-?\d+(.\d+)?grad/.test(nodes[0].value)) {
  112. nodes[0].value = this.normalizeUnit(nodes[0].value, 400);
  113. } else if (/-?\d+(.\d+)?rad/.test(nodes[0].value)) {
  114. nodes[0].value = this.normalizeUnit(nodes[0].value, 2 * Math.PI);
  115. } else if (/-?\d+(.\d+)?turn/.test(nodes[0].value)) {
  116. nodes[0].value = this.normalizeUnit(nodes[0].value, 1);
  117. } else if (nodes[0].value.indexOf('deg') !== -1) {
  118. var num = parseFloat(nodes[0].value);
  119. num = range.wrap(0, 360, num);
  120. nodes[0].value = num + 'deg';
  121. }
  122. if (nodes[0].value === '0deg') {
  123. nodes = this.replaceFirst(nodes, 'to', ' ', 'top');
  124. } else if (nodes[0].value === '90deg') {
  125. nodes = this.replaceFirst(nodes, 'to', ' ', 'right');
  126. } else if (nodes[0].value === '180deg') {
  127. nodes = this.replaceFirst(nodes, 'to', ' ', 'bottom');
  128. } else if (nodes[0].value === '270deg') {
  129. nodes = this.replaceFirst(nodes, 'to', ' ', 'left');
  130. }
  131. return nodes;
  132. };
  133. /**
  134. * Replace old direction to new
  135. */
  136. Gradient.prototype.newDirection = function newDirection(params) {
  137. if (params[0].value === 'to') {
  138. return params;
  139. }
  140. isDirection.lastIndex = 0; // reset search index of global regexp
  141. if (!isDirection.test(params[0].value)) {
  142. return params;
  143. }
  144. params.unshift({
  145. type: 'word',
  146. value: 'to'
  147. }, {
  148. type: 'space',
  149. value: ' '
  150. });
  151. for (var i = 2; i < params.length; i++) {
  152. if (params[i].type === 'div') {
  153. break;
  154. }
  155. if (params[i].type === 'word') {
  156. params[i].value = this.revertDirection(params[i].value);
  157. }
  158. }
  159. return params;
  160. };
  161. /**
  162. * Change new direction to old
  163. */
  164. Gradient.prototype.convertDirection = function convertDirection(params) {
  165. if (params.length > 0) {
  166. if (params[0].value === 'to') {
  167. this.fixDirection(params);
  168. } else if (params[0].value.indexOf('deg') !== -1) {
  169. this.fixAngle(params);
  170. } else if (params[2] && params[2].value === 'at') {
  171. this.fixRadial(params);
  172. }
  173. }
  174. return params;
  175. };
  176. /**
  177. * Replace `to top left` to `bottom right`
  178. */
  179. Gradient.prototype.fixDirection = function fixDirection(params) {
  180. params.splice(0, 2);
  181. for (var _iterator2 = params, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
  182. var _ref2;
  183. if (_isArray2) {
  184. if (_i2 >= _iterator2.length) break;
  185. _ref2 = _iterator2[_i2++];
  186. } else {
  187. _i2 = _iterator2.next();
  188. if (_i2.done) break;
  189. _ref2 = _i2.value;
  190. }
  191. var param = _ref2;
  192. if (param.type === 'div') {
  193. break;
  194. }
  195. if (param.type === 'word') {
  196. param.value = this.revertDirection(param.value);
  197. }
  198. }
  199. };
  200. /**
  201. * Add 90 degrees
  202. */
  203. Gradient.prototype.fixAngle = function fixAngle(params) {
  204. var first = params[0].value;
  205. first = parseFloat(first);
  206. first = Math.abs(450 - first) % 360;
  207. first = this.roundFloat(first, 3);
  208. params[0].value = first + 'deg';
  209. };
  210. /**
  211. * Fix radial direction syntax
  212. */
  213. Gradient.prototype.fixRadial = function fixRadial(params) {
  214. var first = params[0];
  215. var second = [];
  216. var i = void 0;
  217. var div = void 0;
  218. for (i = 4; i < params.length; i++) {
  219. if (params[i].type === 'div') {
  220. div = params[i];
  221. break;
  222. } else {
  223. second.push(params[i]);
  224. }
  225. }
  226. params.splice.apply(params, [0, i].concat(second, [div, first]));
  227. };
  228. Gradient.prototype.revertDirection = function revertDirection(word) {
  229. return this.directions[word.toLowerCase()] || word;
  230. };
  231. /**
  232. * Round float and save digits under dot
  233. */
  234. Gradient.prototype.roundFloat = function roundFloat(float, digits) {
  235. return parseFloat(float.toFixed(digits));
  236. };
  237. /**
  238. * Convert to old webkit syntax
  239. */
  240. Gradient.prototype.oldWebkit = function oldWebkit(node) {
  241. var nodes = node.nodes;
  242. var string = parser.stringify(node.nodes);
  243. if (this.name !== 'linear-gradient') {
  244. return false;
  245. }
  246. if (nodes[0] && nodes[0].value.indexOf('deg') !== -1) {
  247. return false;
  248. }
  249. if (string.indexOf('px') !== -1 || string.indexOf('-corner') !== -1 || string.indexOf('-side') !== -1) {
  250. return false;
  251. }
  252. var params = [[]];
  253. for (var _iterator3 = nodes, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
  254. var _ref3;
  255. if (_isArray3) {
  256. if (_i3 >= _iterator3.length) break;
  257. _ref3 = _iterator3[_i3++];
  258. } else {
  259. _i3 = _iterator3.next();
  260. if (_i3.done) break;
  261. _ref3 = _i3.value;
  262. }
  263. var i = _ref3;
  264. params[params.length - 1].push(i);
  265. if (i.type === 'div' && i.value === ',') {
  266. params.push([]);
  267. }
  268. }
  269. this.oldDirection(params);
  270. this.colorStops(params);
  271. node.nodes = [];
  272. for (var _iterator4 = params, _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) {
  273. var _ref4;
  274. if (_isArray4) {
  275. if (_i4 >= _iterator4.length) break;
  276. _ref4 = _iterator4[_i4++];
  277. } else {
  278. _i4 = _iterator4.next();
  279. if (_i4.done) break;
  280. _ref4 = _i4.value;
  281. }
  282. var param = _ref4;
  283. node.nodes = node.nodes.concat(param);
  284. }
  285. node.nodes.unshift({ type: 'word', value: 'linear' }, this.cloneDiv(node.nodes));
  286. node.value = '-webkit-gradient';
  287. return true;
  288. };
  289. /**
  290. * Change direction syntax to old webkit
  291. */
  292. Gradient.prototype.oldDirection = function oldDirection(params) {
  293. var div = this.cloneDiv(params[0]);
  294. if (params[0][0].value !== 'to') {
  295. return params.unshift([{ type: 'word', value: this.oldDirections.bottom }, div]);
  296. } else {
  297. var _words = [];
  298. for (var _iterator5 = params[0].slice(2), _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) {
  299. var _ref5;
  300. if (_isArray5) {
  301. if (_i5 >= _iterator5.length) break;
  302. _ref5 = _iterator5[_i5++];
  303. } else {
  304. _i5 = _iterator5.next();
  305. if (_i5.done) break;
  306. _ref5 = _i5.value;
  307. }
  308. var node = _ref5;
  309. if (node.type === 'word') {
  310. _words.push(node.value.toLowerCase());
  311. }
  312. }
  313. _words = _words.join(' ');
  314. var old = this.oldDirections[_words] || _words;
  315. params[0] = [{ type: 'word', value: old }, div];
  316. return params[0];
  317. }
  318. };
  319. /**
  320. * Get div token from exists parameters
  321. */
  322. Gradient.prototype.cloneDiv = function cloneDiv(params) {
  323. for (var _iterator6 = params, _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) {
  324. var _ref6;
  325. if (_isArray6) {
  326. if (_i6 >= _iterator6.length) break;
  327. _ref6 = _iterator6[_i6++];
  328. } else {
  329. _i6 = _iterator6.next();
  330. if (_i6.done) break;
  331. _ref6 = _i6.value;
  332. }
  333. var i = _ref6;
  334. if (i.type === 'div' && i.value === ',') {
  335. return i;
  336. }
  337. }
  338. return { type: 'div', value: ',', after: ' ' };
  339. };
  340. /**
  341. * Change colors syntax to old webkit
  342. */
  343. Gradient.prototype.colorStops = function colorStops(params) {
  344. var result = [];
  345. for (var i = 0; i < params.length; i++) {
  346. var pos = void 0;
  347. var param = params[i];
  348. var item = void 0;
  349. if (i === 0) {
  350. continue;
  351. }
  352. var color = parser.stringify(param[0]);
  353. if (param[1] && param[1].type === 'word') {
  354. pos = param[1].value;
  355. } else if (param[2] && param[2].type === 'word') {
  356. pos = param[2].value;
  357. }
  358. var stop = void 0;
  359. if (i === 1 && (!pos || pos === '0%')) {
  360. stop = 'from(' + color + ')';
  361. } else if (i === params.length - 1 && (!pos || pos === '100%')) {
  362. stop = 'to(' + color + ')';
  363. } else if (pos) {
  364. stop = 'color-stop(' + pos + ', ' + color + ')';
  365. } else {
  366. stop = 'color-stop(' + color + ')';
  367. }
  368. var div = param[param.length - 1];
  369. params[i] = [{ type: 'word', value: stop }];
  370. if (div.type === 'div' && div.value === ',') {
  371. item = params[i].push(div);
  372. }
  373. result.push(item);
  374. }
  375. return result;
  376. };
  377. /**
  378. * Remove old WebKit gradient too
  379. */
  380. Gradient.prototype.old = function old(prefix) {
  381. if (prefix === '-webkit-') {
  382. var type = this.name === 'linear-gradient' ? 'linear' : 'radial';
  383. var string = '-gradient';
  384. var regexp = utils.regexp('-webkit-(' + type + '-gradient|gradient\\(\\s*' + type + ')', false);
  385. return new OldValue(this.name, prefix + this.name, string, regexp);
  386. } else {
  387. return _Value.prototype.old.call(this, prefix);
  388. }
  389. };
  390. /**
  391. * Do not add non-webkit prefixes for list-style and object
  392. */
  393. Gradient.prototype.add = function add(decl, prefix) {
  394. var p = decl.prop;
  395. if (p.indexOf('mask') !== -1) {
  396. if (prefix === '-webkit-' || prefix === '-webkit- old') {
  397. return _Value.prototype.add.call(this, decl, prefix);
  398. }
  399. } else if (p === 'list-style' || p === 'list-style-image' || p === 'content') {
  400. if (prefix === '-webkit-' || prefix === '-webkit- old') {
  401. return _Value.prototype.add.call(this, decl, prefix);
  402. }
  403. } else {
  404. return _Value.prototype.add.call(this, decl, prefix);
  405. }
  406. return undefined;
  407. };
  408. return Gradient;
  409. }(Value);
  410. Object.defineProperty(Gradient, 'names', {
  411. enumerable: true,
  412. writable: true,
  413. value: ['linear-gradient', 'repeating-linear-gradient', 'radial-gradient', 'repeating-radial-gradient']
  414. });
  415. module.exports = Gradient;