Skip to content

Commit 98369b7

Browse files
committed
Refactor to use textContent, add default listener for 'reprocess' event, simplify <style> writing logic
1 parent 64b73ec commit 98369b7

5 files changed

Lines changed: 93 additions & 103 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ You can download jsincss and add it to your codebase manually, or download it wi
1414
npm install jsincss
1515
```
1616

17-
> Another option that works for building or testing, that isn't ideal for production use, is linking to the module directly from a CDN like unpkg:
17+
Another option that works for building or testing, that isn't ideal for production use, is linking to the module directly from a CDN like unpkg:
1818

1919
```html
2020
<script type=module>
@@ -29,10 +29,10 @@ You can import the plugin into your own JavaScript modules in a couple of ways.
2929
The first way is using the native [`import` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) in JavaScript. Here you can assign any name you want to the function you are importing, and you only need to provide a path to the plugin's `index.vanilla.js` file:
3030

3131
```js
32-
import jsincss from './node_modules/jsincss/index.vanilla.js'
32+
import jsincss from './index.vanilla.js'
3333
```
3434

35-
You can also use `require` to load this plugin instead with a bundler like Webpack or Parcel:
35+
You can also use `require()` to load this plugin instead with a bundler like Webpack or Parcel:
3636

3737
```js
3838
const jsincss = require('jsincss')

index.browser.js

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,56 @@
1-
function registerEvent(target, event, id, stylesheet) {
1+
function jsincss(
2+
stylesheet = '',
3+
selector = window,
4+
events = ['load', 'resize', 'input', 'click', 'reprocess']
5+
) {
26

3-
return target.addEventListener(event, e => populateStylesheet(id, stylesheet))
7+
function registerEvent(target, event, id, stylesheet) {
48

5-
}
9+
return target.addEventListener(
10+
event,
11+
e => populateStylesheet(id, stylesheet)
12+
)
613

7-
function populateStylesheet(id, stylesheet) {
14+
}
815

9-
let tag = document.querySelector(`#jsincss-${id}`)
16+
function populateStylesheet(id, stylesheet) {
1017

11-
if (!tag) {
18+
let tag = document.querySelector(`#jsincss-${id}`)
1219

13-
tag = document.createElement('style')
14-
tag.id = `jsincss-${id}`
15-
document.head.appendChild(tag)
20+
if (!tag) {
1621

17-
}
22+
tag = document.createElement('style')
23+
tag.id = `jsincss-${id}`
24+
document.head.appendChild(tag)
1825

19-
const generatedStyles = stylesheet()
20-
const currentStyles = tag.innerHTML
26+
}
2127

22-
if (!currentStyles
23-
|| (currentStyles && generatedStyles !== currentStyles)) {
28+
const currentStyles = tag.textContent
29+
const generatedStyles = stylesheet()
2430

25-
return tag.innerHTML = generatedStyles
31+
if (!currentStyles || (generatedStyles !== currentStyles)) {
2632

27-
}
33+
return tag.textContent = generatedStyles
2834

29-
}
35+
}
3036

31-
function jsincss(stylesheet, selector, events) {
37+
}
3238

3339
let id = Date.now() + Math.floor(Math.random() * 100)
3440

35-
selector = selector || window
36-
events = events || ['load', 'resize', 'input', 'click']
37-
3841
if (selector === window) {
3942

40-
events.forEach(event => {
41-
43+
return events.forEach(event =>
4244
registerEvent(window, event, id, stylesheet)
43-
44-
})
45+
)
4546

4647
} else {
4748

48-
document.querySelectorAll(selector).forEach(tag => {
49-
50-
events.forEach(event => {
51-
49+
return document.querySelectorAll(selector).forEach(tag =>
50+
events.forEach(event =>
5251
registerEvent(tag, event, id, stylesheet)
53-
54-
})
55-
56-
})
52+
)
53+
)
5754

5855
}
5956

