progressively-enhance-web-components 1.0.1 → 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 +6 -0
- package/package.json +2 -2
- package/progressively-enhance-web-components.js +48 -38
- package/test/test.js +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,12 @@
|
|
|
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
|
+
|
|
7
13
|
## 1.0.1
|
|
8
14
|
|
|
9
15
|
- Fixed a bug that prevented camelCase attribute names from replacing `${templateLiteral}` values for values in <template> markup.
|
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
|
}
|
package/test/test.js
CHANGED
|
@@ -29,11 +29,11 @@ describe('progressively-enhance-web-components tests', function () {
|
|
|
29
29
|
const filePath = path.join(sampleAppDir, 'mvc', '.preprocessed_views', 'pageWithForm.html')
|
|
30
30
|
const fileContent = fs.readFileSync(filePath, 'utf8')
|
|
31
31
|
assert(fileContent.includes(`<word-count text="Once upon a time... " elid="story">
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
})
|