Skip to content

Commit 0b7c6cc

Browse files
committed
Added polyfills
1 parent efe0829 commit 0b7c6cc

File tree

4 files changed

+238
-27
lines changed

4 files changed

+238
-27
lines changed

.github/workflows/deploy-image.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ jobs:
2424
with:
2525
node-version: 16
2626
- run: npm install
27-
- run: npm run format
28-
- run: npm run lint:fix
29-
- run: npm run build
27+
- run: npm run format:all
28+
- run: npm run lint:fix:all
29+
- run: npm run build:all
3030

3131
# - run: git config advice.addIgnoredFile false
3232

@@ -51,7 +51,7 @@ jobs:
5151
commit_user_name: github-actions
5252
commit_user_email: noreply@arnaud.tech
5353
commit_author: github-actions <noreply@arnaud.tech>
54-
file_pattern: ./build/index.min.js ./build/index.min.js.map ./package-lock.json
54+
file_pattern: ./build/index.min.js ./build/index.min.js.map build/polyfill.min.js build/polyfill.min.js.map package-lock.json
5555
skip_dirty_check: true
5656
push_options: '--force'
5757
branch: ${{ steps.package-version.outputs.current-version }}
@@ -74,6 +74,8 @@ jobs:
7474
files: |
7575
build/index.min.js
7676
build/index.min.js.map
77+
build/polyfill.min.js
78+
build/polyfill.min.js.map
7779
package-lock.json
7880
tag_name: ${{ steps.package-version.outputs.current-version }}
7981
name: ${{ steps.package-version.outputs.current-version }}

README.md

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ CSS-in-JS-in-HTML is a JavaScript library which permits you to apply CSS propert
7474
### Built With
7575

7676
[![Javascript][Javascript-shield]][Javascript-url]
77+
[![NPM][NPM-shield]][NPM-url]
7778

7879

7980
<p align="right">(<a href="#readme-top">back to top</a>)</p>
@@ -83,11 +84,24 @@ CSS-in-JS-in-HTML is a JavaScript library which permits you to apply CSS propert
8384
<!-- GETTING STARTED -->
8485
## Getting Started
8586

87+
### Prerequisites
8688

89+
The followed prerequisites are the minimum requirements to use the library. The library may work with older browsers with the polyfills but it's not guaranteed (and recommended).
8790

88-
### Prerequisites
91+
__Modern browers__
92+
- Chrome 9+
93+
- Edge 12+
94+
- Firefox 6+
95+
- Safari 5.1+
96+
- Opera 12.1+
8997

98+
__Browsers for mobile__
99+
- IOS Safari 5.1+
100+
- UC Browser for Android 11
101+
- Firefox for Android 49
90102

103+
__Older browsers__
104+
- IE10+
91105

92106
### Installation
93107

@@ -99,16 +113,14 @@ CSS-in-JS-in-HTML is a JavaScript library which permits you to apply CSS propert
99113
```
100114

101115
2. Add one line of CSS to hide the page while building (and permits to hide elements with hidden attribute)
102-
103-
```css
104-
html[aria-busy="true"], [hidden] { display: none!important; }
105-
```
116+
```css
117+
html[aria-busy="true"], [hidden] { display: none!important; }
118+
```
106119

107120
3. Call `CSS_IN_JS_IN_HTML.init(document,null)` to start the library. The optimal use case is to call it in the `DOMContentLoaded` event.
108-
109-
```js
110-
document.addEventListener('DOMContentLoaded', () => CSS_IN_JS_IN_HTML.init(document, null));
111-
```
121+
```js
122+
document.addEventListener('DOMContentLoaded', () => CSS_IN_JS_IN_HTML.init(document, null));
123+
```
112124

113125
#### Manual installation
114126

@@ -118,11 +130,31 @@ document.addEventListener('DOMContentLoaded', () => CSS_IN_JS_IN_HTML.init(docum
118130
```
119131
2. Import `index.min.js` (or `index.js`) in your HTML file
120132
```html
121-
<script src="path/to/css-in-js-in-html/index.min.js"></script>
133+
<script src="path/to/css-in-js-in-html/build/index.min.js"></script>
122134
```
123135

124-
<p align="right">(<a href="#readme-top">back to top</a>)</p>
136+
#### (Optional) Add polyfills
137+
138+
Some polyfills are required to make the library work on older browsers (IE9 and above). You can add them by importing the following file in your HTML file.
139+
140+
```html
141+
<script type="text/javascript" src="path/to/css-in-js-in-html/build/polyfill.min.js"></script>
142+
```
143+
144+
The polyfills permit to use the following features :
145+
- `window.getComputedStyle`
146+
- `Array.isArray`
147+
- `Array.prototype.lastIndexOf`
148+
- `Array.prototype.reduce`
149+
- `addEventListener`
150+
- `window.matchMedia`
151+
- `document.querySelectorAll`
152+
- `document.querySelector`
153+
- `String.prototype.trim`
125154

