Skip to content

Commit eb2b384

Browse files
committed
embed JS as plain text instead of relying on data URLs
1 parent acbe806 commit eb2b384

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

src/core.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ const FILE_SIGNATURES: [[&[u8]; 2]; 18] = [
107107
];
108108
// All known non-"text/..." plaintext media types
109109
const PLAINTEXT_MEDIA_TYPES: &[&str] = &[
110+
"application/javascript", // .js
110111
"application/json", // .json
111112
"application/ld+json", // .jsonld
112113
"application/x-sh", // .sh

src/html.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -637,18 +637,32 @@ pub fn retrieve_and_embed_asset(
637637
// Every other type of element gets processed here
638638

639639
// Parse media type for SCRIPT elements
640-
if node_name == "script" && get_node_attr(node, "src").is_some() {
640+
if node_name == "script" {
641641
let script_media_type =
642642
get_node_attr(node, "type").unwrap_or(String::from("text/javascript"));
643643

644-
if script_media_type == "text/javascript" {
645-
// TODO: embed content here instead of using data URLs
644+
if script_media_type == "text/javascript"
645+
|| script_media_type == "application/javascript"
646+
{
647+
// Embed javascript code instead of using data URLs
648+
let script_dom: RcDom =
649+
parse_document(RcDom::default(), Default::default())
650+
.one("<script>;</script>");
651+
for script_node in
652+
find_nodes(&script_dom.document, vec!["html", "head", "script"])
653+
.iter()
654+
{
655+
let text_node = &script_node.children.borrow()[0];
646656

647-
// Create and embed data URL
648-
let mut data_url =
649-
create_data_url(&script_media_type, &charset, &data, &final_url);
650-
data_url.set_fragment(resolved_url.fragment());
651-
set_node_attr(node, attr_name, Some(data_url.to_string()));
657+
if let NodeData::Text { ref contents } = text_node.data {
658+
let mut tendril = contents.borrow_mut();
659+
tendril.clear();
660+
tendril.push_slice(&String::from_utf8_lossy(&data));
661+
}
662+
663+
node.children.borrow_mut().push(text_node.clone());
664+
set_node_attr(node, attr_name, None);
665+
}
652666
} else {
653667
// Create and embed data URL
654668
let mut data_url =

tests/cli/data_url.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ mod failing {
221221
// STDOUT should contain HTML without contents of local JS file
222222
assert_eq!(
223223
String::from_utf8_lossy(&out.stdout),
224-
"<html><head><script src=\"data:text/javascript;base64,\"></script></head><body></body></html>\n"
224+
"<html><head><script></script></head><body></body></html>\n"
225225
);
226226

227227
// Exit code should be 0

tests/cli/local_files.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ mod passing {
6262
<img alt=\"\">\n \
6363
<a href=\"file://local-file.html/\">Tricky href</a>\n \
6464
<a href=\"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/Y2Z/monolith\">Remote URL</a>\n \
65-
<script src=\"data:text/javascript;base64,ZG9jdW1lbnQuYm9keS5zdHlsZS5iYWNrZ3JvdW5kQ29sb3IgPSAiZ3JlZW4iOwpkb2N1bWVudC5ib2R5LnN0eWxlLmNvbG9yID0gInJlZCI7Cg==\"></script>\n\n\n\n\
65+
<script>document.body.style.backgroundColor = \"green\";\ndocument.body.style.color = \"red\";\n</script>\n\n\n\n\
6666
</body></html>\n\
6767
"
6868
);
@@ -289,7 +289,7 @@ mod passing {
289289
<link href=\"data:text/css;base64,Ym9keSB7CiAgICBiYWNrZ3JvdW5kLWNvbG9yOiAjMDAwOwogICAgY29sb3I6ICNGRkY7Cn0K\" rel=\"stylesheet\" type=\"text/css\" crossorigin=\"anonymous\">\n \
290290
<link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\" crossorigin=\"anonymous\">\n</head>\n\n<body>\n \
291291
<p>This page should have black background and white foreground, but only when served via http: (not via file:)</p>\n \
292-
<script src=\"data:text/javascript;base64,ZnVuY3Rpb24gbm9vcCgpIHsKICAgIGNvbnNvbGUubG9nKCJtb25vbGl0aCIpOwp9Cg==\"></script>\n \
292+
<script>function noop() {{\n console.log(\"monolith\");\n}}\n</script>\n \
293293
<script src=\"script.js\"></script>\n\n\n\n\
294294
</body></html>\n\
295295
"

0 commit comments

Comments
 (0)