1 | 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) {}
|
---|
14 | /* do nothing and continue */
|
---|
15 | // observe value changes on <input>, <select>, and <textarea>
|
---|
16 |
|
---|
17 |
|
---|
18 | const window = (document.ownerDocument || document).defaultView;
|
---|
19 | observeValueOfHTMLElement(window.HTMLInputElement);
|
---|
20 | observeValueOfHTMLElement(window.HTMLSelectElement);
|
---|
21 | observeValueOfHTMLElement(window.HTMLTextAreaElement); // form control elements selector
|
---|
22 |
|
---|
23 | const selector = 'input,select,textarea'; // conditionally update all form control elements
|
---|
24 |
|
---|
25 | Array.prototype.forEach.call(document.querySelectorAll(selector), node => {
|
---|
26 | node.addEventListener('input', configureCssBlankAttribute);
|
---|
27 | configureCssBlankAttribute.call(node);
|
---|
28 | }); // conditionally observe added or unobserve removed form control elements
|
---|
29 |
|
---|
30 | new MutationObserver(mutationsList => {
|
---|
31 | mutationsList.forEach(mutation => {
|
---|
32 | if (mutation.addedNodes) {
|
---|
33 | mutation.addedNodes.forEach(node => {
|
---|
34 | if (node.nodeType === 1 && node.matches(selector)) {
|
---|
35 | node.addEventListener('input', configureCssBlankAttribute);
|
---|
36 | configureCssBlankAttribute.call(node);
|
---|
37 | }
|
---|
38 | });
|
---|
39 | }
|
---|
40 |
|
---|
41 | if (mutation.removedNodes) {
|
---|
42 | mutation.removedNodes.forEach(node => {
|
---|
43 | if (node.nodeType === 1 && node.matches(selector)) {
|
---|
44 | node.removeEventListener('input', configureCssBlankAttribute);
|
---|
45 | }
|
---|
46 | });
|
---|
47 | }
|
---|
48 | });
|
---|
49 | }).observe(document, {
|
---|
50 | childList: true,
|
---|
51 | subtree: true
|
---|
52 | }); // update a form control element’s css-blank attribute
|
---|
53 |
|
---|
54 | function configureCssBlankAttribute() {
|
---|
55 | if (this.value) {
|
---|
56 | if (attr) {
|
---|
57 | this.removeAttribute(attr);
|
---|
58 | }
|
---|
59 |
|
---|
60 | if (className) {
|
---|
61 | this.classList.remove(className);
|
---|
62 | }
|
---|
63 |
|
---|
64 | this.removeAttribute('blank');
|
---|
65 | } else {
|
---|
66 | if (attr) {
|
---|
67 | this.setAttribute('blank', attr);
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (className) {
|
---|
71 | this.classList.add(className);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | } // observe changes to the "value" property on an HTML Element
|
---|
75 |
|
---|
76 |
|
---|
77 | function observeValueOfHTMLElement(HTMLElement) {
|
---|
78 | const descriptor = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'value');
|
---|
79 | const nativeSet = descriptor.set;
|
---|
80 |
|
---|
81 | descriptor.set = function set(value) {
|
---|
82 | // eslint-disable-line no-unused-vars
|
---|
83 | nativeSet.apply(this, arguments);
|
---|
84 | configureCssBlankAttribute.apply(this);
|
---|
85 | };
|
---|
86 |
|
---|
87 | Object.defineProperty(HTMLElement.prototype, 'value', descriptor);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | export default cssBlankPseudo;
|
---|
92 | //# sourceMappingURL=index.mjs.map
|
---|