Skip to content

Commit 1415125

Browse files
committed
Updated to 0.4.0
1 parent 30e08d7 commit 1415125

File tree

8 files changed

+438
-253
lines changed

8 files changed

+438
-253
lines changed

.github/workflows/deploy-image.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
commit_author: github-actions <noreply@arnaud.tech>
4040
file_pattern: |
4141
./.gitignore
42+
build/index.js
4243
build/index.min.js
4344
build/index.min.js.gz
4445
skip_dirty_check: true
@@ -61,6 +62,7 @@ jobs:
6162
uses: softprops/action-gh-release@v1
6263
with:
6364
files: |
65+
build/index.js
6466
build/index.min.js
6567
build/index.min.js.gz
6668
tag_name: ${{ steps.package-version.outputs.current-version }}

README.md

Lines changed: 21 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919
<p align="center">
2020
CSS-in-JS without CSS and JS
2121
<br />
22-
<a href="https://github.com/ulyssear/css-in-js-in-html"><strong>Explore the docs »</strong></a>
23-
<br />
24-
<br />
25-
<a href="https://github.com/ulyssear/css-in-js-in-html">View Demo</a>
26-
·
2722
<a href="https://github.com/ulyssear/css-in-js-in-html/issues">Report Bug</a>
2823
·
2924
<a href="https://github.com/ulyssear/css-in-js-in-html/issues">Request Feature</a>
@@ -45,12 +40,10 @@
4540
<li>
4641
<a href="#getting-started">Getting Started</a>
4742
<ul>
48-
<li><a href="#prerequisites">Prerequisites</a></li>
43+
<li><a href="#compatible-browsers">Compatible browsers</a></li>
4944
<li><a href="#installation">Installation</a></li>
5045
</ul>
5146
</li>
52-
<li><a href="#usage">Usage</a></li>
53-
<li><a href="#roadmap">Roadmap</a></li>
5447
<li><a href="#contributing">Contributing</a></li>
5548
<li><a href="#license">License</a></li>
5649
<li><a href="#contact">Contact</a></li>
@@ -84,74 +77,40 @@ CSS-in-JS-in-HTML is a JavaScript library which permits you to apply CSS propert
8477
<!-- GETTING STARTED -->
8578
## Getting Started
8679

87-
### Prerequisites
88-
89-
> ⚠️ The following prerequisites were tested with the version 0.1.6 of the library and not the current version. However, the compatibility should be the same.
90-
91-
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).
80+
### Compatible browsers
9281

93-
__Modern browers__
94-
- Chrome 9+
95-
- Edge 12+
96-
- Firefox 6+
97-
- Safari 5.1+
98-
- Opera 12.1+
99-
100-
__Browsers for mobile__
101-
- IOS Safari 5.1+
102-
- UC Browser for Android 11
103-
- Firefox for Android 49
104-
105-
__Older browsers__
106-
- IE10+
82+
* Chrome : 73+
83+
* Firefox : 67+
84+
* Safari : 13+
85+
* Edge (Chromium) : 79+
86+
* Opera : 60+
87+
* Samsung Internet : 11.1+
88+
* iOS Safari : 13+
10789

10890
### Installation
10991

11092
#### From CDN
11193

11294
1. Import `main.js` in your HTML file
113-
```html
114-
<script src="https://cdn.jsdelivr.net/gh/ulyssear/css-in-js-in-html@0.3.13/build/index.min.js"></script>
115-
```
95+
```html
96+
<script src="https://cdn.jsdelivr.net/gh/ulyssear/css-in-js-in-html@0.4.0/build/index.min.js"></script>
97+
```
11698

11799
2. Add one line of CSS to hide the page while building (and permits to hide elements with hidden attribute)
118-
```css
119-
html[aria-busy="true"], [hidden] { display: none!important; }
120-
```
100+
```css
101+
[hidden]:not([hidden="false"]) { display: none!important; }
102+
```
121103

122104
#### Manual installation
123105

