Skip to content

Commit 282575e

Browse files
committed
feat: Support for the picture element
1 parent 1bd8052 commit 282575e

File tree

7 files changed

+458
-16
lines changed

7 files changed

+458
-16
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ style_traits = { version = "0.3", package = "stylo_traits" }
3030
style_config = { version = "0.3", package = "stylo_config" }
3131
style_dom = { version = "0.3", package = "stylo_dom" }
3232
selectors = { version = "0.28", package = "selectors" }
33+
cssparser = "0.35"
34+
mime = "0.3"
3335

3436
markup5ever = "0.16.1" # needs to match stylo web_atoms version
3537
html5ever = "0.31" # needs to match stylo web_atoms version

packages/blitz-dom/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ atomic_refcell = { workspace = true, features = ["serde"] }
4141
string_cache = { workspace = true }
4242
markup5ever = { workspace = true }
4343
smallvec = { workspace = true }
44+
cssparser = { workspace = true }
45+
mime = { workspace = true }
4446

4547
# DioxusLabs dependencies
4648
taffy = { workspace = true }

packages/blitz-dom/src/document.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use style::properties::ComputedValues;
2121
use style::properties::style_structs::Font;
2222
use style::values::GenericAtomIdent;
2323
use style::values::computed::Overflow;
24+
use style_traits::ParsingMode;
2425
// use quadtree_rs::Quadtree;
2526
use crate::net::{Resource, StylesheetLoader};
2627
use selectors::{Element, matching::QuirksMode};
@@ -38,9 +39,12 @@ use style::servo_arc::Arc as ServoArc;
3839
use style::{
3940
dom::{TDocument, TNode},
4041
media_queries::{Device, MediaList},
42+
parser::ParserContext,
4143
selector_parser::SnapshotMap,
4244
shared_lock::{SharedRwLock, StylesheetGuards},
43-
stylesheets::{AllowImportRules, DocumentStyleSheet, Origin, Stylesheet, UrlExtraData},
45+
stylesheets::{
46+
AllowImportRules, CssRuleType, DocumentStyleSheet, Origin, Stylesheet, UrlExtraData,
47+
},
4448
stylist::Stylist,
4549
};
4650
use taffy::AvailableSpace;
@@ -1225,6 +1229,32 @@ impl BaseDocument {
12251229
);
12261230
chain
12271231
}
1232+
1233+
/// https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia
1234+
pub fn match_media(&self, media_query_string: &str) -> bool {
1235+
let mut input = cssparser::ParserInput::new(media_query_string);
1236+
let mut parser = cssparser::Parser::new(&mut input);
1237+
1238+
let url_data = UrlExtraData::from(
1239+
self.base_url
1240+
.clone()
1241+
.unwrap_or_else(|| "about:blank".parse::<Url>().unwrap()),
1242+
);
1243+
let quirks_mode = self.stylist.quirks_mode();
1244+
let context = ParserContext::new(
1245+
Origin::Author,
1246+
&url_data,
1247+
Some(CssRuleType::Style),
1248+
ParsingMode::all(),
1249+
quirks_mode,
1250+
Default::default(),
1251+
None,
1252+
None,
1253+
);
1254+
1255+
let media_list = MediaList::parse(&context, &mut parser);
1256+
media_list.evaluate(self.stylist.device(), quirks_mode)
1257+
}
12281258
}
12291259

12301260
impl AsRef<BaseDocument> for BaseDocument {

packages/blitz-dom/src/mutator.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::collections::HashSet;
22

3-
use crate::net::{CssHandler, ImageHandler};
3+
use crate::net::CssHandler;
44
use crate::node::NodeSpecificData;
5-
use crate::util::ImageType;
65
use crate::{Attribute, BaseDocument, ElementNodeData, NodeData, QualName, local_name, ns};
76
use blitz_traits::net::Request;
87
use style::invalidation::element::restyle_hints::RestyleHint;
@@ -84,6 +83,7 @@ impl DocumentMutator<'_> {
8483
let child_ids = std::mem::take(&mut self.doc.nodes[old_parent_id].children);
8584
self.maybe_push_style_node(old_parent_id);
8685
self.append_children(new_parent_id, &child_ids);
86+
self.maybe_load_image(&child_ids);
8787
}
8888

