Skip to content

Commit

Permalink
Implement GridTemplateAreas with reference counting
Browse files Browse the repository at this point in the history
  • Loading branch information
CYBAI committed Dec 24, 2017
1 parent bcdb82b commit c6dac38
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
10 changes: 5 additions & 5 deletions components/style/properties/gecko.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2264,18 +2264,18 @@ fn static_assert() {

let mut refptr = unsafe {
UniqueRefPtr::from_addrefed(
Gecko_NewGridTemplateAreasValue(v.areas.len() as u32, v.strings.len() as u32, v.width))
Gecko_NewGridTemplateAreasValue(v.0.areas.len() as u32, v.0.strings.len() as u32, v.0.width))
};

for (servo, gecko) in v.areas.into_iter().zip(refptr.mNamedAreas.iter_mut()) {
for (servo, gecko) in v.0.areas.into_iter().zip(refptr.mNamedAreas.iter_mut()) {
gecko.mName.assign_utf8(&*servo.name);
gecko.mColumnStart = servo.columns.start;
gecko.mColumnEnd = servo.columns.end;
gecko.mRowStart = servo.rows.start;
gecko.mRowEnd = servo.rows.end;
}

for (servo, gecko) in v.strings.into_iter().zip(refptr.mTemplates.iter_mut()) {
for (servo, gecko) in v.0.strings.into_iter().zip(refptr.mTemplates.iter_mut()) {
gecko.assign_utf8(&*servo);
}

Expand All @@ -2293,7 +2293,7 @@ fn static_assert() {
pub fn clone_grid_template_areas(&self) -> values::computed::position::GridTemplateAreas {
use std::ops::Range;
use values::None_;
use values::specified::position::{NamedArea, TemplateAreas};
use values::specified::position::{NamedArea, TemplateAreas, TemplateAreasArc};

if self.gecko.mGridTemplateAreas.mRawPtr.is_null() {
return Either::Second(None_);
Expand Down Expand Up @@ -2329,7 +2329,7 @@ fn static_assert() {
(*gecko_grid_template_areas).mNColumns
};

Either::First(TemplateAreas{ areas, strings, width })
Either::First(TemplateAreasArc(Arc::new(TemplateAreas{ areas, strings, width })))
}

</%self:impl_trait>
Expand Down
2 changes: 1 addition & 1 deletion components/style/properties/longhand/position.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,5 +301,5 @@ ${helpers.predefined_type("grid-template-areas",
initial_value="computed::GridTemplateAreas::none()",
products="gecko",
animation_value_type="discrete",
boxed=True,
gecko_pref="layout.css.grid.enabled",
spec="https://drafts.csswg.org/css-grid/#propdef-grid-template-areas")}
21 changes: 13 additions & 8 deletions components/style/properties/shorthand/position.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,19 @@
spec="https://drafts.csswg.org/css-grid/#propdef-grid-template"
products="gecko">
use parser::Parse;
use servo_arc::Arc;
use values::{Either, None_};
use values::generics::grid::{LineNameList, TrackSize, TrackList, TrackListType};
use values::generics::grid::{TrackListValue, concat_serialize_idents};
use values::specified::{GridTemplateComponent, GenericGridTemplateComponent};
use values::specified::grid::parse_line_names;
use values::specified::position::TemplateAreas;
use values::specified::position::{TemplateAreas, TemplateAreasArc};

/// Parsing for `<grid-template>` shorthand (also used by `grid` shorthand).
pub fn parse_grid_template<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<(GridTemplateComponent,
GridTemplateComponent,
Either<TemplateAreas, None_>), ParseError<'i>> {
Either<TemplateAreasArc, None_>), ParseError<'i>> {
// Other shorthand sub properties also parse `none` and `subgrid` keywords and this
// shorthand should know after these keywords there is nothing to parse. Otherwise it
// gets confused and rejects the sub properties that contains `none` or `subgrid`.
Expand Down Expand Up @@ -332,7 +333,7 @@
};

Ok((GenericGridTemplateComponent::TrackList(template_rows),
template_cols, Either::First(template_areas)))
template_cols, Either::First(TemplateAreasArc(Arc::new(template_areas)))))
} else {
let mut template_rows = GridTemplateComponent::parse(context, input)?;
if let GenericGridTemplateComponent::TrackList(ref mut list) = template_rows {
Expand Down Expand Up @@ -364,7 +365,7 @@
/// Serialization for `<grid-template>` shorthand (also used by `grid` shorthand).
pub fn serialize_grid_template<W>(template_rows: &GridTemplateComponent,
template_columns: &GridTemplateComponent,
template_areas: &Either<TemplateAreas, None_>,
template_areas: &Either<TemplateAreasArc, None_>,
dest: &mut W) -> fmt::Result where W: fmt::Write {
match *template_areas {
Either::Second(_none) => {
Expand All @@ -374,7 +375,7 @@
},
Either::First(ref areas) => {
// The length of template-area and template-rows values should be equal.
if areas.strings.len() != template_rows.track_list_len() {
if areas.0.strings.len() != template_rows.track_list_len() {
return Ok(());
}

Expand Down Expand Up @@ -419,7 +420,7 @@
}

let mut names_iter = track_list.line_names.iter();
for (((i, string), names), value) in areas.strings.iter().enumerate()
for (((i, string), names), value) in areas.0.strings.iter().enumerate()
.zip(&mut names_iter)
.zip(track_list.values.iter()) {
if i > 0 {
Expand Down Expand Up @@ -452,8 +453,12 @@
impl<'a> ToCss for LonghandsToSerialize<'a> {
#[inline]
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
serialize_grid_template(self.grid_template_rows, self.grid_template_columns,
self.grid_template_areas, dest)
serialize_grid_template(
self.grid_template_rows,
self.grid_template_columns,
self.grid_template_areas,
dest
)
}
}
</%helpers:shorthand>
Expand Down
29 changes: 28 additions & 1 deletion components/style/values/specified/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

use cssparser::Parser;
use hash::FnvHashMap;
#[cfg(feature = "gecko")]
use malloc_size_of::{MallocSizeOf, MallocConditionalSizeOf, MallocSizeOfOps};
use parser::{Parse, ParserContext};
use selectors::parser::SelectorParseErrorKind;
use servo_arc::Arc;
use std::fmt;
use std::ops::Range;
use str::HTML_SPACE_CHARACTERS;
Expand Down Expand Up @@ -612,6 +615,30 @@ impl Parse for TemplateAreas {

trivial_to_computed_value!(TemplateAreas);

#[derive(Clone, Debug, PartialEq, ToCss)]
/// Arc type for `Arc<TemplateAreas>`
pub struct TemplateAreasArc(pub Arc<TemplateAreas>);

impl Parse for TemplateAreasArc {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let parsed = TemplateAreas::parse(context, input)?;

Ok(TemplateAreasArc(Arc::new(parsed)))
}
}

#[cfg(feature = "gecko")]
impl MallocSizeOf for TemplateAreasArc {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.0.conditional_size_of(ops)
}
}

trivial_to_computed_value!(TemplateAreasArc);

#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
#[derive(Clone, Debug, PartialEq)]
/// Not associated with any particular grid item, but can
Expand Down Expand Up @@ -661,7 +688,7 @@ fn is_name_code_point(c: char) -> bool {
/// The syntax of this property also provides a visualization of
/// the structure of the grid, making the overall layout of
/// the grid container easier to understand.
pub type GridTemplateAreas = Either<TemplateAreas, None_>;
pub type GridTemplateAreas = Either<TemplateAreasArc, None_>;

impl GridTemplateAreas {
#[inline]
Expand Down

0 comments on commit c6dac38

Please sign in to comment.