property.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. module.exports = exports = Property;
  3. /**
  4. * Module dependencies.
  5. */
  6. var utils = require('./utils');
  7. /**
  8. * CSS property constructor.
  9. *
  10. * @param {String} property
  11. * @param {String} value
  12. * @param {Selector} selector the property originates from
  13. * @param {Integer} priority 0 for normal properties, 2 for !important properties.
  14. * @param {Array} additional array of integers representing more detailed priorities (sorting)
  15. * @api public
  16. */
  17. function Property(prop, value, selector, priority, additionalPriority) {
  18. this.prop = prop;
  19. this.value = value;
  20. this.selector = selector;
  21. this.priority = priority || 0;
  22. this.additionalPriority = additionalPriority || [];
  23. }
  24. /**
  25. * Compares with another Property based on Selector#specificity.
  26. *
  27. * @api public
  28. */
  29. Property.prototype.compareFunc = function(property) {
  30. var a = [];
  31. a.push.apply(a, this.selector.specificity());
  32. a.push.apply(a, this.additionalPriority);
  33. a[0] += this.priority;
  34. var b = [];
  35. b.push.apply(b, property.selector.specificity());
  36. b.push.apply(b, property.additionalPriority);
  37. b[0] += property.priority;
  38. return utils.compareFunc(a, b);
  39. };
  40. Property.prototype.compare = function(property) {
  41. var winner = this.compareFunc(property);
  42. if (winner === 1) {
  43. return this;
  44. }
  45. return property;
  46. };
  47. /**
  48. * Returns CSS property
  49. *
  50. * @api public
  51. */
  52. Property.prototype.toString = function() {
  53. return this.prop + ': ' + this.value.replace(/['"]+/g, '') + ';';
  54. };