From f733958f2ecb7f43cab9a9f17b62df09a0a40777 Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Tue, 16 May 2017 09:47:19 +1000 Subject: [PATCH] Add bindings for list and pair list value of nsCSSValue. --- components/style/gecko/rules.rs | 26 +++++++----- .../gecko_bindings/sugar/ns_css_value.rs | 40 +++++++++++++++++++ 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/components/style/gecko/rules.rs b/components/style/gecko/rules.rs index 1bf58b449c4..5c28a6fe896 100644 --- a/components/style/gecko/rules.rs +++ b/components/style/gecko/rules.rs @@ -221,7 +221,7 @@ impl ToNsCssValue for counter_style::Ranges { if self.0.is_empty() { nscssvalue.set_auto(); } else { - for range in &self.0 { + nscssvalue.set_pair_list(self.0.iter().map(|range| { fn set_bound(bound: Option, nscssvalue: &mut nsCSSValue) { if let Some(finite) = bound { nscssvalue.set_integer(finite) @@ -233,8 +233,8 @@ impl ToNsCssValue for counter_style::Ranges { let mut end = nsCSSValue::null(); set_bound(range.start, &mut start); set_bound(range.end, &mut end); - // FIXME: add bindings for nsCSSValuePairList - } + (start, end) + })); } } } @@ -256,16 +256,24 @@ impl ToNsCssValue for counter_style::Fallback { } impl ToNsCssValue for counter_style::Symbols { - fn convert(&self, _nscssvalue: &mut nsCSSValue) { - debug_assert!(!self.0.is_empty()); - // FIXME: add bindings for nsCSSValueList + fn convert(&self, nscssvalue: &mut nsCSSValue) { + nscssvalue.set_list(self.0.iter().map(|item| { + let mut value = nsCSSValue::null(); + value.set_from(item); + value + })); } } impl ToNsCssValue for counter_style::AdditiveSymbols { - fn convert(&self, _nscssvalue: &mut nsCSSValue) { - debug_assert!(!self.0.is_empty()); - // FIXME: add bindings for nsCSSValuePairList + fn convert(&self, nscssvalue: &mut nsCSSValue) { + nscssvalue.set_pair_list(self.0.iter().map(|tuple| { + let mut weight = nsCSSValue::null(); + let mut symbol = nsCSSValue::null(); + weight.set_integer(tuple.weight as i32); + symbol.set_from(&tuple.symbol); + (weight, symbol) + })); } } diff --git a/components/style/gecko_bindings/sugar/ns_css_value.rs b/components/style/gecko_bindings/sugar/ns_css_value.rs index ec18ec2c6a9..409fad108e6 100644 --- a/components/style/gecko_bindings/sugar/ns_css_value.rs +++ b/components/style/gecko_bindings/sugar/ns_css_value.rs @@ -6,6 +6,7 @@ use app_units::Au; use gecko_bindings::bindings; +use gecko_bindings::structs; use gecko_bindings::structs::{nsCSSValue, nsCSSUnit}; use gecko_bindings::structs::{nsCSSValue_Array, nscolor}; use gecko_string_cache::Atom; @@ -220,6 +221,45 @@ impl nsCSSValue { pub fn set_pair(&mut self, x: &nsCSSValue, y: &nsCSSValue) { unsafe { bindings::Gecko_CSSValue_SetPair(self, x, y) } } + + /// Set to a list value + /// + /// This is only supported on the main thread. + pub fn set_list(&mut self, mut values: I) where I: ExactSizeIterator { + debug_assert!(values.len() > 0, "Empty list is not supported"); + unsafe { bindings::Gecko_CSSValue_SetList(self, values.len() as u32); } + debug_assert_eq!(self.mUnit, nsCSSUnit::eCSSUnit_List); + let mut item_ptr = &mut unsafe { + self.mValue.mList.as_ref() // &*nsCSSValueList_heap + .as_mut().expect("List pointer should be non-null") + }._base as *mut structs::nsCSSValueList; + while let Some(item) = unsafe { item_ptr.as_mut() } { + item.mValue = values.next().expect("Values shouldn't have been exhausted"); + item_ptr = item.mNext; + } + debug_assert!(values.next().is_none(), "Values should have been exhausted"); + } + + /// Set to a pair list value + /// + /// This is only supported on the main thread. + pub fn set_pair_list(&mut self, mut values: I) + where I: ExactSizeIterator { + debug_assert!(values.len() > 0, "Empty list is not supported"); + unsafe { bindings::Gecko_CSSValue_SetPairList(self, values.len() as u32); } + debug_assert_eq!(self.mUnit, nsCSSUnit::eCSSUnit_PairList); + let mut item_ptr = &mut unsafe { + self.mValue.mPairList.as_ref() // &*nsCSSValuePairList_heap + .as_mut().expect("List pointer should be non-null") + }._base as *mut structs::nsCSSValuePairList; + while let Some(item) = unsafe { item_ptr.as_mut() } { + let value = values.next().expect("Values shouldn't have been exhausted"); + item.mXValue = value.0; + item.mYValue = value.1; + item_ptr = item.mNext; + } + debug_assert!(values.next().is_none(), "Values should have been exhausted"); + } } impl Drop for nsCSSValue {