progressively-enhance-web-components 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,17 @@
|
|
|
4
4
|
|
|
5
5
|
- Put your changes here...
|
|
6
6
|
|
|
7
|
+
## 1.0.2
|
|
8
|
+
|
|
9
|
+
- Fixed a bug that could cause templates to be significantly altered by this preprocessor due to having been fully ingested by a DOM parser and then re-serialized back into a string. As of this version, only the custom elements that are progressively enhanced will be ingested by the DOM parser and re-serialized back into a string.
|
|
10
|
+
- Fixed a bug that caused errors to print to the console if inline CSS existed in a scanned template.
|
|
11
|
+
- Updated various dependencies.
|
|
12
|
+
|
|
13
|
+
## 1.0.1
|
|
14
|
+
|
|
15
|
+
- Fixed a bug that prevented camelCase attribute names from replacing `${templateLiteral}` values for values in <template> markup.
|
|
16
|
+
- Updated various dependencies.
|
|
17
|
+
|
|
7
18
|
## 1.0.0
|
|
8
19
|
|
|
9
20
|
- Initial commit.
|
package/README.md
CHANGED
|
@@ -33,7 +33,7 @@ Suppose the intended use of the `<word-count>` component looks like this:
|
|
|
33
33
|
|
|
34
34
|
And suppose also that you have an Express application with templates loaded into `mvc/views`.
|
|
35
35
|
|
|
36
|
-
So leverage this module's progressive enhancement technique, you will need to define this component using a `<template>` element in any one of your templates as follows:
|
|
36
|
+
So to leverage this module's progressive enhancement technique, you will need to define this component using a `<template>` element in any one of your templates as follows:
|
|
37
37
|
|
|
38
38
|
```html
|
|
39
39
|
<template id="word-count">
|
|
@@ -58,7 +58,7 @@ So leverage this module's progressive enhancement technique, you will need to de
|
|
|
58
58
|
<div>
|
|
59
59
|
<textarea rows="10" cols="50" name="${id}" id="${id}">${text}</textarea>
|
|
60
60
|
<slot name="description"></slot>
|
|
61
|
-
<span></span>
|
|
61
|
+
<span class="word-count"></span>
|
|
62
62
|
</div>
|
|
63
63
|
</template>
|
|
64
64
|
```
|
|
@@ -115,7 +115,7 @@ Will be replaced with this:
|
|
|
115
115
|
<word-count text="Once upon a time... " id="story">
|
|
116
116
|
<div>
|
|
117
117
|
<textarea rows="10" cols="50" name="story" id="story">Once upon a time... </textarea>
|
|
118
|
-
<span></span>
|
|
118
|
+
<span class="word-count"></span>
|
|
119
119
|
</div>
|
|
120
120
|
<p slot="description">Type your story in the box above!</p>
|
|
121
121
|
</word-count>
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"url": "https://github.com/rooseveltframework/progressively-enhance-web-components/graphs/contributors"
|
|
9
9
|
}
|
|
10
10
|
],
|
|
11
|
-
"version": "1.0.
|
|
11
|
+
"version": "1.0.2",
|
|
12
12
|
"homepage": "https://github.com/rooseveltframework/progressively-enhance-web-components",
|
|
13
13
|
"license": "CC-BY-4.0",
|
|
14
14
|
"main": "progressively-enhance-web-components.js",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"node": ">=18.0.0"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"js-beautify": "1.15.
|
|
19
|
+
"js-beautify": "1.15.4",
|
|
20
20
|
"jsdom": "26.0.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
@@ -27,6 +27,14 @@ function isBinaryFile (filePath, bytesToCheck = 512) {
|
|
|
27
27
|
return false
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
// hack to get jsdom to shut up about invalid css
|
|
31
|
+
const originalConsoleError = console.error
|
|
32
|
+
console.error = (...args) => {
|
|
33
|
+
if (args?.[0]?.type !== 'css parsing') {
|
|
34
|
+
originalConsoleError(...args)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
30
38
|
module.exports = (params) => {
|
|
31
39
|
const beautifyOptions = params?.beautifyOptions || {
|
|
32
40
|
indent_size: 2
|
|
@@ -47,50 +55,52 @@ module.exports = (params) => {
|
|
|
47
55
|
}
|
|
48
56
|
}
|
|
49
57
|
|
|
50
|
-
//
|
|
58
|
+
// find all invocations of each of the custom elements that are in allTemplateElements
|
|
59
|
+
const allFiles = {}
|
|
51
60
|
const editedFiles = {}
|
|
52
|
-
for (const
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
const customElements = Array.from(document.querySelectorAll('*')).filter(element => element.tagName.includes('-')) // if there is a hyphen in the element name, it is a custom element
|
|
69
|
-
for (const element of customElements) {
|
|
70
|
-
if (!allTemplateElements[element.tagName.toLowerCase()]) continue // no <template> element found for this custom element, so do not convert it into a progressive enhancement-friendly version of the element
|
|
61
|
+
for (const customElement in allTemplateElements) { // loop through list of known custom elements
|
|
62
|
+
for (const templateFile of templateFiles) { // loop through all template files to search for invocations of this custom element
|
|
63
|
+
const templateCode = fs.readFileSync(templateFile, 'utf8')
|
|
64
|
+
if (!allFiles[templateFile]) allFiles[templateFile] = templateCode
|
|
65
|
+
if (!isBinaryFile(templateFile)) {
|
|
66
|
+
// replace all invocations of this custom element with the progressive enhancement-friendly version
|
|
67
|
+
const newTemplateCode = templateCode.replace(new RegExp(`<${customElement}\\b[^>]*>(.*?)<\\/${customElement}>`, 'gs'), (match, content) => {
|
|
68
|
+
// make a dom just for modifying this element
|
|
69
|
+
const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>')
|
|
70
|
+
const document = dom.window.document
|
|
71
|
+
const fragment = JSDOM.fragment(match)
|
|
72
|
+
document.body.appendChild(fragment)
|
|
73
|
+
|
|
74
|
+
// the element we're modifying
|
|
75
|
+
const element = document.body.firstChild
|
|
71
76
|
|
|
72
|
-
|
|
73
|
-
|
|
77
|
+
// clone the <template> markup so we can construct a more verbose, progressive enhancement-compatible version of the web component
|
|
78
|
+
const template = allTemplateElements[customElement].content.cloneNode(true)
|
|
74
79
|
|
|
75
|
-
|
|
76
|
-
|
|
80
|
+
// remove elements that are not needed in the progressive enhancement context from the <template> markup
|
|
81
|
+
template.querySelectorAll('style, script, slot').forEach(el => el.remove())
|
|
77
82
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
83
|
+
// replace template literals with attribute values in the <template> markup
|
|
84
|
+
const container = document.createElement('div')
|
|
85
|
+
container.appendChild(template)
|
|
86
|
+
for (const attrib of element.attributes) container.innerHTML = container.innerHTML.replace(new RegExp(`\\$\\{${attrib.name}\\}`, 'gi'), attrib.value)
|
|
87
|
+
const componentBeautifyOptions = { ...beautifyOptions }
|
|
88
|
+
componentBeautifyOptions.preserve_newlines = false // remove newlines from components in case any style, script, or slot elements have been removed
|
|
89
|
+
container.innerHTML = beautify(container.innerHTML, componentBeautifyOptions)
|
|
85
90
|
|
|
86
|
-
|
|
87
|
-
|
|
91
|
+
// move any component child elements with slot attributs to the root of the component's light dom because such elements always need to be at the root to be accepted by the web component JS
|
|
92
|
+
element.querySelectorAll('[slot]').forEach(slot => container.appendChild(slot.cloneNode(true)))
|
|
88
93
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
+
// replace the original custom element with the new progressive enhancement-friendly version of the element
|
|
95
|
+
element.innerHTML = params?.disableBeautify ? container.innerHTML : beautify(container.innerHTML, beautifyOptions)
|
|
96
|
+
let newCode = document.body.innerHTML
|
|
97
|
+
newCode = params?.disableBeautify ? newCode : beautify(newCode, beautifyOptions)
|
|
98
|
+
return newCode
|
|
99
|
+
})
|
|
100
|
+
if (templateCode !== newTemplateCode) {
|
|
101
|
+
allFiles[templateFile] = newTemplateCode
|
|
102
|
+
editedFiles[templateFile] = newTemplateCode
|
|
103
|
+
}
|
|
94
104
|
}
|
|
95
105
|
}
|
|
96
106
|
}
|
|
@@ -39,9 +39,9 @@
|
|
|
39
39
|
}
|
|
40
40
|
</style>
|
|
41
41
|
<div>
|
|
42
|
-
<textarea rows="10" cols="50" name="${
|
|
42
|
+
<textarea rows="10" cols="50" name="${elId}" id="${elId}">${text}</textarea>
|
|
43
43
|
<slot name="description"></slot>
|
|
44
|
-
<span></span>
|
|
44
|
+
<span class="word-count"></span>
|
|
45
45
|
</div>
|
|
46
46
|
</template>
|
|
47
47
|
</noparse>
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
<dd><input type="text" name="your_name" id="your_name" value=""></dd>
|
|
8
8
|
<dt><label for="story">Tell me a story:</label></dt>
|
|
9
9
|
<dd>
|
|
10
|
-
<word-count text="Once upon a time... "
|
|
10
|
+
<word-count text="Once upon a time... " elId="story">
|
|
11
11
|
<p slot="description">Type your story in the box above!</p>
|
|
12
12
|
</word-count>
|
|
13
13
|
</dd>
|
package/test/test.js
CHANGED
|
@@ -28,12 +28,12 @@ describe('progressively-enhance-web-components tests', function () {
|
|
|
28
28
|
it('should progressively enhance the <word-count> invocation in the sample app', async function () {
|
|
29
29
|
const filePath = path.join(sampleAppDir, 'mvc', '.preprocessed_views', 'pageWithForm.html')
|
|
30
30
|
const fileContent = fs.readFileSync(filePath, 'utf8')
|
|
31
|
-
assert(fileContent.includes(`<word-count text="Once upon a time... "
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
assert(fileContent.includes(`<word-count text="Once upon a time... " elid="story">
|
|
32
|
+
<div>
|
|
33
|
+
<textarea rows="10" cols="50" name="story" id="story">Once upon a time... </textarea>
|
|
34
|
+
<span class="word-count"></span>
|
|
35
|
+
</div>
|
|
36
|
+
<p slot="description">Type your story in the box above!</p>
|
|
37
|
+
</word-count>`), 'Expected markup not found')
|
|
38
38
|
})
|
|
39
39
|
})
|