Skip to content

Commit acbe806

Browse files
committed
find nodes by path, do more work on SVG embedding
1 parent dc78a40 commit acbe806

File tree

6 files changed

+275
-170
lines changed

6 files changed

+275
-170
lines changed

src/core.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,18 @@ const FILE_SIGNATURES: [[&[u8]; 2]; 18] = [
105105
[b"....moov", b"video/quicktime"],
106106
[b"\x1A\x45\xDF\xA3", b"video/webm"],
107107
];
108+
// All known non-"text/..." plaintext media types
108109
const PLAINTEXT_MEDIA_TYPES: &[&str] = &[
109-
"application/javascript",
110-
"application/json",
111-
"image/svg+xml",
110+
"application/json", // .json
111+
"application/ld+json", // .jsonld
112+
"application/x-sh", // .sh
113+
"application/xhtml+xml", // .xhtml
114+
"application/xml", // .xml
115+
"application/vnd.mozilla.xul+xml", // .xul
116+
"image/svg+xml", // .svg
112117
];
113118

119+
// TODO: split it into three separate functions [create_from_data(), create_from_url(), create() -- create is likely not public]
114120
pub fn create_monolithic_document(
115121
source: String,
116122
options: &Options,
@@ -137,7 +143,9 @@ pub fn create_monolithic_document(
137143
"-" => {
138144
// Read from pipe (stdin)
139145
use_stdin = true;
140-
// Set default target URL to an empty data URL; the user can set it via --base-url
146+
147+
// Set default target URL to an empty data URL
148+
// (the user can change it by using the custom base URL option)
141149
Url::parse("data:text/html,").unwrap()
142150
}
143151
target => match Url::parse(target) {
@@ -266,7 +274,7 @@ pub fn create_monolithic_document(
266274
}
267275
}
268276

269-
// Use custom base URL if specified, read and use what's in the DOM otherwise
277+
// Use custom base URL if specified; read and use what's in the DOM otherwise
270278
let custom_base_url: String = options.base_url.clone().unwrap_or("".to_string());
271279
if custom_base_url.is_empty() {
272280
// No custom base URL is specified
@@ -389,8 +397,9 @@ pub fn detect_media_type_by_file_name(filename: &str) -> String {
389397
"htm" | "html" => "text/html",
390398
"ico" => "image/x-icon",
391399
"jpeg" | "jpg" => "image/jpeg",
392-
"js" => "application/javascript",
400+
"js" => "text/javascript",
393401
"json" => "application/json",
402+
"jsonld" => "application/ld+json",
394403
"mp3" => "audio/mpeg",
395404
"mp4" | "m4v" => "video/mp4",
396405
"ogg" => "audio/ogg",
@@ -405,12 +414,12 @@ pub fn detect_media_type_by_file_name(filename: &str) -> String {
405414
"webp" => "image/webp",
406415
"woff" => "font/woff",
407416
"woff2" => "font/woff2",
417+
"xhtml" => "application/xhtml+xml",
408418
"xml" => "text/xml",
409419
&_ => "",
410420
},
411421
None => "",
412422
};
413-
414423
mime.to_string()
415424
}
416425

@@ -511,9 +520,11 @@ pub fn retrieve_asset(
511520
let (media_type, charset, data) = parse_data_url(url);
512521
Ok((data, url.clone(), media_type, charset))
513522
} else if url.scheme() == "file" {
523+
let cache_key: String = clean_url(url.clone()).as_str().to_string();
524+
514525
// Check if parent_url is also a file: URL (if not, then we don't embed the asset)
515526
if parent_url.scheme() != "file" {
516-
print_error_message(&format!("{} (security error)", &url), options);
527+
print_error_message(&format!("{} (security error)", &cache_key), options);
517528

518529
// Provoke error
519530
client.get("").send()?;
@@ -523,12 +534,12 @@ pub fn retrieve_asset(
523534
let path: &Path = path_buf.as_path();
524535
if path.exists() {
525536
if path.is_dir() {
526-
print_error_message(&format!("{} (is a directory)", &url), options);
537+
print_error_message(&format!("{} (is a directory)", &cache_key), options);
527538

528539
// Provoke error
529540
Err(client.get("").send().unwrap_err())
530541
} else {
531-
print_info_message(&format!("{}", &url), options);
542+
print_info_message(&cache_key.to_string(), options);
532543

533544
let file_blob: Vec<u8> = fs::read(path).expect("unable to read file");
534545

0 commit comments

Comments
 (0)