legacy.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. function cssBlankPseudo(document, opts) {
  3. // configuration
  4. const className = Object(opts).className;
  5. const attr = Object(opts).attr || 'blank';
  6. const force = Object(opts).force;
  7. try {
  8. document.querySelector(':blank');
  9. if (!force) {
  10. return;
  11. }
  12. } catch (ignoredError) {}
  13. /* do nothing and continue */
  14. // observe value changes on <input>, <select>, and <textarea>
  15. const window = (document.ownerDocument || document).defaultView;
  16. observeValueOfHTMLElement(window.HTMLInputElement);
  17. observeValueOfHTMLElement(window.HTMLSelectElement);
  18. observeValueOfHTMLElement(window.HTMLTextAreaElement);
  19. observeSelectedOfHTMLElement(window.HTMLOptionElement); // form control elements selector
  20. const selector = 'INPUT,SELECT,TEXTAREA';
  21. const selectorRegExp = /^(INPUT|SELECT|TEXTAREA)$/; // conditionally update all form control elements
  22. Array.prototype.forEach.call(document.querySelectorAll(selector), node => {
  23. if (node.nodeName === 'SELECT') {
  24. node.addEventListener('change', configureCssBlankAttribute);
  25. } else {
  26. node.addEventListener('input', configureCssBlankAttribute);
  27. }
  28. configureCssBlankAttribute.call(node);
  29. }); // conditionally observe added or unobserve removed form control elements
  30. new MutationObserver(mutationsList => {
  31. mutationsList.forEach(mutation => {
  32. Array.prototype.forEach.call(mutation.addedNodes || [], node => {
  33. if (node.nodeType === 1 && selectorRegExp.test(node.nodeName)) {
  34. if (node.nodeName === 'SELECT') {
  35. node.addEventListener('change', configureCssBlankAttribute);
  36. } else {
  37. node.addEventListener('input', configureCssBlankAttribute);
  38. }
  39. configureCssBlankAttribute.call(node);
  40. }
  41. });
  42. Array.prototype.forEach.call(mutation.removedNodes || [], node => {
  43. if (node.nodeType === 1 && selectorRegExp.test(node.nodeName)) {
  44. if (node.nodeName === 'SELECT') {
  45. node.removeEventListener('change', configureCssBlankAttribute);
  46. } else {
  47. node.removeEventListener('input', configureCssBlankAttribute);
  48. }
  49. }
  50. });
  51. });
  52. }).observe(document, {
  53. childList: true,
  54. subtree: true
  55. }); // update a form control element’s css-blank attribute
  56. function configureCssBlankAttribute() {
  57. if (this.value || this.nodeName === 'SELECT' && this.options[this.selectedIndex].value) {
  58. if (attr) {
  59. this.removeAttribute(attr);
  60. }
  61. if (className) {
  62. this.classList.remove(className);
  63. }
  64. this.removeAttribute('blank');
  65. } else {
  66. if (attr) {
  67. this.setAttribute('blank', attr);
  68. }
  69. if (className) {
  70. this.classList.add(className);
  71. }
  72. }
  73. } // observe changes to the "value" property on an HTML Element
  74. function observeValueOfHTMLElement(HTMLElement) {
  75. const descriptor = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'value');
  76. const nativeSet = descriptor.set;
  77. descriptor.set = function set(value) {
  78. // eslint-disable-line no-unused-vars
  79. nativeSet.apply(this, arguments);
  80. configureCssBlankAttribute.apply(this);
  81. };
  82. Object.defineProperty(HTMLElement.prototype, 'value', descriptor);
  83. } // observe changes to the "selected" property on an HTML Element
  84. function observeSelectedOfHTMLElement(HTMLElement) {
  85. const descriptor = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'selected');
  86. const nativeSet = descriptor.set;
  87. descriptor.set = function set(value) {
  88. // eslint-disable-line no-unused-vars
  89. nativeSet.apply(this, arguments);
  90. const event = document.createEvent('Event');
  91. event.initEvent('change', true, true);
  92. this.dispatchEvent(event);
  93. };
  94. Object.defineProperty(HTMLElement.prototype, 'selected', descriptor);
  95. }
  96. }
  97. module.exports = cssBlankPseudo;
  98. //# sourceMappingURL=legacy.js.map