Support persisting unminified external stylesheets (#33919)

* Support local tweaking of external stylesheets

Signed-off-by: Taym <haddadi.taym@gmail.com>

* Remove duplicated code between unminify_css and unminify_js

Signed-off-by: Taym <haddadi.taym@gmail.com>

* Add License

Signed-off-by: Taym <haddadi.taym@gmail.com>

* Use js-beautify instead of npx

Signed-off-by: Taym <haddadi.taym@gmail.com>

* Fix clippy warning

Signed-off-by: Taym <haddadi.taym@gmail.com>

---------

Signed-off-by: Taym <haddadi.taym@gmail.com>
This commit is contained in:
Taym Haddadi 2024-10-30 12:12:20 +01:00 committed by GitHub
parent bac1101163
commit ee68dc2589
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 206 additions and 77 deletions

View file

@ -70,6 +70,9 @@ use crate::script_runtime::CanGc;
use crate::task::TaskCanceller;
use crate::task_source::dom_manipulation::DOMManipulationTaskSource;
use crate::task_source::{TaskSource, TaskSourceName};
use crate::unminify::{
create_output_file, create_temp_files, execute_js_beautify, BeautifyFileType,
};
// TODO Implement offthread compilation in mozjs
/*pub struct OffThreadCompilationContext {
@ -853,17 +856,11 @@ impl HTMLScriptElement {
}
fn unminify_js(&self, script: &mut ScriptOrigin) {
if !self.parser_document.window().unminify_js() {
if self.parser_document.window().unminified_js_dir().is_none() {
return;
}
// Write the minified code to a temporary file and pass its path as an argument
// to js-beautify to read from. Meanwhile, redirect the process' stdout into
// another temporary file and read that into a string. This avoids some hangs
// observed on macOS when using direct input/output pipes with very large
// unminified content.
let (input, output) = (tempfile::NamedTempFile::new(), tempfile::tempfile());
if let (Ok(mut input), Ok(mut output)) = (input, output) {
if let Some((mut input, mut output)) = create_temp_files() {
match &script.code {
SourceCode::Text(text) => {
input.write_all(text.as_bytes()).unwrap();
@ -874,66 +871,24 @@ impl HTMLScriptElement {
.unwrap();
},
}
match Command::new("js-beautify")
.arg(input.path())
.stdout(output.try_clone().unwrap())
.status()
{
Ok(status) if status.success() => {
let mut script_content = String::new();
output.seek(std::io::SeekFrom::Start(0)).unwrap();
output.read_to_string(&mut script_content).unwrap();
script.code = SourceCode::Text(Rc::new(DOMString::from(script_content)));
},
_ => {
warn!("Failed to execute js-beautify. Will store unmodified script");
},
if execute_js_beautify(
input.path(),
output.try_clone().unwrap(),
BeautifyFileType::Js,
) {
let mut script_content = String::new();
output.seek(std::io::SeekFrom::Start(0)).unwrap();
output.read_to_string(&mut script_content).unwrap();
script.code = SourceCode::Text(Rc::new(DOMString::from(script_content)));
}
} else {
warn!("Error creating input and output files for unminify");
}
let path = match window_from_node(self).unminified_js_dir() {
Some(unminified_js_dir) => PathBuf::from(unminified_js_dir),
None => {
warn!("Unminified script directory not found");
return;
},
};
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.
path.join(&script.url[url::Position::BeforeHost..])
} else {
// Inline script or url ends with '/'
base.join(Uuid::new_v4().to_string())
};
debug!("script will be stored in {:?}", path);
match File::create(&path) {
match create_output_file(
window_from_node(self).unminified_js_dir(),
&script.url,
Some(script.external),
) {
Ok(mut file) => match &script.code {
SourceCode::Text(text) => file.write_all(text.as_bytes()).unwrap(),
SourceCode::Compiled(compiled_source_code) => {