Skip to content

Commit 4790509

Browse files
Handle empty src attributes (#1254)
Fixes #1253
1 parent fcabd18 commit 4790509

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

packages/optimizer/lib/HtmlDomHelper.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ function findMetaViewport(head) {
3939
*/
4040
function findRuntimeScript(head) {
4141
for (let node = head.firstChild; node !== null; node = node.nextSibling) {
42-
if (node.tagName === 'script' && node.attribs.src.match(/^https:\/\/.+\/v0(\.js|\.mjs)$/)) {
42+
if (
43+
node.tagName === 'script' &&
44+
node.attribs.src &&
45+
node.attribs.src.match(/^https:\/\/.+\/v0(\.js|\.mjs)$/)
46+
) {
4347
return node;
4448
}
4549
}

packages/optimizer/spec/lib/HtmlDomHelper.test.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
const {findMetaViewport} = require('../../lib/HtmlDomHelper');
17+
const {findMetaViewport, findRuntimeScript} = require('../../lib/HtmlDomHelper');
1818
const treeParser = require('../../lib/TreeParser');
1919
const {firstChildByTag} = require('../../lib/NodeUtils');
2020

@@ -54,3 +54,37 @@ test('findMetaViewport returns the correct tag', async () => {
5454
const result = findMetaViewport(head);
5555
expect(result).toEqual(head.children[3]);
5656
});
57+
58+
test('findRuntimeScript returns runtime v0.mjs', async () => {
59+
const root = await treeParser.parse(`<html><head>
60+
<meta charset="utf-8">
61+
<script async src="https://cdn.ampproject.org/v0.mjs"></script>
62+
</head></html>`);
63+
const html = firstChildByTag(root, 'html');
64+
const head = firstChildByTag(html, 'head');
65+
const result = findRuntimeScript(head);
66+
expect(result).toEqual(head.children[3]);
67+
});
68+
69+
test('findRuntimeScript returns runtime v0.js', async () => {
70+
const root = await treeParser.parse(`<html><head>
71+
<meta charset="utf-8">
72+
<script async src="https://cdn.ampproject.org/v0.js"></script>
73+
</head></html>`);
74+
const html = firstChildByTag(root, 'html');
75+
const head = firstChildByTag(html, 'head');
76+
const result = findRuntimeScript(head);
77+
expect(result).toEqual(head.children[3]);
78+
});
79+
80+
test('findRuntimeScript ignores empty src', async () => {
81+
const root = await treeParser.parse(`<html><head>
82+
<meta charset="utf-8">
83+
<script id="amp-access" type="application/json"></script>
84+
<script async src="https://cdn.ampproject.org/v0.js"></script>
85+
</head></html>`);
86+
const html = firstChildByTag(root, 'html');
87+
const head = firstChildByTag(html, 'head');
88+
const result = findRuntimeScript(head);
89+
expect(result).toEqual(head.children[5]);
90+
});

0 commit comments

Comments
 (0)