diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2018-07-20 17:25:14 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-20 17:25:14 -0400 |
commit | aa76909f55e126957dfc163db9512ffe344c348f (patch) | |
tree | ada1d986f5224ec9a7f2556bfb0869bedc75d200 | |
parent | 1a9943d736dcba7c8e07adfa2fcc64ea0943c9e6 (diff) | |
parent | c881c2a0fbaf21a33220ab28d2560cc9060ed59c (diff) | |
download | servo-aa76909f55e126957dfc163db9512ffe344c348f.tar.gz servo-aa76909f55e126957dfc163db9512ffe344c348f.zip |
Auto merge of #21215 - nupurbaghel:update_source_set, r=jdm
Implement step 5 of select_image_source
<!-- Please describe your changes on the following line: -->
This step is responsible for selecting an image source based on user agent's environment( closest value which is greater than user's pixel density)
In case of no device, maximum density source is selected.
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #11416
<!-- Either: -->
- [x] These changes do not require tests because tests are already present
<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->
<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/21215)
<!-- Reviewable:end -->
-rw-r--r-- | components/script/dom/htmlimageelement.rs | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/components/script/dom/htmlimageelement.rs b/components/script/dom/htmlimageelement.rs index 4ec57511f8d..271665eff15 100644 --- a/components/script/dom/htmlimageelement.rs +++ b/components/script/dom/htmlimageelement.rs @@ -517,14 +517,14 @@ impl HTMLImageElement { } } - fn evaluate_source_size_list(&self, source_size_list: &mut SourceSizeList, width: Option<Length>) -> Au { + fn evaluate_source_size_list(&self, source_size_list: &mut SourceSizeList, _width: Option<Length>) -> Au { let document = document_from_node(self); let device = document.device(); if !device.is_some() { return Au(1); } let quirks_mode = document.quirks_mode(); - source_size_list.set_fallback_value(width); + //FIXME https://github.com/whatwg/html/issues/3832 source_size_list.evaluate(&device.unwrap(), quirks_mode) } @@ -603,15 +603,33 @@ impl HTMLImageElement { } } } + + let mut max = (0f64, 0); let img_sources = &mut vec![]; - for outer_index in 0..len { - if !repeat_indices.contains(&outer_index) { - img_sources.push(&source_set.image_sources[outer_index]); + for (index, image_source) in source_set.image_sources.iter().enumerate() { + if repeat_indices.contains(&index) { + continue; + } + let den = image_source.descriptor.den.unwrap(); + if max.0 < den { + max = (den, img_sources.len()); } + img_sources.push(image_source); } - // TODO Step 5 - select source based on pixel density - let selected_source = img_sources.remove(0).clone(); + // Step 5 + let mut best_candidate = max; + let device = document_from_node(self).device(); + if let Some(device) = device { + let device_den = device.device_pixel_ratio().get() as f64; + for (index, image_source) in img_sources.iter().enumerate() { + let current_den = image_source.descriptor.den.unwrap(); + if current_den < best_candidate.0 && current_den >= device_den { + best_candidate = (current_den, index); + } + } + } + let selected_source = img_sources.remove(best_candidate.1).clone(); Some((DOMString::from_string(selected_source.url), selected_source.descriptor.den.unwrap() as f32)) } |