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