intrinsic.js 3.2 KB

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