8989
pub fn append_children(&mut self, parent_id: usize, child_ids: &[usize]) {
@@ -99,6 +99,7 @@ impl DocumentMutator<'_> {
9999
}
100100

101101
self.maybe_push_style_node(parent_id);
102+
self.maybe_load_image(child_ids);
102103
}
103104

104105
pub fn replace_node_with(&mut self, anchor_node_id: usize, new_node_ids: &[usize]) {
@@ -137,11 +138,13 @@ impl DocumentMutator<'_> {
137138
}
138139

139140
self.maybe_push_parent_style_node(anchor_node_id);
141+
self.maybe_load_image(new_node_ids);
140142
}
141143

142144
pub fn insert_nodes_before(&mut self, anchor_node_id: usize, new_node_ids: &[usize]) {
143145
self.doc.insert_before(anchor_node_id, new_node_ids);
144146
self.maybe_push_parent_style_node(anchor_node_id);
147+
self.maybe_load_image(new_node_ids);
145148
}
146149

147150
pub fn remove_node_if_unparented(&mut self, node_id: usize) {
@@ -210,7 +213,7 @@ impl DocumentMutator<'_> {
210213
let tag = node.element_data().unwrap().name.local.as_ref();
211214
match tag {
212215
"link" => self.load_linked_stylesheet(id),
213-
"img" => self.load_image(id),
216+
// "img" => self.doc.load_image(id),
214217
"style" => {
215218
self.style_nodes.insert(id);
216219
}
@@ -255,7 +258,8 @@ impl DocumentMutator<'_> {
255258
};
256259

257260
let attr = name.local.as_ref();
258-
let load_image = element.name.local == local_name!("img") && attr == "src";
261+
let load_image =
262+
element.name.local == local_name!("img") && (attr == "src" || attr == "srcset");
259263

260264
if element.name.local == local_name!("input") && attr == "checked" {
261265
set_input_checked_state(element, value.to_string());
@@ -284,7 +288,7 @@ impl DocumentMutator<'_> {
284288
}
285289

286290
if load_image {
287-
self.load_image(node_id);
291+
self.doc.load_image(node_id);
288292
}
289293
}
290294

@@ -377,16 +381,16 @@ impl<'doc> DocumentMutator<'doc> {
377381
);
378382
}
379383

380-
fn load_image(&mut self, target_id: usize) {
381-
let node = &self.doc.nodes[target_id];
382-
if let Some(raw_src) = node.attr(local_name!("src")) {
383-
if !raw_src.is_empty() {
384-
let src = self.doc.resolve_url(raw_src);
385-
self.doc.net_provider.fetch(
386-
self.doc.id(),
387-
Request::get(src),
388-
Box::new(ImageHandler::new(target_id, ImageType::Image)),
389-
);
384+
fn is_img_node(&self, node_id: usize) -> bool {
385+
self.doc.nodes[node_id]
386+
.data
387+
.is_element_with_tag_name(&local_name!("img"))
388+
}
389+
390+
fn maybe_load_image(&self, node_ids: &[usize]) {
391+
for id in node_ids.iter() {
392+
if self.is_img_node(*id) {
393+
self.doc.load_image(*id);
390394
}
391395
}
392396
}

packages/blitz-dom/src/node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ use url::Url;
3737
use crate::layout::table::TableContext;
3838
use blitz_traits::{BlitzMouseButtonEvent, DomEventData, HitResult};
3939

40+
pub mod image;
41+
4042
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4143
pub enum DisplayOuter {
4244
Block,

0 commit comments

Comments
 (0)