Skip to content

Commit cd953a3

Browse files
Ulysse ARNAUDUlysse ARNAUD
authored andcommitted
0.3.11
1 parent b8fb21f commit cd953a3

File tree

5 files changed

+27
-36
lines changed

5 files changed

+27
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ __Older browsers__
111111

112112
1. Import `main.js` in your HTML file
113113
```html
114-
<script src="https://cdn.jsdelivr.net/gh/ulyssear/[email protected].9/build/index.min.js"></script>
114+
<script src="https://cdn.jsdelivr.net/gh/ulyssear/[email protected].11/build/index.min.js"></script>
115115
```
116116

117117
2. Add one line of CSS to hide the page while building (and permits to hide elements with hidden attribute)

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

-91 Bytes
Binary file not shown.

index.js

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,11 @@ function init() {
77
return events;
88
}
99

10-
function getFullPathToElement(element) {
11-
const path = [];
12-
while (element.tagName !== 'HTML') {
13-
let selector = element.tagName;
14-
if (element.id) {
15-
selector += '#' + element.id;
16-
path.unshift(selector);
17-
break;
18-
} else {
19-
let sib = element, nth = 1;
20-
while (sib = sib.previousElementSibling) {
21-
if (sib.tagName === element.tagName) nth++;
22-
}
23-
if (nth != 1) selector += `:nth-of-type(${nth})`;
24-
}
25-
path.unshift(selector);
26-
element = element.parentElement;
27-
}
28-
path.unshift('HTML');
29-
return path.join('>');
10+
function generateUUID(prefix="") {
11+
return (prefix ? prefix + "-" : "") + 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
12+
const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
13+
return v.toString(16);
14+
});
3015
}
3116

3217
function main() {
@@ -83,9 +68,16 @@ function init() {
8368

8469
if (_entry.join(',') === 'events,query,classes') {
8570
const events = split[entry.events].split(',');
86-
const query = split[entry.query].replaceAll(/current\s*,?/g, getFullPathToElement(element) + ',');
71+
let query = split[entry.query].replaceAll(/current\s*,?/g, '#' + uuid + ',');
8772
const classes = Array.from(split[entry.classes].slice(1, -1).matchAll(regularExpressions[0])).map(e => e.slice(1));
73+
if (query.startsWith('>')) {
74+
query = '#' + uuid + query;
75+
}
76+
if (query.endsWith(',')) {
77+
query = query.slice(0, -1);
78+
}
8879
const elements = document.querySelectorAll(query);
80+
element.removeAttribute('id');
8981
for (const element of elements) {
9082
for (const event of events) {
9183
if (event === 'hover') {
@@ -115,17 +107,18 @@ function init() {
115107
}
116108

117109
if (_entry.join(',') === 'query,classes') {
118-
let query = split[entry.query].replaceAll(/current\s*,?/g, getFullPathToElement(element) + ',');
110+
const uuid = generateUUID('cijih');
111+
element.setAttribute('id',uuid)
112+
let query = split[entry.query].replaceAll(/current\s*,?/g, '#' + uuid + ',');
119113
const classes = Array.from(split[entry.classes].slice(1, -1).matchAll(regularExpressions[0])).map(e => e.slice(1));
120114
if (query.startsWith('>')) {
121-
console.log('before : ', query);
122-
query = getFullPathToElement(element) + query;
123-
console.log('after : ', query);
115+
query = '#' + uuid + query;
124116
}
125117
if (query.endsWith(',')) {
126118
query = query.slice(0, -1);
127119
}
128120
const elements = document.querySelectorAll(query);
121+
element.removeAttribute('id');
129122
for (const element of elements) {
130123
for (const [property, value] of classes) {
131124
element.style.setProperty(property, value);
@@ -164,22 +157,20 @@ function init() {
164157
let observer;
165158
window.addEventListener('css-in-js-in-html-ready', function () {
166159
if (!observer) {
167-
observer = new MutationObserver((mutationsList) => {
160+
observer = new MutationObserver(function (mutationsList) {
168161
for (const mutation of mutationsList) {
169-
if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
170-
init();
171-
break;
172-
}
173-
if (mutation.type === 'childList' && (mutation.addedNodes.length || mutation.removedNodes.length)) {
162+
if (
163+
(mutation.type === 'childList' && mutation.addedNodes.length > 0) ||
164+
(mutation.type === 'attributes' && mutation.attributeName === 'class')
165+
) {
174166
init();
175-
break;
176167
}
177168
}
178169
});
179170

180171
observer.observe(document.body, {
181-
attributes: true,
182172
childList: true,
173+
attributes: true,
183174
subtree: true,
184175
attributeFilter: ['class']
185176
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ulyssear/css-in-js-in-html",
3-
"version": "0.3.9",
3+
"version": "0.3.11",
44
"description": "CSS-in-JS-in-HTML is a library that allows you to use CSS-in-JS with HTML only.",
55
"main": "build/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)