Skip to content

Commit

Permalink
Merge pull request #572 from Sharktheone/css/external-file-stylesheets
Browse files Browse the repository at this point in the history
also load external stylesheets with file://
  • Loading branch information
Sharktheone authored Aug 28, 2024
2 parents a74b431 + 4d09bfe commit 27c8ef7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 40 deletions.
95 changes: 59 additions & 36 deletions crates/gosub_html5/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4199,47 +4199,67 @@ impl<'chars> Html5Parser<'chars> {

#[cfg(not(target_arch = "wasm32"))]
fn load_external_stylesheet(&self, origin: CssOrigin, url: Url) -> Option<CssStylesheet> {
if url.scheme() != "http" && url.scheme() != "https" {
// Only load http and https
return None;
}

// Fetch the html from the url
let response = ureq::get(url.as_ref()).call();
if response.is_err() {
warn!(
"Could not load external stylesheet from {}. Error: {}",
url,
response.unwrap_err()
);
return None;
}
let response = response.expect("result");
let css = if url.scheme() == "http" && url.scheme() == "https" {
// Fetch the html from the url
let response = ureq::get(url.as_ref()).call();
if response.is_err() {
warn!(
"Could not load external stylesheet from {}. Error: {}",
url,
response.unwrap_err()
);

if response.status() != 200 {
warn!(
"Could not load external stylesheet from {}. Status code {} ",
url,
response.status()
);
return None;
}
if response.content_type() != "text/css" {
warn!(
"External stylesheet has no text/css content type: {} ",
response.content_type()
);
}
return None;
}
let response = response.expect("result");

let css = match response.into_string() {
Ok(css) => css,
Err(err) => {
if response.status() != 200 {
warn!(
"Could not load external stylesheet from {}. Error: {}",
url, err
"Could not load external stylesheet from {}. Status code {} ",
url,
response.status()
);

return None;
}
if response.content_type() != "text/css" {
warn!(
"External stylesheet has no text/css content type: {} ",
response.content_type()
);
}

match response.into_string() {
Ok(css) => css,
Err(err) => {
warn!(
"Could not load external stylesheet from {}. Error: {}",
url, err
);

return None;
}
}
} else if url.scheme() == "file" {
let path = &url.as_str()[7..];

match std::fs::read_to_string(path) {
Ok(css) => css,
Err(err) => {
warn!(
"Could not load external stylesheet from {}. Error: {}",
url, err
);

return None;
}
}
} else {
warn!(
"Unsupported URL scheme for external stylesheet: {}",
url.scheme()
);
return None;
};

let config = ParserConfig {
Expand Down Expand Up @@ -4325,11 +4345,14 @@ impl<'chars> Html5Parser<'chars> {
}
}
};
// println!("loading external stylesheet: {}", css_url);
println!("loading external stylesheet: {}", css_url);

if let Some(stylesheet) = self.load_external_stylesheet(CssOrigin::Author, css_url)
{
println!("success: loaded external stylesheet");
self.document.get_mut().stylesheets.push(stylesheet);
} else {
println!("failed loading stylesheet")
}
}
_ => {
Expand Down
4 changes: 0 additions & 4 deletions crates/gosub_renderer/src/draw/img.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ pub fn request_img<B: RenderBackend>(
url: &str,
size: SizeU32,
) -> Result<ImageBuffer<B>> {
println!("getting image from url: {}", url);

let res = fetcher.get(url)?;

println!("got response from url: {}", res.status);

if !res.is_ok() {
return Err(anyhow!("Could not get url. Status code {}", res.status));
}
Expand Down

0 comments on commit 27c8ef7

Please sign in to comment.