aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/htmlscriptelement.rs
diff options
context:
space:
mode:
authorFernando Jiménez Moreno <ferjmoreno@gmail.com>2017-04-10 09:36:30 +0200
committerFernando Jiménez Moreno <ferjmoreno@gmail.com>2017-04-12 09:17:52 +0200
commit3ad473755c00e9a8e66cc47b3fe3dd303d8c2709 (patch)
tree2cd241946c1636bd7eda6d3d8a50ac09f2787efc /components/script/dom/htmlscriptelement.rs
parentc8cd70f333d41864ff7bf7dce84301509c71d1d6 (diff)
downloadservo-3ad473755c00e9a8e66cc47b3fe3dd303d8c2709.tar.gz
servo-3ad473755c00e9a8e66cc47b3fe3dd303d8c2709.zip
Unminify JS and dump it to a file before executing it
Diffstat (limited to 'components/script/dom/htmlscriptelement.rs')
-rw-r--r--components/script/dom/htmlscriptelement.rs50
1 files changed, 49 insertions, 1 deletions
diff --git a/components/script/dom/htmlscriptelement.rs b/components/script/dom/htmlscriptelement.rs
index f13c0224b1a..5b8e11f237f 100644
--- a/components/script/dom/htmlscriptelement.rs
+++ b/components/script/dom/htmlscriptelement.rs
@@ -34,11 +34,17 @@ use net_traits::{FetchMetadata, FetchResponseListener, Metadata, NetworkError};
use net_traits::request::{CorsSettings, CredentialsMode, Destination, RequestInit, RequestMode, Type as RequestType};
use network_listener::{NetworkListener, PreInvoke};
use servo_atoms::Atom;
+use servo_config::opts;
use servo_url::ServoUrl;
use std::ascii::AsciiExt;
use std::cell::Cell;
+use std::fs::File;
+use std::io::{Read, Write};
+use std::path::PathBuf;
+use std::process::{Command, Stdio};
use std::sync::{Arc, Mutex};
use style::str::{HTML_SPACE_CHARACTERS, StaticStringVec};
+use uuid::Uuid;
#[dom_struct]
pub struct HTMLScriptElement {
@@ -450,6 +456,46 @@ impl HTMLScriptElement {
}
}
+ fn unminify_js(&self, script: &mut ClassicScript) {
+ if !opts::get().unminify_js {
+ return;
+ }
+
+ let process = Command::new("js-beautify")
+ .stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .spawn()
+ .expect("Failed to execute js-beautify");
+
+ let mut script_content = String::from(script.text.clone());
+ let _ = process.stdin.unwrap().write_all(script_content.as_bytes());
+ script_content.clear();
+ let _ = process.stdout.unwrap().read_to_string(&mut script_content);
+
+ script.text = DOMString::from(script_content);
+
+ let unminified_js_dir = PathBuf::from(window_from_node(self).unminified_js_dir().unwrap());
+ let path = if script.external {
+ // External script.
+ debug!("unminifying script {:?}", script.url);
+ let url = script.url.clone().into_string();
+ let path_parts = url.split("/").collect::<Vec<&str>>();
+ match path_parts.last() {
+ Some(script_name) => unminified_js_dir.join(script_name),
+ None => unminified_js_dir.join(Uuid::new_v4().to_string()),
+ }
+ } else {
+ // Inline script.
+ debug!("unminifying inline script for {:?}", script.url);
+ unminified_js_dir.join(Uuid::new_v4().to_string())
+ };
+
+ debug!("unminified script will be stored in {:?}", path);
+ if let Ok(mut file) = File::create(&path) {
+ file.write_all(script.text.as_bytes()).unwrap();
+ }
+ }
+
/// https://html.spec.whatwg.org/multipage/#execute-the-script-block
pub fn execute(&self, result: Result<ClassicScript, NetworkError>) {
// Step 1.
@@ -458,7 +504,7 @@ impl HTMLScriptElement {
return;
}
- let script = match result {
+ let mut script = match result {
// Step 2.
Err(e) => {
warn!("error loading script {:?}", e);
@@ -469,6 +515,8 @@ impl HTMLScriptElement {
Ok(script) => script,
};
+ self.unminify_js(&mut script);
+
// Step 3.
let neutralized_doc = if script.external {
debug!("loading external script, url = {}", script.url);