index.js

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,56 @@
1-
function registerEvent(target, event, id, stylesheet) {
1+
module.exports = function(
2+
stylesheet = '',
3+
selector = window,
4+
events = ['load', 'resize', 'input', 'click', 'reprocess']
5+
) {
26

3-
return target.addEventListener(event, e => populateStylesheet(id, stylesheet))
7+
function registerEvent(target, event, id, stylesheet) {
48

5-
}
9+
return target.addEventListener(
10+
event,
11+
e => populateStylesheet(id, stylesheet)
12+
)
613

7-
function populateStylesheet(id, stylesheet) {
14+
}
815

9-
let tag = document.querySelector(`#jsincss-${id}`)
16+
function populateStylesheet(id, stylesheet) {
1017

11-
if (!tag) {
18+
let tag = document.querySelector(`#jsincss-${id}`)
1219

13-
tag = document.createElement('style')
14-
tag.id = `jsincss-${id}`
15-
document.head.appendChild(tag)
20+
if (!tag) {
1621

17-
}
22+
tag = document.createElement('style')
23+
tag.id = `jsincss-${id}`
24+
document.head.appendChild(tag)
1825

19-
const generatedStyles = stylesheet()
20-
const currentStyles = tag.innerHTML
26+
}
2127

22-
if (!currentStyles
23-
|| (currentStyles && generatedStyles !== currentStyles)) {
28+
const currentStyles = tag.textContent
29+
const generatedStyles = stylesheet()
2430

25-
return tag.innerHTML = generatedStyles
31+
if (!currentStyles || (generatedStyles !== currentStyles)) {
2632

27-
}
33+
return tag.textContent = generatedStyles
2834

29-
}
35+
}
3036

31-
module.exports = (stylesheet, selector, events) => {
37+
}
3238

3339
let id = Date.now() + Math.floor(Math.random() * 100)
3440

35-
selector = selector || window
36-
events = events || ['load', 'resize', 'input', 'click']
37-
3841
if (selector === window) {
3942

40-
events.forEach(event => {
41-
43+
return events.forEach(event =>
4244
registerEvent(window, event, id, stylesheet)
43-
44-
})
45+
)
4546

4647
} else {
4748

48-
document.querySelectorAll(selector).forEach(tag => {
49-
50-
events.forEach(event => {
51-
49+
return document.querySelectorAll(selector).forEach(tag =>
50+
events.forEach(event =>
5251
registerEvent(tag, event, id, stylesheet)
53-
54-
})
55-
56-
})
52+
)
53+
)
5754

5855
}
5956

index.vanilla.js

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,56 @@
1-
function registerEvent(target, event, id, stylesheet) {
1+
export default function(
2+
stylesheet = '',
3+
selector = window,
4+
events = ['load', 'resize', 'input', 'click', 'reprocess']
5+
) {
26

3-
return target.addEventListener(event, e => populateStylesheet(id, stylesheet))
7+
function registerEvent(target, event, id, stylesheet) {
48

5-
}
9+
return target.addEventListener(
10+
event,
11+
e => populateStylesheet(id, stylesheet)
12+
)
613

7-
function populateStylesheet(id, stylesheet) {
14+
}
815

9-
let tag = document.querySelector(`#jsincss-${id}`)
16+
function populateStylesheet(id, stylesheet) {
1017

11-
if (!tag) {
18+
let tag = document.querySelector(`#jsincss-${id}`)
1219

13-
tag = document.createElement('style')
14-
tag.id = `jsincss-${id}`
15-
document.head.appendChild(tag)
20+
if (!tag) {
1621

17-
}
22+
tag = document.createElement('style')
23+
tag.id = `jsincss-${id}`
24+
document.head.appendChild(tag)
1825

19-
const generatedStyles = stylesheet()
20-
const currentStyles = tag.innerHTML
26+
}
2127

22-
if (!currentStyles
23-
|| (currentStyles && generatedStyles !== currentStyles)) {
28+
const currentStyles = tag.textContent
29+
const generatedStyles = stylesheet()
2430

25-
return tag.innerHTML = generatedStyles
31+
if (!currentStyles || (generatedStyles !== currentStyles)) {
2632

27-
}
33+
return tag.textContent = generatedStyles
2834

29-
}
35+
}
3036

31-
export default (stylesheet, selector, events) => {
37+
}
3238

3339
let id = Date.now() + Math.floor(Math.random() * 100)
3440

35-
selector = selector || window
36-
events = events || ['load', 'resize', 'input', 'click']
37-
3841
if (selector === window) {
3942

40-
events.forEach(event => {
41-
43+
return events.forEach(event =>
4244
registerEvent(window, event, id, stylesheet)
43-
44-
})
45+
)
4546

4647
} else {
4748

48-
document.querySelectorAll(selector).forEach(tag => {
49-
50-
events.forEach(event => {
51-
49+
return document.querySelectorAll(selector).forEach(tag =>
50+
events.forEach(event =>
5251
registerEvent(tag, event, id, stylesheet)
53-
54-
})
55-
56-
})
52+
)
53+
)
5754

5855
}
5956

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
"JIC",
1818
"css",
1919
"reprocessor",
20-
"reprocessor",
2120
"stylesheet",
2221
"loader"
2322
],

0 commit comments

Comments
 (0)