Skip to content

Commit

Permalink
refactor(mrml-core): add children_count function
Browse files Browse the repository at this point in the history
Signed-off-by: Jade Ellis <[email protected]>
  • Loading branch information
JadedBlueEyes committed Jul 1, 2024
1 parent 726ef4e commit 2bf91ec
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 7 deletions.
12 changes: 11 additions & 1 deletion packages/mrml-core/src/mj_body/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ impl<'root> Renderer<'root, MjBody, ()> {
self.element.children.iter()
}

#[cfg(feature = "fragment")]
fn children_count(&self) -> usize {
self.children_iter().count()
}

#[cfg(not(feature = "fragment"))]
fn children_count(&self) -> usize {
self.element.children.len()
}

fn get_width(&self) -> Option<Pixel> {
self.attribute("width")
.and_then(|value| Pixel::try_from(value).ok())
Expand Down Expand Up @@ -57,7 +67,7 @@ impl<'root> Renderer<'root, MjBody, ()> {
let element_width = self.get_width();

div.render_open(&mut cursor.buffer)?;
let siblings = self.children_iter().count();
let siblings = self.children_count();
let raw_siblings = self.children_iter().filter(|i| i.is_raw()).count();

for (index, child) in self.children_iter().enumerate() {
Expand Down
15 changes: 13 additions & 2 deletions packages/mrml-core/src/mj_carousel/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,19 @@ impl<'root> Renderer<'root, MjCarousel, MjCarouselExtra> {
fn children_iter(&self) -> impl Iterator<Item = &MjCarouselChild> {
self.element.children.iter()
}

#[cfg(feature = "fragment")]
fn children_count(&self) -> usize {
self.children_iter().count()
}

#[cfg(not(feature = "fragment"))]
fn children_count(&self) -> usize {
self.element.children.len()
}

fn get_thumbnails_width(&self) -> Pixel {
let count = self.children_iter().count();
let count = self.children_count();
if count == 0 {
Pixel::new(0.0)
} else {
Expand Down Expand Up @@ -266,7 +277,7 @@ impl<'root> Renderer<'root, MjCarousel, MjCarouselExtra> {
}

fn render_style(&self) -> Option<String> {
let length = self.children_iter().count();
let length = self.children_count();
if length == 0 {
return None;
}
Expand Down
12 changes: 11 additions & 1 deletion packages/mrml-core/src/mj_column/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ impl<'root> Renderer<'root, MjColumn, MjColumnExtra<'root>> {
self.element.children.iter()
}

#[cfg(feature = "fragment")]
fn children_count(&self) -> usize {
self.children_iter().count()
}

#[cfg(not(feature = "fragment"))]
fn children_count(&self) -> usize {
self.element.children.len()
}

fn current_width(&self) -> Option<Pixel> {
let parent_width = self.container_width.as_ref()?;
let non_raw_siblings = self.non_raw_siblings();
Expand Down Expand Up @@ -242,7 +252,7 @@ impl<'root> Renderer<'root, MjColumn, MjColumnExtra<'root>> {
.add_attribute("width", "100%");
let tbody = Tag::tbody();

let siblings = self.children_iter().count();
let siblings = self.children_count();
let raw_siblings = self.children_iter().filter(|i| i.is_raw()).count();

let current_width = self.current_width();
Expand Down
12 changes: 11 additions & 1 deletion packages/mrml-core/src/mj_group/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ impl<'root> Renderer<'root, MjGroup, ()> {
self.element.children.iter()
}

#[cfg(feature = "fragment")]
fn children_count(&self) -> usize {
self.children_iter().count()
}

#[cfg(not(feature = "fragment"))]
fn children_count(&self) -> usize {
self.element.children.len()
}

fn current_width(&self) -> Pixel {
let parent_width = self.container_width.as_ref().unwrap();
let non_raw_siblings = self.non_raw_siblings();
Expand Down Expand Up @@ -93,7 +103,7 @@ impl<'root> Renderer<'root, MjGroup, ()> {

fn render_children(&self, cursor: &mut RenderCursor) -> Result<(), Error> {
let current_width = self.current_width();
let siblings = self.children_iter().count();
let siblings = self.children_count();
let raw_siblings = self.children_iter().filter(|item| item.is_raw()).count();

for (index, child) in self.children_iter().enumerate() {
Expand Down
12 changes: 11 additions & 1 deletion packages/mrml-core/src/mj_hero/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ impl<'root> Renderer<'root, MjHero, ()> {
self.element.children.iter()
}

#[cfg(feature = "fragment")]
fn children_count(&self) -> usize {
self.children_iter().count()
}

#[cfg(not(feature = "fragment"))]
fn children_count(&self) -> usize {
self.element.children.len()
}

fn set_style_div<'t>(&self, tag: Tag<'t>) -> Tag<'t> {
tag.add_style("margin", "0 auto").maybe_add_style(
"max-width",
Expand Down Expand Up @@ -157,7 +167,7 @@ impl<'root> Renderer<'root, MjHero, ()> {
}

fn render_children(&self, cursor: &mut RenderCursor) -> Result<(), Error> {
let siblings = self.children_iter().count();
let siblings = self.children_count();
let raw_siblings = self.children_iter().filter(|c| c.is_raw()).count();
for (index, child) in self.children_iter().enumerate() {
let mut renderer = child.renderer(self.context());
Expand Down
12 changes: 11 additions & 1 deletion packages/mrml-core/src/mj_raw/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ impl<'root> Renderer<'root, MjRaw, ()> {
fn children_iter(&self) -> impl Iterator<Item = &MjRawChild> {
self.element.children.iter()
}

#[cfg(feature = "fragment")]
fn children_count(&self) -> usize {
self.children_iter().count()
}

#[cfg(not(feature = "fragment"))]
fn children_count(&self) -> usize {
self.element.children.len()
}
}

impl<'root> Render<'root> for Renderer<'root, MjRaw, ()> {
Expand All @@ -48,7 +58,7 @@ impl<'root> Render<'root> for Renderer<'root, MjRaw, ()> {
}

fn render(&self, cursor: &mut RenderCursor) -> Result<(), Error> {
let siblings = self.children_iter().count();
let siblings = self.children_count();
for (index, child) in self.children_iter().enumerate() {
let mut renderer = child.renderer(self.context());
renderer.set_index(index);
Expand Down

0 comments on commit 2bf91ec

Please sign in to comment.