backgroundPosition.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. var parsers = require('../parsers');
  3. var valid_keywords = ['top', 'center', 'bottom', 'left', 'right'];
  4. var parse = function parse(v) {
  5. if (v === '' || v === null) {
  6. return undefined;
  7. }
  8. var parts = v.split(/\s+/);
  9. if (parts.length > 2 || parts.length < 1) {
  10. return undefined;
  11. }
  12. var types = [];
  13. parts.forEach(function(part, index) {
  14. types[index] = parsers.valueType(part);
  15. });
  16. if (parts.length === 1) {
  17. if (types[0] === parsers.TYPES.LENGTH || types[0] === parsers.TYPES.PERCENT) {
  18. return v;
  19. }
  20. if (types[0] === parsers.TYPES.KEYWORD) {
  21. if (valid_keywords.indexOf(v.toLowerCase()) !== -1 || v.toLowerCase() === 'inherit') {
  22. return v;
  23. }
  24. }
  25. return undefined;
  26. }
  27. if (
  28. (types[0] === parsers.TYPES.LENGTH || types[0] === parsers.TYPES.PERCENT) &&
  29. (types[1] === parsers.TYPES.LENGTH || types[1] === parsers.TYPES.PERCENT)
  30. ) {
  31. return v;
  32. }
  33. if (types[0] !== parsers.TYPES.KEYWORD || types[1] !== parsers.TYPES.KEYWORD) {
  34. return undefined;
  35. }
  36. if (valid_keywords.indexOf(parts[0]) !== -1 && valid_keywords.indexOf(parts[1]) !== -1) {
  37. return v;
  38. }
  39. return undefined;
  40. };
  41. module.exports.isValid = function isValid(v) {
  42. return parse(v) !== undefined;
  43. };
  44. module.exports.definition = {
  45. set: function(v) {
  46. this._setProperty('background-position', parse(v));
  47. },
  48. get: function() {
  49. return this.getPropertyValue('background-position');
  50. },
  51. enumerable: true,
  52. configurable: true,
  53. };