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 Jan 24, 2018
1 parent 2024ef5 commit 0c79fd8
Show file tree
Hide file tree
Showing 4 changed files with 38 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 @@ -2188,18 +2188,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 @@ -2217,7 +2217,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 @@ -2253,7 +2253,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 @@ -365,7 +366,7 @@
pub fn serialize_grid_template<W>(
template_rows: &GridTemplateComponent,
template_columns: &GridTemplateComponent,
template_areas: &Either<TemplateAreas, None_>,
template_areas: &Either<TemplateAreasArc, None_>,
dest: &mut CssWriter<W>,
) -> fmt::Result
where
Expand All @@ -378,7 +379,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 @@ -423,7 +424,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 @@ -456,8 +457,12 @@
impl<'a> ToCss for LonghandsToSerialize<'a> {
#[inline]
fn to_css<W>(&self, dest: &mut CssWriter<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
20 changes: 19 additions & 1 deletion components/style/values/specified/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use cssparser::Parser;
use hash::FnvHashMap;
use parser::{Parse, ParserContext};
use selectors::parser::SelectorParseErrorKind;
use servo_arc::Arc;
use std::fmt::{self, Write};
use std::ops::Range;
use str::HTML_SPACE_CHARACTERS;
Expand Down Expand Up @@ -624,6 +625,23 @@ impl Parse for TemplateAreas {

trivial_to_computed_value!(TemplateAreas);

#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToCss)]
/// Arc type for `Arc<TemplateAreas>`
pub struct TemplateAreasArc(#[ignore_malloc_size_of = "Arc"] 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)))
}
}

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 @@ -673,7 +691,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 0c79fd8

Please sign in to comment.