Skip to content

postcss-blank : browser compat #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 45 additions & 18 deletions plugins/css-blank-pseudo/src/browser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/* global document,MutationObserver */
/* global document,window,self,MutationObserver */
import isValidReplacement from './is-valid-replacement.mjs';

// form control elements selector
const BLANK_CANDIDATES = 'input,select,textarea';
function isFormControlElement(element) {
if (element.nodeName === 'INPUT' || element.nodeName === 'SELECT' || element.nodeName === 'TEXTAREA') {
return true;
}

return false;
}

function createNewEvent(eventName) {
let event;
Expand All @@ -21,8 +27,6 @@ function generateHandler(replaceWith) {
let selector;
let remove;
let add;
const matches = typeof document.body.matches === 'function'
? 'matches' : 'msMatchesSelector';

if (replaceWith[0] === '.') {
selector = replaceWith.slice(1);
Expand All @@ -37,7 +41,7 @@ function generateHandler(replaceWith) {

return function handleInputOrChangeEvent(event) {
const element = event.target;
if (!element[matches](BLANK_CANDIDATES)) {
if (!isFormControlElement(element)) {
return;
}

Expand Down Expand Up @@ -111,39 +115,62 @@ export default function cssBlankPseudoInit(opts) {

const handler = generateHandler(options.replaceWith);

document.body.addEventListener('change', handler);
document.body.addEventListener('input', handler);

// observe value changes on <input>, <select>, and <textarea>
const window = (document.ownerDocument || document).defaultView;
if (document.body) {
document.body.addEventListener('change', handler);
document.body.addEventListener('input', handler);
} else {
window.addEventListener('load', () => {
if (document.body) {
document.body.addEventListener('change', handler);
document.body.addEventListener('input', handler);
}
});
}

observeValueOfHTMLElement(window.HTMLInputElement, handler);
observeValueOfHTMLElement(window.HTMLSelectElement, handler);
observeValueOfHTMLElement(window.HTMLTextAreaElement, handler);
observeSelectedOfHTMLElement(window.HTMLOptionElement, handler);
observeValueOfHTMLElement(self.HTMLInputElement, handler);
observeValueOfHTMLElement(self.HTMLSelectElement, handler);
observeValueOfHTMLElement(self.HTMLTextAreaElement, handler);
observeSelectedOfHTMLElement(self.HTMLOptionElement, handler);

// conditionally update all form control elements
Array.prototype.forEach.call(
document.querySelectorAll(BLANK_CANDIDATES),
document.querySelectorAll('input, select, textarea'),
node => {
handler({ target: node });
},
);

if (typeof window.MutationObserver !== 'undefined') {
if (typeof self.MutationObserver !== 'undefined') {
// conditionally observe added or unobserve removed form control elements
new MutationObserver(mutationsList => {
mutationsList.forEach(mutation => {
Array.prototype.forEach.call(
mutation.addedNodes || [],
node => {
// Matches isn't polyfilled here since IE9 doesn't have support for MO
if (node.nodeType === 1 && node.matches(BLANK_CANDIDATES)) {
if (node.nodeType === 1 && isFormControlElement(node)) {
handler({ target: node });
}
},
);
});
}).observe(document, { childList: true, subtree: true });
} else {
window.addEventListener('load', () => {
Array.prototype.forEach.call(
document.querySelectorAll('input, select, textarea'),
node => {
handler({ target: node });
},
);
});

window.addEventListener('DOMContentLoaded', () => {
Array.prototype.forEach.call(
document.querySelectorAll('input, select, textarea'),
node => {
handler({ target: node });
},
);
});
}
}
1 change: 1 addition & 0 deletions plugins/css-blank-pseudo/test/_browser.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<link rel="stylesheet" href="/test/browser.expect.css">
<script src="/dist/browser-global.js"></script>
<script>self._cssBlankPseudoInit = cssBlankPseudoInit</script>
<script>self._cssBlankPseudoInit();</script>
</head>
<body>
<input type="tel" id="tel-input" aria-label="">
Expand Down