Skip to content

Adding global class to CSS Blank #524

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
merged 11 commits into from
Jul 7, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
2 changes: 1 addition & 1 deletion plugin-packs/postcss-preset-env/test/basic.ch38.expect.css
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ h1.test-custom-selectors,h2.test-custom-selectors,h3.test-custom-selectors,h4.te
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ h1.test-custom-selectors,h2.test-custom-selectors,h3.test-custom-selectors,h4.te
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ h1.test-custom-selectors,h2.test-custom-selectors,h3.test-custom-selectors,h4.te
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
2 changes: 1 addition & 1 deletion plugin-packs/postcss-preset-env/test/basic.expect.css
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
2 changes: 1 addition & 1 deletion plugin-packs/postcss-preset-env/test/basic.ff49.expect.css
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
2 changes: 1 addition & 1 deletion plugin-packs/postcss-preset-env/test/basic.ff66.expect.css
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
2 changes: 1 addition & 1 deletion plugin-packs/postcss-preset-env/test/basic.ie10.expect.css
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ h1.test-custom-selectors,h2.test-custom-selectors,h3.test-custom-selectors,h4.te
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ h1.test-custom-selectors,h2.test-custom-selectors,h3.test-custom-selectors,h4.te
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ h1.test-custom-selectors,h2.test-custom-selectors,h3.test-custom-selectors,h4.te
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ h1.test-custom-selectors,h2.test-custom-selectors,h3.test-custom-selectors,h4.te
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ h1.test-custom-selectors,h2.test-custom-selectors,h3.test-custom-selectors,h4.te
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ h1.test-custom-selectors,h2.test-custom-selectors,h3.test-custom-selectors,h4.te
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank] {
.test-blank-pseudo-class[blank].js-blank-pseudo, .js-blank-pseudo .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[blank] {
[blank].js-blank-pseudo, .js-blank-pseudo [blank] {
order: 1;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
input[blank] {
input[blank].js-blank-pseudo, .js-blank-pseudo input[blank] {
order: 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ h1.test-custom-selectors:not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):n
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank]:not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#) {
.test-blank-pseudo-class[blank].js-blank-pseudo:not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#), .js-blank-pseudo:not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#) .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ h1.test-custom-selectors:not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):n
background-image: conic-gradient(yellowgreen 40%, gold 0deg 75%, #f06 0deg);
}

.test-blank-pseudo-class[blank]:not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#) {
.test-blank-pseudo-class[blank].js-blank-pseudo:not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#), .js-blank-pseudo:not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#):not(#\#) .test-blank-pseudo-class[blank] {
background-color: yellow;
}

Expand Down
7 changes: 5 additions & 2 deletions plugins/css-blank-pseudo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ input:blank {

/* becomes */

input[blank] {
input[blank].js-blank-pseudo, .js-blank-pseudo input[blank] {
background-color: yellow;
}
input:blank {
Expand Down Expand Up @@ -63,7 +63,7 @@ input:blank {

/* becomes */

input[blank] {
input[blank].js-blank-pseudo, .js-blank-pseudo input[blank] {
background-color: yellow;
}
```
Expand Down Expand Up @@ -149,6 +149,9 @@ cssBlankPseudoInit({ replaceWith: '.css-blank' });
```

This option should be used if it was changed at PostCSS configuration level.
Please note that using a class, leverages `classList` under the hood which
might not be supported on some old browsers such as IE9, so you may need
to polyfill `classList` in those cases.

[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
[css-url]: https://cssdb.org/#blank-pseudo-class
Expand Down
3 changes: 3 additions & 0 deletions plugins/css-blank-pseudo/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ cssBlankPseudoInit({ replaceWith: '.css-blank' });
```

This option should be used if it was changed at PostCSS configuration level.
Please note that using a class, leverages `classList` under the hood which
might not be supported on some old browsers such as IE9, so you may need
to polyfill `classList` in those cases.

<linkList>
[Selectors Level 4]: <specUrl>
58 changes: 27 additions & 31 deletions plugins/css-blank-pseudo/src/browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* global document,window,self,MutationObserver */
import isValidReplacement from './is-valid-replacement.mjs';

const CSS_CLASS_LOADED = 'js-blank-pseudo';

// form control elements selector
function isFormControlElement(element) {
if (element.nodeName === 'INPUT' || element.nodeName === 'SELECT' || element.nodeName === 'TEXTAREA') {
Expand Down Expand Up @@ -114,17 +116,29 @@ export default function cssBlankPseudoInit(opts) {
} catch (ignoredError) { /* do nothing and continue */ }

const handler = generateHandler(options.replaceWith);
const bindEvents = () => {
if (document.body) {
document.body.addEventListener( 'change', handler );
document.body.addEventListener( 'input', handler );
}
};
const updateAllCandidates = () => {
Array.prototype.forEach.call(
document.querySelectorAll('input, select, textarea'),
node => {
handler({ target: node });
},
);
};

if (document.body) {
document.body.addEventListener('change', handler);
document.body.addEventListener('input', handler);
bindEvents();
} else {
window.addEventListener('load', () => {
if (document.body) {
document.body.addEventListener('change', handler);
document.body.addEventListener('input', handler);
}
});
window.addEventListener('load', bindEvents);
}

if (document.documentElement.className.indexOf(CSS_CLASS_LOADED) === -1) {
document.documentElement.className += ` ${CSS_CLASS_LOADED}`;
}

observeValueOfHTMLElement(self.HTMLInputElement, handler);
Expand All @@ -133,12 +147,7 @@ export default function cssBlankPseudoInit(opts) {
observeSelectedOfHTMLElement(self.HTMLOptionElement, handler);

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

if (typeof self.MutationObserver !== 'undefined') {
// conditionally observe added or unobserve removed form control elements
Expand All @@ -155,22 +164,9 @@ export default function cssBlankPseudoInit(opts) {
});
}).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 });
},
);
});
const handleOnLoad = () => updateAllCandidates();

window.addEventListener('load', handleOnLoad);
window.addEventListener('DOMContentLoaded', handleOnLoad);
}
}
Loading