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