Skip to content

Commit

Permalink
Variable renames (yewstack#696)
Browse files Browse the repository at this point in the history
* childs -> children

* precursor -> previous_sibling

* cargo fmt and name changes
  • Loading branch information
jstarry authored and llebout committed Jan 20, 2020
1 parent 7a54b73 commit feebec8
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 57 deletions.
2 changes: 1 addition & 1 deletion crates/macro/src/html_tree/html_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl ToTokens for HtmlList {
tokens.extend(quote! {
::yew::virtual_dom::VNode::VList(
::yew::virtual_dom::vlist::VList {
childs: vec![#(#html_trees,)*],
children: vec![#(#html_trees,)*],
}
)
});
Expand Down
6 changes: 3 additions & 3 deletions examples/crm/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ where
pre.add_child(top.into());
top = pre;
} else if let Tag::Table(aligns) = tag {
for r in top.childs.iter_mut() {
for r in top.children.iter_mut() {
if let &mut VNode::VTag(ref mut vtag) = r {
for (i, c) in vtag.childs.iter_mut().enumerate() {
for (i, c) in vtag.children.iter_mut().enumerate() {
if let &mut VNode::VTag(ref mut vtag) = c {
match aligns[i] {
Alignment::None => {}
Expand All @@ -51,7 +51,7 @@ where
}
}
} else if let Tag::TableHead = tag {
for c in top.childs.iter_mut() {
for c in top.children.iter_mut() {
if let &mut VNode::VTag(ref mut vtag) = c {
// TODO
// vtag.tag = "th".into();
Expand Down
2 changes: 1 addition & 1 deletion src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ where
{
fn render(&self) -> Html<COMP> {
VList {
childs: self.iter().map(|c| c.into()).collect(),
children: self.iter().map(|c| c.into()).collect(),
}
.into()
}
Expand Down
6 changes: 3 additions & 3 deletions src/virtual_dom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ enum Reform {
/// The optional `Node` is used to insert the
/// new node in the correct slot of the parent.
///
/// If it does not exist, a `precursor` must be
/// If it does not exist, a `previous_sibling` must be
/// specified (see `VDiff::apply()`).
Before(Option<Node>),
}
Expand All @@ -158,7 +158,7 @@ pub trait VDiff {
///
/// Parameters:
/// - `parent`: the parent node in the DOM.
/// - `precursor`: the "previous node" in a list of nodes, used to efficiently
/// - `previous_sibling`: the "previous node" in a list of nodes, used to efficiently
/// find where to put the node.
/// - `ancestor`: the node that this node will be replacing in the DOM.
/// This method will _always_ remove the `ancestor` from the `parent`.
Expand All @@ -175,7 +175,7 @@ pub trait VDiff {
fn apply(
&mut self,
parent: &Element,
precursor: Option<&Node>,
previous_sibling: Option<&Node>,
ancestor: Option<VNode<Self::Component>>,
scope: &Scope<Self::Component>,
) -> Option<Node>;
Expand Down
11 changes: 6 additions & 5 deletions src/virtual_dom/vcomp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ where
fn apply(
&mut self,
parent: &Element,
precursor: Option<&Node>,
previous_sibling: Option<&Node>,
ancestor: Option<VNode<Self::Component>>,
env: &Scope<Self::Component>,
) -> Option<Node> {
Expand Down Expand Up @@ -308,11 +308,12 @@ where
.insert_before(&element, &sibling)
.expect("can't insert dummy element for a component");
} else {
let precursor = precursor.and_then(|before| before.next_sibling());
if let Some(precursor) = precursor {
let previous_sibling =
previous_sibling.and_then(|before| before.next_sibling());
if let Some(previous_sibling) = previous_sibling {
parent
.insert_before(&element, &precursor)
.expect("can't insert dummy element before precursor");
.insert_before(&element, &previous_sibling)
.expect("can't insert dummy element before previous_sibling");
} else {
parent.append_child(&element);
}
Expand Down
31 changes: 17 additions & 14 deletions src/virtual_dom/vlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use stdweb::web::{Element, Node};
/// This struct represents a fragment of the Virtual DOM tree.
pub struct VList<COMP: Component> {
/// The list of children nodes. Which also could have their own children.
pub childs: Vec<VNode<COMP>>,
pub children: Vec<VNode<COMP>>,
}

impl<COMP: Component> Default for VList<COMP> {
Expand All @@ -18,12 +18,14 @@ impl<COMP: Component> Default for VList<COMP> {
impl<COMP: Component> VList<COMP> {
/// Creates a new empty `VList` instance.
pub fn new() -> Self {
VList { childs: Vec::new() }
VList {
children: Vec::new(),
}
}

/// Add `VNode` child.
pub fn add_child(&mut self, child: VNode<COMP>) {
self.childs.push(child);
self.children.push(child);
}
}

Expand All @@ -32,7 +34,7 @@ impl<COMP: Component> VDiff for VList<COMP> {

fn detach(&mut self, parent: &Element) -> Option<Node> {
let mut last_sibling = None;
for mut child in self.childs.drain(..) {
for mut child in self.children.drain(..) {
last_sibling = child.detach(parent);
}
last_sibling
Expand All @@ -41,18 +43,18 @@ impl<COMP: Component> VDiff for VList<COMP> {
fn apply(
&mut self,
parent: &Element,
precursor: Option<&Node>,
previous_sibling: Option<&Node>,
ancestor: Option<VNode<Self::Component>>,
env: &Scope<Self::Component>,
) -> Option<Node> {
// Reuse precursor, because fragment reuse parent
let mut precursor = precursor.map(|node| node.to_owned());
// Reuse previous_sibling, because fragment reuse parent
let mut previous_sibling = previous_sibling.cloned();
let mut rights = {
match ancestor {
// If element matched this type
Some(VNode::VList(vlist)) => {
// Previously rendered items
vlist.childs
vlist.children
}
Some(vnode) => {
// Use the current node as a single fragment list
Expand All @@ -63,32 +65,33 @@ impl<COMP: Component> VDiff for VList<COMP> {
}
};

if self.childs.is_empty() {
if self.children.is_empty() {
// Fixes: https://github.com/yewstack/yew/issues/294
// Without a placeholder the next element becomes first
// and corrupts the order of rendering
// We use empty text element to stake out a place
let placeholder = VText::new("".into());
self.childs.push(placeholder.into());
self.children.push(placeholder.into());
}

// Process children
let mut lefts = self.childs.iter_mut();
let mut lefts = self.children.iter_mut();
let mut rights = rights.drain(..);
loop {
match (lefts.next(), rights.next()) {
(Some(left), Some(right)) => {
precursor = left.apply(parent, precursor.as_ref(), Some(right), &env);
previous_sibling =
left.apply(parent, previous_sibling.as_ref(), Some(right), &env);
}
(Some(left), None) => {
precursor = left.apply(parent, precursor.as_ref(), None, &env);
previous_sibling = left.apply(parent, previous_sibling.as_ref(), None, &env);
}
(None, Some(ref mut right)) => {
right.detach(parent);
}
(None, None) => break,
}
}
precursor
previous_sibling
}
}
10 changes: 5 additions & 5 deletions src/virtual_dom/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ impl<COMP: Component> VDiff for VNode<COMP> {
fn apply(
&mut self,
parent: &Element,
precursor: Option<&Node>,
previous_sibling: Option<&Node>,
ancestor: Option<VNode<Self::Component>>,
env: &Scope<Self::Component>,
) -> Option<Node> {
match *self {
VNode::VTag(ref mut vtag) => vtag.apply(parent, precursor, ancestor, env),
VNode::VText(ref mut vtext) => vtext.apply(parent, precursor, ancestor, env),
VNode::VComp(ref mut vcomp) => vcomp.apply(parent, precursor, ancestor, env),
VNode::VList(ref mut vlist) => vlist.apply(parent, precursor, ancestor, env),
VNode::VTag(ref mut vtag) => vtag.apply(parent, previous_sibling, ancestor, env),
VNode::VText(ref mut vtext) => vtext.apply(parent, previous_sibling, ancestor, env),
VNode::VComp(ref mut vcomp) => vcomp.apply(parent, previous_sibling, ancestor, env),
VNode::VList(ref mut vlist) => vlist.apply(parent, previous_sibling, ancestor, env),
VNode::VRef(ref mut node) => {
let sibling = match ancestor {
Some(mut n) => n.detach(parent),
Expand Down
36 changes: 19 additions & 17 deletions src/virtual_dom/vtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct VTag<COMP: Component> {
/// List of attributes.
pub attributes: Attributes,
/// The list of children nodes. Which also could have own children.
pub childs: Vec<VNode<COMP>>,
pub children: Vec<VNode<COMP>>,
/// List of attached classes.
pub classes: Classes,
/// Contains a value of an
Expand Down Expand Up @@ -63,7 +63,7 @@ impl<COMP: Component> VTag<COMP> {
attributes: Attributes::new(),
listeners: Vec::new(),
captured: Vec::new(),
childs: Vec::new(),
children: Vec::new(),
value: None,
kind: None,
// In HTML node `checked` attribute sets `defaultChecked` parameter,
Expand All @@ -79,13 +79,13 @@ impl<COMP: Component> VTag<COMP> {

/// Add `VNode` child.
pub fn add_child(&mut self, child: VNode<COMP>) {
self.childs.push(child);
self.children.push(child);
}

/// Add multiple `VNode` children.
pub fn add_children(&mut self, children: Vec<VNode<COMP>>) {
for child in children {
self.childs.push(child);
self.children.push(child);
}
}

Expand Down Expand Up @@ -352,7 +352,7 @@ impl<COMP: Component> VDiff for VTag<COMP> {
.expect("tried to remove not rendered VTag from DOM");

// recursively remove its children
self.childs.drain(..).for_each(|mut child| {
self.children.drain(..).for_each(|mut child| {
child.detach(&node);
});

Expand All @@ -368,7 +368,7 @@ impl<COMP: Component> VDiff for VTag<COMP> {
fn apply(
&mut self,
parent: &Element,
precursor: Option<&Node>,
previous_sibling: Option<&Node>,
ancestor: Option<VNode<Self::Component>>,
env: &Scope<Self::Component>,
) -> Option<Node> {
Expand Down Expand Up @@ -425,11 +425,12 @@ impl<COMP: Component> VDiff for VTag<COMP> {
.insert_before(&element, &sibling)
.expect("can't insert tag before sibling");
} else {
let precursor = precursor.and_then(|before| before.next_sibling());
if let Some(precursor) = precursor {
let previous_sibling =
previous_sibling.and_then(|before| before.next_sibling());
if let Some(previous_sibling) = previous_sibling {
parent
.insert_before(&element, &precursor)
.expect("can't insert tag before precursor");
.insert_before(&element, &previous_sibling)
.expect("can't insert tag before previous_sibling");
} else {
parent.append_child(&element);
}
Expand Down Expand Up @@ -457,14 +458,15 @@ impl<COMP: Component> VDiff for VTag<COMP> {
}

// Process children
// Start with an empty precursor, because it put childs to itself
let mut precursor = None;
let mut self_childs = self.childs.iter_mut();
let mut ancestor_childs = ancestor.into_iter().flat_map(|a| a.childs);
// Start with an empty previous_sibling, because it put children to itself
let mut previous_sibling = None;
let mut self_children = self.children.iter_mut();
let mut ancestor_children = ancestor.into_iter().flat_map(|a| a.children);
loop {
match (self_childs.next(), ancestor_childs.next()) {
match (self_children.next(), ancestor_children.next()) {
(Some(left), right) => {
precursor = left.apply(&element, precursor.as_ref(), right, &env);
previous_sibling =
left.apply(&element, previous_sibling.as_ref(), right, &env);
}
(None, Some(ref mut right)) => {
right.detach(&element);
Expand Down Expand Up @@ -514,7 +516,7 @@ impl<COMP: Component> PartialEq for VTag<COMP> {
&& self.attributes == other.attributes
&& self.classes.set.len() == other.classes.set.len()
&& self.classes.set.iter().eq(other.classes.set.iter())
&& &self.childs == &other.childs
&& &self.children == &other.children
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/virtual_dom/vtext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ impl<COMP: Component> VDiff for VText<COMP> {

/// Renders virtual node over existent `TextNode`, but
/// only if value of text had changed.
/// Parameter `precursor` is necessary for `VTag` and `VList` which
/// Parameter `previous_sibling` is necessary for `VTag` and `VList` which
/// has children and renders them.
fn apply(
&mut self,
parent: &Element,
_: Option<&Node>,
opposite: Option<VNode<Self::Component>>,
ancestor: Option<VNode<Self::Component>>,
_: &Scope<Self::Component>,
) -> Option<Node> {
assert!(
self.reference.is_none(),
"reference is ignored so must not be set"
);
let reform = {
match opposite {
match ancestor {
// If element matched this type
Some(VNode::VText(mut vtext)) => {
self.reference = vtext.reference.take();
Expand All @@ -82,12 +82,12 @@ impl<COMP: Component> VDiff for VText<COMP> {
};
match reform {
Reform::Keep => {}
Reform::Before(node) => {
Reform::Before(ancestor) => {
let element = document().create_text_node(&self.text);
if let Some(sibling) = node {
if let Some(ancestor) = ancestor {
parent
.insert_before(&element, &sibling)
.expect("can't insert text before sibling");
.insert_before(&element, &ancestor)
.expect("can't insert text before ancestor");
} else {
parent.append_child(&element);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/vtag_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ fn supports_svg() {
let svg_tag = assert_vtag(&mut svg_node);
svg_tag.apply(&div_el, None, None, &scope);
assert_namespace(svg_tag, SVG_NAMESPACE);
let path_tag = assert_vtag(svg_tag.childs.get_mut(0).unwrap());
let path_tag = assert_vtag(svg_tag.children.get_mut(0).unwrap());
assert_namespace(path_tag, SVG_NAMESPACE);

let g_tag = assert_vtag(&mut g_node);
Expand Down

0 comments on commit feebec8

Please sign in to comment.