intrinsic.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. 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; }
  4. 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) : subClass.__proto__ = superClass; }
  5. var OldValue = require('../old-value');
  6. var Value = require('../value');
  7. function _regexp(name) {
  8. return new RegExp('(^|[\\s,(])(' + name + '($|[\\s),]))', 'gi');
  9. }
  10. var Intrinsic = function (_Value) {
  11. _inherits(Intrinsic, _Value);
  12. function Intrinsic() {
  13. _classCallCheck(this, Intrinsic);
  14. return _possibleConstructorReturn(this, _Value.apply(this, arguments));
  15. }
  16. Intrinsic.prototype.regexp = function regexp() {
  17. if (!this.regexpCache) this.regexpCache = _regexp(this.name);
  18. return this.regexpCache;
  19. };
  20. Intrinsic.prototype.isStretch = function isStretch() {
  21. return this.name === 'stretch' || this.name === 'fill' || this.name === 'fill-available';
  22. };
  23. Intrinsic.prototype.replace = function replace(string, prefix) {
  24. if (prefix === '-moz-' && this.isStretch()) {
  25. return string.replace(this.regexp(), '$1-moz-available$3');
  26. } else if (prefix === '-webkit-' && this.isStretch()) {
  27. return string.replace(this.regexp(), '$1-webkit-fill-available$3');
  28. } else {
  29. return _Value.prototype.replace.call(this, string, prefix);
  30. }
  31. };
  32. Intrinsic.prototype.old = function old(prefix) {
  33. var prefixed = prefix + this.name;
  34. if (this.isStretch()) {
  35. if (prefix === '-moz-') {
  36. prefixed = '-moz-available';
  37. } else if (prefix === '-webkit-') {
  38. prefixed = '-webkit-fill-available';
  39. }
  40. }
  41. return new OldValue(this.name, prefixed, prefixed, _regexp(prefixed));
  42. };
  43. Intrinsic.prototype.add = function add(decl, prefix) {
  44. if (decl.prop.indexOf('grid') !== -1 && prefix !== '-webkit-') {
  45. return undefined;
  46. }
  47. return _Value.prototype.add.call(this, decl, prefix);
  48. };
  49. return Intrinsic;
  50. }(Value);
  51. Object.defineProperty(Intrinsic, 'names', {
  52. enumerable: true,
  53. writable: true,
  54. value: ['max-content', 'min-content', 'fit-content', 'fill', 'fill-available', 'stretch']
  55. });
  56. module.exports = Intrinsic;