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

@ -297,6 +297,10 @@ pub struct Window {
/// opt is enabled.
unminified_js_dir: DomRefCell<Option<String>>,
/// Directory to store unminified css for this window if unminify-css
/// opt is enabled.
unminified_css_dir: DomRefCell<Option<String>>,
/// Directory with stored unminified scripts
local_script_source: Option<String>,
@ -330,6 +334,9 @@ pub struct Window {
/// Unminify Javascript.
unminify_js: bool,
/// Unminify Css.
unminify_css: bool,
/// Where to load userscripts from, if any. An empty string will load from
/// the resources/user-agent-js directory, and if the option isn't passed userscripts
/// won't be loaded.
@ -538,10 +545,6 @@ impl Window {
self.replace_surrogates
}
pub fn unminify_js(&self) -> bool {
self.unminify_js
}
pub fn get_player_context(&self) -> WindowGLContext {
self.player_context.clone()
}
@ -2248,13 +2251,14 @@ impl Window {
assert!(self.document.get().is_none());
assert!(document.window() == self);
self.document.set(Some(document));
if !self.unminify_js {
return;
}
// Set a path for the document host to store unminified scripts.
let mut path = env::current_dir().unwrap();
path.push("unminified-js");
*self.unminified_js_dir.borrow_mut() = Some(path.into_os_string().into_string().unwrap());
set_unminified_path(self.unminify_js, &self.unminified_js_dir, "unminified-js");
set_unminified_path(
self.unminify_css,
&self.unminified_css_dir,
"unminified-css",
);
}
/// Commence a new URL load which will either replace this window or scroll to a fragment.
@ -2521,6 +2525,10 @@ impl Window {
self.unminified_js_dir.borrow().clone()
}
pub fn unminified_css_dir(&self) -> Option<String> {
self.unminified_css_dir.borrow().clone()
}
pub fn local_script_source(&self) -> &Option<String> {
&self.local_script_source
}
@ -2585,6 +2593,7 @@ impl Window {
relayout_event: bool,
prepare_for_screenshot: bool,
unminify_js: bool,
unminify_css: bool,
local_script_source: Option<String>,
userscripts_path: Option<String>,
is_headless: bool,
@ -2661,6 +2670,7 @@ impl Window {
webxr_registry,
pending_layout_images: Default::default(),
unminified_js_dir: Default::default(),
unminified_css_dir: Default::default(),
local_script_source,
test_worklet: Default::default(),
paint_worklet: Default::default(),
@ -2671,6 +2681,7 @@ impl Window {
relayout_event,
prepare_for_screenshot,
unminify_js,
unminify_css,
userscripts_path,
replace_surrogates,
player_context,
@ -2902,3 +2913,12 @@ fn is_named_element_with_name_attribute(elem: &Element) -> bool {
fn is_named_element_with_id_attribute(elem: &Element) -> bool {
elem.is_html_element()
}
fn set_unminified_path(option: bool, dir_ref: &DomRefCell<Option<String>>, folder_name: &str) {
if option {
// Set a path for the document host to store unminified files.
let mut path = env::current_dir().unwrap();
path.push(folder_name);
*dir_ref.borrow_mut() = Some(path.into_os_string().into_string().unwrap());
}
}