diff options
author | Taym Haddadi <haddadi.taym@gmail.com> | 2024-10-30 12:12:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-30 11:12:20 +0000 |
commit | ee68dc2589283d9ef37b578b83258d421ec29bcc (patch) | |
tree | 91d7eea5a80652d14ce1e26a635d07bf94b8395a /components/script/stylesheet_loader.rs | |
parent | bac1101163a436096fe0b71bed79094658bba3b2 (diff) | |
download | servo-ee68dc2589283d9ef37b578b83258d421ec29bcc.tar.gz servo-ee68dc2589283d9ef37b578b83258d421ec29bcc.zip |
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>
Diffstat (limited to 'components/script/stylesheet_loader.rs')
-rw-r--r-- | components/script/stylesheet_loader.rs | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/components/script/stylesheet_loader.rs b/components/script/stylesheet_loader.rs index fa48dffe6f8..d076d1e7f06 100644 --- a/components/script/stylesheet_loader.rs +++ b/components/script/stylesheet_loader.rs @@ -2,6 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +use std::io::{Read, Seek, Write}; use std::sync::atomic::AtomicBool; use base::id::PipelineId; @@ -42,6 +43,9 @@ use crate::dom::shadowroot::ShadowRoot; use crate::fetch::create_a_potential_cors_request; use crate::network_listener::{self, PreInvoke, ResourceTimingListener}; use crate::script_runtime::CanGc; +use crate::unminify::{ + create_output_file, create_temp_files, execute_js_beautify, BeautifyFileType, +}; pub trait StylesheetOwner { /// Returns whether this element was inserted by the parser (i.e., it should @@ -88,6 +92,41 @@ pub struct StylesheetContext { resource_timing: ResourceFetchTiming, } +impl StylesheetContext { + fn unminify_css(&self, data: Vec<u8>, file_url: ServoUrl) -> Vec<u8> { + if self.document.root().window().unminified_css_dir().is_none() { + return data; + } + + let mut style_content = data; + + if let Some((input, mut output)) = create_temp_files() { + if execute_js_beautify( + input.path(), + output.try_clone().unwrap(), + BeautifyFileType::Css, + ) { + output.seek(std::io::SeekFrom::Start(0)).unwrap(); + output.read_to_end(&mut style_content).unwrap(); + } + } + match create_output_file( + self.document.root().window().unminified_css_dir(), + &file_url, + None, + ) { + Ok(mut file) => { + file.write_all(&style_content).unwrap(); + }, + Err(why) => { + log::warn!("Could not store script {:?}", why); + }, + } + + style_content + } +} + impl PreInvoke for StylesheetContext {} impl FetchResponseListener for StylesheetContext { @@ -134,7 +173,8 @@ impl FetchResponseListener for StylesheetContext { }); let data = if is_css { - std::mem::take(&mut self.data) + let data = std::mem::take(&mut self.data); + self.unminify_css(data, metadata.final_url.clone()) } else { vec![] }; |