Skip to content

Commit

Permalink
Implement clone_content method
Browse files Browse the repository at this point in the history
  • Loading branch information
dadaa committed Aug 15, 2017
1 parent e0b8340 commit c05baa2
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 15 deletions.
13 changes: 13 additions & 0 deletions components/style/gecko/conversions.rs
Expand Up @@ -919,3 +919,16 @@ impl<T> Rect<T> where T: GeckoStyleCoordConvertible {
)
}
}

/// Convert to String from given chars pointer.
pub unsafe fn string_from_chars_pointer(p: *const u16) -> String {
use std::slice;
let mut length = 0;
let mut iter = p;
while *iter != 0 {
length += 1;
iter = iter.offset(1);
}
let char_vec = slice::from_raw_parts(p, length as usize);
String::from_utf16_lossy(char_vec)
}
21 changes: 6 additions & 15 deletions components/style/gecko/media_queries.rs
Expand Up @@ -14,7 +14,7 @@ use font_metrics::get_metrics_provider_for_product;
use gecko::values::convert_nscolor_to_rgba;
use gecko_bindings::bindings;
use gecko_bindings::structs;
use gecko_bindings::structs::{nsCSSKeyword, nsCSSProps_KTableEntry, nsCSSValue, nsCSSUnit, nsStringBuffer};
use gecko_bindings::structs::{nsCSSKeyword, nsCSSProps_KTableEntry, nsCSSValue, nsCSSUnit};
use gecko_bindings::structs::{nsMediaExpression_Range, nsMediaFeature};
use gecko_bindings::structs::{nsMediaFeature_ValueType, nsMediaFeature_RangeType, nsMediaFeature_RequirementFlags};
use gecko_bindings::structs::{nsPresContext, RawGeckoPresContextOwned};
Expand Down Expand Up @@ -294,19 +294,6 @@ impl ToCss for Resolution {
}
}

unsafe fn string_from_ns_string_buffer(buffer: *const nsStringBuffer) -> String {
use std::slice;
debug_assert!(!buffer.is_null());
let data = buffer.offset(1) as *const u16;
let mut length = 0;
let mut iter = data;
while *iter != 0 {
length += 1;
iter = iter.offset(1);
}
String::from_utf16_lossy(slice::from_raw_parts(data, length))
}

/// A value found or expected in a media expression.
#[derive(PartialEq, Debug, Clone)]
pub enum MediaExpressionValue {
Expand Down Expand Up @@ -334,6 +321,8 @@ pub enum MediaExpressionValue {

impl MediaExpressionValue {
fn from_css_value(for_expr: &Expression, css_value: &nsCSSValue) -> Option<Self> {
use gecko::conversions::string_from_chars_pointer;

// NB: If there's a null value, that means that we don't support the
// feature.
if css_value.mUnit == nsCSSUnit::eCSSUnit_Null {
Expand Down Expand Up @@ -372,7 +361,9 @@ impl MediaExpressionValue {
nsMediaFeature_ValueType::eIdent => {
debug_assert!(css_value.mUnit == nsCSSUnit::eCSSUnit_Ident);
let string = unsafe {
string_from_ns_string_buffer(*css_value.mValue.mString.as_ref())
let buffer = *css_value.mValue.mString.as_ref();
debug_assert!(!buffer.is_null());
string_from_chars_pointer(buffer.offset(1) as *const u16)
};
Some(MediaExpressionValue::Ident(string))
}
Expand Down
74 changes: 74 additions & 0 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -5579,6 +5579,80 @@ clip-path
self.copy_content_from(other)
}

pub fn clone_content(&self) -> longhands::content::computed_value::T {
use gecko::conversions::string_from_chars_pointer;
use gecko_bindings::structs::nsStyleContentType::*;
use properties::longhands::content::computed_value::{T, ContentItem};
use values::generics::CounterStyleOrNone;
use values::specified::url::SpecifiedUrl;
use values::specified::Attr;

if self.gecko.mContents.is_empty() {
return T::Normal;
}

if self.gecko.mContents.len() == 1 &&
self.gecko.mContents[0].mType == eStyleContentType_AltContent {
return T::MozAltContent;
}

T::Items(
self.gecko.mContents.iter().map(|gecko_content| {
match gecko_content.mType {
eStyleContentType_OpenQuote => ContentItem::OpenQuote,
eStyleContentType_CloseQuote => ContentItem::CloseQuote,
eStyleContentType_NoOpenQuote => ContentItem::NoOpenQuote,
eStyleContentType_NoCloseQuote => ContentItem::NoCloseQuote,
eStyleContentType_String => {
let gecko_chars = unsafe { gecko_content.mContent.mString.as_ref() };
let string = unsafe { string_from_chars_pointer(*gecko_chars) };
ContentItem::String(string)
},
eStyleContentType_Attr => {
let gecko_chars = unsafe { gecko_content.mContent.mString.as_ref() };
let string = unsafe { string_from_chars_pointer(*gecko_chars) };
let (namespace, attribute) =
match string.find('|') {
None => (None, string),
Some(index) => {
let (_, val) = string.split_at(index);
// FIXME: We should give NamespaceId as well to make Attr
// struct. However, there is no field for it in Gecko.
debug_assert!(false, "Attr with namespace does not support yet");
(None, val.to_string())
}
};
ContentItem::Attr(Attr { namespace, attribute })
},
eStyleContentType_Counter | eStyleContentType_Counters => {
let gecko_function =
unsafe { &**gecko_content.mContent.mCounters.as_ref() };
let ident = gecko_function.mIdent.to_string();
let style =
CounterStyleOrNone::from_gecko_value(&gecko_function.mCounterStyle);
if gecko_content.mType == eStyleContentType_Counter {
ContentItem::Counter(ident, style)
} else {
let separator = gecko_function.mSeparator.to_string();
ContentItem::Counters(ident, separator, style)
}
},
eStyleContentType_Image => {
unsafe {
let gecko_image_request =
unsafe { &**gecko_content.mContent.mImage.as_ref() };
ContentItem::Url(
SpecifiedUrl::from_image_request(gecko_image_request)
.expect("mContent could not convert to SpecifiedUrl")
)
}
},
x => panic!("Found unexpected value in style struct for content property: {:?}", x),
}
}).collect()
)
}

% for counter_property in ["Increment", "Reset"]:
pub fn set_counter_${counter_property.lower()}(&mut self, v: longhands::counter_increment::computed_value::T) {
unsafe {
Expand Down

0 comments on commit c05baa2

Please sign in to comment.