index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import generate from 'css-tree/generator';
  2. import { calculate, calculateForAST } from './core/index.js';
  3. import { compare, equals, greaterThan, lessThan } from './util/compare.js';
  4. import { min, max } from './util/filter.js';
  5. import { sortAsc, sortDesc } from './util/sort.js';
  6. class NotAllowedError extends Error {
  7. constructor() {
  8. super('Manipulating a Specificity instance is not allowed. Instead, create a new Specificity()');
  9. }
  10. }
  11. class Specificity {
  12. constructor(value, selector = null) {
  13. this.value = value;
  14. this.selector = selector;
  15. }
  16. get a() {
  17. return this.value.a;
  18. }
  19. set a(val) {
  20. throw new NotAllowedError();
  21. }
  22. get b() {
  23. return this.value.b;
  24. }
  25. set b(val) {
  26. throw new NotAllowedError();
  27. }
  28. get c() {
  29. return this.value.c;
  30. }
  31. set c(val) {
  32. throw new NotAllowedError();
  33. }
  34. selectorString() {
  35. // this.selector already is a String
  36. if (typeof this.selector === 'string' || this.selector instanceof String) {
  37. return this.selector;
  38. }
  39. // this.selector is a Selector as parsed by CSSTree
  40. if (this.selector instanceof Object) {
  41. if (this.selector.type === 'Selector') {
  42. return generate(this.selector);
  43. }
  44. }
  45. // this.selector is something else …
  46. return '';
  47. }
  48. toObject() {
  49. return this.value;
  50. }
  51. toArray() {
  52. return [this.value.a, this.value.b, this.value.c];
  53. }
  54. toString() {
  55. return `(${this.value.a},${this.value.b},${this.value.c})`;
  56. }
  57. toJSON() {
  58. return {
  59. selector: this.selectorString(),
  60. asObject: this.toObject(),
  61. asArray: this.toArray(),
  62. asString: this.toString(),
  63. };
  64. }
  65. isEqualTo(otherSpecificity) {
  66. return equals(this, otherSpecificity);
  67. }
  68. isGreaterThan(otherSpecificity) {
  69. return greaterThan(this, otherSpecificity);
  70. }
  71. isLessThan(otherSpecificity) {
  72. return lessThan(this, otherSpecificity);
  73. }
  74. static calculate(selector) {
  75. return calculate(selector);
  76. }
  77. static calculateForAST(selector) {
  78. return calculateForAST(selector);
  79. }
  80. static compare(s1, s2) {
  81. return compare(s1, s2);
  82. }
  83. static equals(s1, s2) {
  84. return equals(s1, s2);
  85. }
  86. static lessThan(s1, s2) {
  87. return lessThan(s1, s2);
  88. }
  89. static greaterThan(s1, s2) {
  90. return greaterThan(s1, s2);
  91. }
  92. static min(...specificities) {
  93. return min(...specificities);
  94. }
  95. static max(...specificities) {
  96. return max(...specificities);
  97. }
  98. static sortAsc(...specificities) {
  99. return sortAsc(...specificities);
  100. }
  101. static sortDesc(...specificities) {
  102. return sortDesc(...specificities);
  103. }
  104. }
  105. export default Specificity;