124106
1. Clone the repo
125-
```sh
126-
git clone https://github.com/ulyssear/css-in-js-in-html.git
127-
```
107+
```sh
108+
git clone https://github.com/ulyssear/css-in-js-in-html.git
109+
```
128110
2. Import `main.js` in your HTML file
129-
```html
130-
<script src="path/to/css-in-js-in-html/build/index.min.js"></script>
131-
```
132-
133-
<p align="right">(<a href="#readme-top">back to top</a>)</p>
134-
135-
136-
<!-- USAGE EXAMPLES -->
137-
## Usage
138-
139-
<!-- TODO : ADD examples -->
140-
141-
<p align="right">(<a href="#readme-top">back to top</a>)</p>
142-
143-
144-
145-
<!-- ROADMAP -->
146-
## Roadmap
147-
148-
<!-- TODO : ADD roadmap
149-
- [ ] Feature 1
150-
- [ ] Feature 2
151-
- [ ] Feature 3
152-
- [ ] Nested Feature
153-
-->
154-
See the [open issues](https://github.com/ulyssear/css-in-js-in-html/issues) for a full list of proposed features (and known issues).
111+
```html
112+
<script src="path/to/css-in-js-in-html/build/index.min.js"></script>
113+
```
155114

156115
<p align="right">(<a href="#readme-top">back to top</a>)</p>
157116

build/index.js

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
const EVENTS = Object.keys(window).filter(e=>e.substring(0,2)=='on').map(e=>e.substring(2).toLowerCase());
2+
3+
const REGEXES_COMMENTS = [
4+
/\/\*[\s\S]*?\*\//g,
5+
/\/\/.*/g
6+
];
7+
const REGEXES = [
8+
/([a-zA-Z0-9\-,]+)-\[([a-zA-Z0-9 .%#,\-()'"\/:?!]+)\]/g,
9+
/((?:\[[a-zA-Z0-9\-@():>,+ ]+\]:)*)\{\s*?((?:[a-zA-Z\-]+-\[[a-zA-Z0-9,%. \-()/ :?!'"#]+\]\s*?)+)\}/g
10+
];
11+
12+
function getIndexChildren(element,index=0) {
13+
if ('HTML' !== element.tagName) {
14+
return getIndexChildren(element.parentElement,index+1);
15+
}
16+
return index;
17+
}
18+
19+
function joinArrays(...arrays) {
20+
const newArray = [];
21+
const highestLength = Math.max(...arrays.map(arr => arr.length));
22+
for (let i = 0; i < highestLength; i++) {
23+
const newElement = [];
24+
for (const arr of arrays) {
25+
newElement.push(arr[i] || null);
26+
}
27+
newArray.push(newElement);
28+
}
29+
return newArray;
30+
}
31+
32+
function isEvents(string) {
33+
const events = string.split(',').map(e => e.trim().toLowerCase());
34+
return events.every(e => EVENTS.includes(e));
35+
}
36+
37+
function getCurrentDOMPath(element) {
38+
let dompath = '';
39+
let currentElement = element;
40+
while (currentElement && currentElement.tagName !== 'HTML') {
41+
const tagName = currentElement.tagName.toLowerCase();
42+
const siblingsWithSameTag = Array.from(currentElement.parentNode.children).filter(child => child.tagName === currentElement.tagName);
43+
const index = siblingsWithSameTag.indexOf(currentElement) + 1;
44+
dompath = `>${tagName}${index > 1 ? `:nth-of-type(${index})` : ''}` + dompath;
45+
currentElement = currentElement.parentNode;
46+
if (currentElement.hasAttribute('data-cijih-current-tag')) {
47+
dompath = `${currentElement.getAttribute('data-cijih-current-tag')}` + dompath;
48+
return dompath;
49+
}
50+
}
51+
if (currentElement.tagName === 'HTML') {
52+
return `html`;
53+
}
54+
return xpath.substring(1);
55+
}
56+
57+
function getQueries(string) {
58+
const queries = [];
59+
let currentQuery = '';
60+
let indexBracket = 0;
61+
for (let i = 0; i < string.length; i++) {
62+
const char = string[i];
63+
if (char === '[') {
64+
indexBracket++;
65+
currentQuery += char;
66+
} else if (char === ']') {
67+
indexBracket--;
68+
currentQuery += char;
69+
} else if (char === ',' && indexBracket === 0) {
70+
queries.push(currentQuery.trim());
71+
currentQuery = '';
72+
} else {
73+
currentQuery += char;
74+
}
75+
}
76+
if (currentQuery) {
77+
queries.push(currentQuery.trim());
78+
}
79+
return queries;
80+
}
81+
82+
function getGroupsBrackets(string,result={
83+
media: '',
84+
events: '',
85+
query: ''
86+
}) {
87+
const groups = []
88+
let j = 0;
89+
for (let i = 0; i < string.length; i++) {
90+
const char = string[i];
91+
if (char === '[') {
92+
j = i + 1;
93+
while (j < string.length && string[j] !== ']') {
94+
j++;
95+
}
96+
groups.push(string.slice(i, j + 1));
97+
i = j;
98+
}
99+
}
100+
for (let i = 0; i < groups.length; i++) {
101+
const group = groups[i].substring(1, groups[i].length - 1);
102+
if (group.startsWith('@media(')) {
103+
result.media = group
104+
} else if (isEvents(group)) {
105+
result.events = group
106+
} else {
107+
result.query = group
108+
}
109+
}
110+
return result;
111+
}
112+
113+
function getStylesFromClasses(classes) {
114+
const styles = {};
115+
const matches = classes.matchAll(REGEXES[0]);
116+
for (const match of matches) {
117+
const [_, property, value] = match;
118+
styles[property] = value;
119+
}
120+
return styles;
121+
}
122+
123+
window.addEventListener('DOMContentLoaded', function () {
124+
let elements = document.querySelectorAll('[class]');
125+
const indexes = Array.from(elements).map(e => getIndexChildren(e));
126+
elements = Array.from(elements).map((e, i) => [e, indexes[i]]);
127+
elements = elements.sort((a, b) => a[1] - b[1]);
128+
for (let i = 0; i < elements.length; i++) {
129+
const element = elements[i][0];
130+
131+
for (const regex of REGEXES_COMMENTS) {
132+
element.className = element.className.replace(regex, '');
133+
}
134+
135+
const matchesSecondCase = element.className.matchAll(REGEXES[1]);
136+
137+
for (const match of matchesSecondCase) {
138+
const [_, groupsBrackets, classes] = match;
139+
const groups = getGroupsBrackets(groupsBrackets);
140+
const queries = getQueries(groups.query);
141+
for (let i = 0; i < queries.length; i++) {
142+
if (queries[i].startsWith('>')) {
143+
const currentDOMPath = getCurrentDOMPath(element);
144+
element.dataset.cijihCurrentTag = currentDOMPath;
145+
queries[i] = currentDOMPath+queries[i];
146+
}
147+
if ('current'===queries[i]) {
148+
const currentDOMPath = getCurrentDOMPath(element);
149+
element.dataset.cijihCurrentTag = currentDOMPath;
150+
queries[i] = currentDOMPath;
151+
}
152+
}
153+
if (groups.events) {
154+
const events = groups.events.split(',').map(e => e.trim().toLowerCase());
155+
for (const event of events) {
156+
element.addEventListener(event, function () {
157+
if (queries.length > 0) {
158+
for (const query of queries) {
159+
const elements = element.querySelectorAll(query);
160+
const styles = getStylesFromClasses(classes);
161+
for (const [property, value] of Object.entries(styles)) {
162+
elements.forEach(el => el.style.setProperty(property, value));
163+
}
164+
}
165+
}
166+
else {
167+
const styles = getStylesFromClasses(classes);
168+
for (const [property, value] of Object.entries(styles)) {
169+
element.style.setProperty(property, value);
170+
}
171+
}
172+
});
173+
}
174+
}
175+
for (const query of queries) {
176+
const elements = document.querySelectorAll(query);
177+
const styles = getStylesFromClasses(classes);
178+
for (const [property, value] of Object.entries(styles)) {
179+
elements.forEach(el => el.style.setProperty(property, value));
180+
}
181+
}
182+
const length = match[0].length;
183+
const spaces = ' '.repeat(length);
184+
element.className = element.className.replace(match[0], spaces);
185+
}
186+
187+
const matchesFirstCase = element.className.matchAll(REGEXES[0]);
188+
const styles = getStylesFromClasses(element.className);
189+
for (const [property, value] of Object.entries(styles)) {
190+
element.style.setProperty(property, value);
191+
}
192+
for (const match of matchesFirstCase) {
193+
const length = match[0].length;
194+
const spaces = ' '.repeat(length);
195+
element.className = element.className.replace(match[0], spaces);
196+
}
197+
element.className = element.className.trim();
198+
}
199+
this.document.documentElement.removeAttribute('hidden');
200+
const elementsWithClasses = document.querySelector('[class]');
201+
});

build/index.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/index.min.js.gz

-11 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)