155+
It's recommended to not use the polyfills if you don't need to support old browsers.
156+
157+
<p align="right">(<a href="#readme-top">back to top</a>)</p>
126158

127159

128160
<!-- USAGE EXAMPLES -->
@@ -187,16 +219,6 @@ Project Link: [https://github.com/ulyssear/css-in-js-in-html](https://github.com
187219

188220

189221

190-
<!-- TODO : ACKNOWLEDGMENTS ?
191-
## Acknowledgments
192-
193-
* []()
194-
* []()
195-
* []()
196-
<p align="right">(<a href="#readme-top">back to top</a>)</p>
197-
198-
-->
199-
200222

201223
<!-- MARKDOWN LINKS & IMAGES -->
202224
<!-- https://www.markdownguide.org/basic-syntax/#reference-style-links -->
@@ -214,4 +236,6 @@ Project Link: [https://github.com/ulyssear/css-in-js-in-html](https://github.com
214236
[linkedin-url]: https://linkedin.com/in/ulyssearnaud
215237
[product-screenshot]: images/screenshot.png
216238
[JavaScript-shield]: https://img.shields.io/badge/JavaScript-323330?style=for-the-badge&logo=javascript&logoColor=F7DF1E
217-
[JavaScript-url]: https://www.javascript.com/
239+
[JavaScript-url]: https://www.javascript.com/
240+
[NPM-shield]: https://img.shields.io/badge/npm-CB3837?style=for-the-badge&logo=npm&logoColor=white
241+
[NPM-url]: https://www.npmjs.com/

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66
"scripts": {
77
"test": "jest",
88
"format": "rome format --write --indent-style tab --line-width 160 --quote-style single src/index.js",
9+
"format:polyfill": "rome format --write --indent-style tab --line-width 160 --quote-style single src/polyfill.js",
910
"lint": "rome check src/index.js",
11+
"lint:polyfill": "rome check src/polyfill.js",
1012
"lint:fix": "rome check --apply-suggested src/index.js",
13+
"lint:fix:polyfill": "rome check --apply-suggested src/polyfill.js",
14+
"lint:all": "rome check src",
15+
"lint:fix:all": "rome check --apply-suggested src",
1116
"build": "esbuild src/index.js --bundle --outfile=build/index.min.js --minify --sourcemap",
17+
"build:polyfill": "esbuild src/polyfill.js --bundle --outfile=build/polyfill.min.js --minify --sourcemap",
18+
"build:all": "npm run build && npm run build:polyfill",
1219
"build:hot": "npm run build -- --watch",
13-
"start": "concurrently -n \"build,serve\" \"npm run build:hot\" \"npx serve build\""
20+
"build:hot:polyfill": "npm run build:polyfill -- --watch",
21+
"start": "concurrently -n \"build,build:polyfill,serve\" \"npm run build:hot\" \"npm run build:hot:polyfill\" \"npx serve build\""
1422
},
1523
"repository": {
1624
"type": "git",

src/polyfill.js

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
if (!window.getComputedStyle) {
2+
window.getComputedStyle = function(e, t) {
3+
return this.el = e, this.getPropertyValue = function(t) {
4+
var n = /(\-([a-z]){1})/g;
5+
return t == "float" && (t = "styleFloat"), n.test(t) && (t = t.replace(n, function() {
6+
return arguments[2].toUpperCase();
7+
})), e.currentStyle[t] ? e.currentStyle[t] : null;
8+
}, this;
9+
};
10+
}
11+
12+
if (!Array.isArray) {
13+
Array.isArray = function(arg) {
14+
return Object.prototype.toString.call(arg) === '[object Array]';
15+
};
16+
}
17+
18+
if (!Array.prototype.lastIndexOf) {
19+
Array.prototype.lastIndexOf = function(searchElement /*, fromIndex*/) {
20+
'use strict';
21+
22+
if (this === void 0 || this === null) {
23+
throw new TypeError();
24+
}
25+
26+
var n, k,
27+
t = Object(this),
28+
len = t.length >>> 0;
29+
if (len === 0) {
30+
return -1;
31+
}
32+
33+
n = len - 1;
34+
if (arguments.length > 1) {
35+
n = Number(arguments[1]);
36+
if (n != n) {
37+
n = 0;
38+
}
39+
else if (n != 0 && n != (1 / 0) && n != -(1 / 0)) {
40+
n = (n > 0 || -1) * Math.floor(Math.abs(n));
41+
}
42+
}
43+
44+
for (k = n >= 0 ? Math.min(n, len - 1) : len - Math.abs(n); k >= 0; k--) {
45+
if (k in t && t[k] === searchElement) {
46+
return k;
47+
}
48+
}
49+
return -1;
50+
};
51+
}
52+
53+
54+
if (!Array.prototype.reduce) {
55+
Array.prototype.reduce = function(callback, initial) {
56+
var accumulator = initial;
57+
for (var i = 0; i < this.length; i++) {
58+
if (accumulator !== undefined) {
59+
accumulator = callback.call(undefined, accumulator, this[i], i, this);
60+
continue;
61+
}
62+
accumulator = this[i];
63+
}
64+
return accumulator;
65+
}
66+
}
67+
68+
(function(win, doc){
69+
if(win.addEventListener)return;
70+
71+
function docHijack(p){var old = doc[p];doc[p] = function(v){return addListen(old(v))}}
72+
function addEvent(on, fn, self){
73+
return (self = this).attachEvent('on' + on, function(e){
74+
var e = e || win.event;
75+
e.preventDefault = e.preventDefault || function(){e.returnValue = false}
76+
e.stopPropagation = e.stopPropagation || function(){e.cancelBubble = true}
77+
fn.call(self, e);
78+
});
79+
}
80+
function addListen(obj, i){
81+
if(i = obj.length)while(i--)obj[i].addEventListener = addEvent;
82+
else obj.addEventListener = addEvent;
83+
return obj;
84+
}
85+
86+
addListen([doc, win]);
87+
if('Element' in win){
88+
win.Element.prototype.addEventListener = addEvent;
89+
return;
90+
}
91+
92+
doc.attachEvent('onreadystatechange', function(){addListen(doc.all)});
93+
docHijack('getElementsByTagName');
94+
docHijack('getElementById');
95+
docHijack('createElement');
96+
addListen(doc.all);
97+
})(window, document);
98+
99+
window.matchMedia || (window.matchMedia = function() {
100+
'use strict';
101+
102+
var styleMedia = (window.styleMedia || window.media);
103+
104+
if (!styleMedia) {
105+
var style = document.createElement('style'),
106+
script = document.getElementsByTagName('script')[0],
107+
info = null;
108+
109+
style.type = 'text/css';
110+
style.id = 'matchmediajs-test';
111+
112+
if (!script) {
113+
document.head.appendChild(style);
114+
} else {
115+
script.parentNode.insertBefore(style, script);
116+
}
117+
118+
info = ('getComputedStyle' in window) && window.getComputedStyle(style, null) || style.currentStyle;
119+
120+
styleMedia = {
121+
matchMedium: function(media) {
122+
var text = '@media ' + media + '{ #matchmediajs-test { width: 1px; } }';
123+
124+
if (style.styleSheet) {
125+
style.styleSheet.cssText = text;
126+
} else {
127+
style.textContent = text;
128+
}
129+
130+
return info.width === '1px';
131+
}
132+
};
133+
}
134+
135+
return function(media) {
136+
return {
137+
matches: styleMedia.matchMedium(media || 'all'),
138+
media: media || 'all'
139+
};
140+
};
141+
}());
142+
143+
if (!document.querySelectorAll) {
144+
document.querySelectorAll = function (selectors) {
145+
var style = document.createElement('style'), elements = [], element;
146+
document.documentElement.firstChild.appendChild(style);
147+
document._qsa = [];
148+
149+
style.styleSheet.cssText = selectors + '{x-qsa:expression(document._qsa && document._qsa.push(this))}';
150+
window.scrollBy(0, 0);
151+
style.parentNode.removeChild(style);
152+
153+
while (document._qsa.length) {
154+
element = document._qsa.shift();
155+
element.style.removeAttribute('x-qsa');
156+
elements.push(element);
157+
}
158+
document._qsa = null;
159+
return elements;
160+
};
161+
}
162+
163+
if (!document.querySelector) {
164+
document.querySelector = function (selectors) {
165+
var elements = document.querySelectorAll(selectors);
166+
return (elements.length) ? elements[0] : null;
167+
};
168+
}
169+
170+
if (!String.prototype.trim) {
171+
(function() {
172+
var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
173+
String.prototype.trim = function() {
174+
return this.replace(rtrim, '');
175+
};
176+
})();
177+
}

0 commit comments

Comments
 (0)