diff options
author | skrzyp1 <skrzyp2@gmail.com> | 2020-06-02 19:32:52 +0200 |
---|---|---|
committer | skrzyp1 <skrzyp2@gmail.com> | 2020-06-02 21:23:45 +0200 |
commit | ee6906443f9c60c60e38e6f6daaeb585fa886540 (patch) | |
tree | 9b6195a7f49f137d1c5bb6c14cb585429f565e26 /components/script/dom/htmlscriptelement.rs | |
parent | 6a1cb940bf3b21c694b026755e78dbc9d02d7539 (diff) | |
download | servo-ee6906443f9c60c60e38e6f6daaeb585fa886540.tar.gz servo-ee6906443f9c60c60e38e6f6daaeb585fa886540.zip |
reading unminified scripts from disk
Diffstat (limited to 'components/script/dom/htmlscriptelement.rs')
-rw-r--r-- | components/script/dom/htmlscriptelement.rs | 66 |
1 files changed, 56 insertions, 10 deletions
diff --git a/components/script/dom/htmlscriptelement.rs b/components/script/dom/htmlscriptelement.rs index 8d0d9f219c1..f98e7df100a 100644 --- a/components/script/dom/htmlscriptelement.rs +++ b/components/script/dom/htmlscriptelement.rs @@ -45,7 +45,7 @@ use servo_atoms::Atom; use servo_url::ImmutableOrigin; use servo_url::ServoUrl; use std::cell::Cell; -use std::fs::File; +use std::fs::{create_dir_all, read_to_string, File}; use std::io::{Read, Seek, Write}; use std::path::PathBuf; use std::process::Command; @@ -698,17 +698,34 @@ impl HTMLScriptElement { return; }, } - - let path = if script.external { + let (base, has_name) = match script.url.as_str().ends_with("/") { + true => ( + path.join(&script.url[url::Position::BeforeHost..]) + .as_path() + .to_owned(), + false, + ), + false => ( + path.join(&script.url[url::Position::BeforeHost..]) + .parent() + .unwrap() + .to_owned(), + true, + ), + }; + match create_dir_all(base.clone()) { + Ok(()) => debug!("Created base dir: {:?}", base), + Err(e) => { + debug!("Failed to create base dir: {:?}, {:?}", base, e); + return; + }, + } + let path = if script.external && has_name { // External script. - let path_parts = script.url.path_segments().unwrap(); - match path_parts.last() { - Some(script_name) => path.join(script_name), - None => path.join(Uuid::new_v4().to_string()), - } + path.join(&script.url[url::Position::BeforeHost..]) } else { - // Inline script. - path.join(Uuid::new_v4().to_string()) + // Inline script or url ends with '/' + base.join(Uuid::new_v4().to_string()) }; debug!("script will be stored in {:?}", path); @@ -719,6 +736,34 @@ impl HTMLScriptElement { } } + fn substitute_with_local_script(&self, script: &mut ScriptOrigin) { + if self + .parser_document + .window() + .local_script_source() + .is_none() || + !script.external + { + return; + } + let mut path = PathBuf::from( + self.parser_document + .window() + .local_script_source() + .clone() + .unwrap(), + ); + path = path.join(&script.url[url::Position::BeforeHost..]); + debug!("Attempting to read script stored at: {:?}", path); + match read_to_string(path.clone()) { + Ok(local_script) => { + debug!("Found script stored at: {:?}", path); + script.text = DOMString::from(local_script); + }, + Err(why) => warn!("Could not restore script from file {:?}", why), + } + } + /// <https://html.spec.whatwg.org/multipage/#execute-the-script-block> pub fn execute(&self, result: ScriptResult) { // Step 1. @@ -740,6 +785,7 @@ impl HTMLScriptElement { if script.type_ == ScriptType::Classic { self.unminify_js(&mut script); + self.substitute_with_local_script(&mut script); } // Step 3. |