|
1 | 1 | use super::tag::{Element, ElementKind, Node}; |
2 | 2 |
|
3 | | -pub struct ChildrenIter<'a> { |
4 | | - parent: &'a Element, |
5 | | - cur: usize, |
6 | | - cur_branch: usize, |
7 | | -} |
8 | | - |
9 | | -impl<'a> ChildrenIter<'a> { |
10 | | - pub(super) fn new(parent: &'a Element) -> Self { |
11 | | - Self { |
12 | | - parent, |
13 | | - cur: 0, |
14 | | - cur_branch: 0, |
| 3 | +macro_rules! iter { |
| 4 | + ($t:ident, $($mut_t:tt)*) => { |
| 5 | + pub struct $t<'a> { |
| 6 | + parent: &'a $($mut_t)* Element, |
| 7 | + cur: usize, |
| 8 | + cur_branch: usize, |
15 | 9 | } |
16 | | - } |
17 | | -} |
18 | | - |
19 | | -impl<'a> Iterator for ChildrenIter<'a> { |
20 | | - type Item = &'a Node; |
21 | | - |
22 | | - fn next(&mut self) -> Option<Self::Item> { |
23 | | - match &self.parent.kind { |
24 | | - ElementKind::Normal { children, .. } |
25 | | - | ElementKind::Pure { children, .. } |
26 | | - | ElementKind::For { children, .. } => { |
27 | | - let ret = children.get(self.cur); |
28 | | - if ret.is_some() { |
29 | | - self.cur += 1; |
| 10 | + |
| 11 | + impl<'a> $t<'a> { |
| 12 | + pub(super) fn new(parent: &'a $($mut_t)* Element) -> Self { |
| 13 | + Self { |
| 14 | + parent, |
| 15 | + cur: 0, |
| 16 | + cur_branch: 0, |
30 | 17 | } |
31 | | - ret |
32 | 18 | } |
33 | | - ElementKind::If { |
34 | | - branches, |
35 | | - else_branch, |
36 | | - } => { |
37 | | - let ret = loop { |
38 | | - if self.cur_branch >= branches.len() { |
39 | | - if let Some((_, children)) = else_branch { |
40 | | - let ret = children.get(self.cur); |
41 | | - if ret.is_some() { |
| 19 | + } |
| 20 | + |
| 21 | + impl<'a> Iterator for $t<'a> { |
| 22 | + type Item = &'a $($mut_t)* Node; |
| 23 | + |
| 24 | + fn next(&mut self) -> Option<Self::Item> { |
| 25 | + match & $($mut_t)* self.parent.kind { |
| 26 | + ElementKind::Normal { children, .. } |
| 27 | + | ElementKind::Pure { children, .. } |
| 28 | + | ElementKind::For { children, .. } => { |
| 29 | + let ret = children.get(self.cur).map(|x| x as *const _); |
| 30 | + if let Some(ret) = ret { |
| 31 | + self.cur += 1; |
| 32 | + // it is safe because the iterator never returns an item twice |
| 33 | + Some(unsafe { & $($mut_t)* *(ret as *mut _) }) |
| 34 | + } else { |
| 35 | + None |
| 36 | + } |
| 37 | + } |
| 38 | + ElementKind::If { |
| 39 | + branches, |
| 40 | + else_branch, |
| 41 | + } => { |
| 42 | + let ret = loop { |
| 43 | + if self.cur_branch >= branches.len() { |
| 44 | + if let Some((_, children)) = else_branch { |
| 45 | + let ret = children.get(self.cur).map(|x| x as *const _); |
| 46 | + if ret.is_some() { |
| 47 | + self.cur += 1; |
| 48 | + } |
| 49 | + break ret; |
| 50 | + } |
| 51 | + break None; |
| 52 | + } |
| 53 | + let (_, _, children) = unsafe { branches.get_unchecked(self.cur_branch) }; |
| 54 | + let ret = children.get(self.cur).map(|x| x as *const _); |
| 55 | + if let Some(ret) = ret { |
42 | 56 | self.cur += 1; |
| 57 | + break Some(ret); |
43 | 58 | } |
44 | | - break ret; |
45 | | - } |
46 | | - break None; |
| 59 | + self.cur_branch += 1; |
| 60 | + self.cur = 0; |
| 61 | + }; |
| 62 | + // it is safe because the iterator never returns an item twice |
| 63 | + ret.map(|ret| unsafe { & $($mut_t)* *(ret as *mut _) }) |
47 | 64 | } |
48 | | - let (_, _, children) = unsafe { branches.get_unchecked(self.cur_branch) }; |
49 | | - let ret = children.get(self.cur); |
50 | | - if ret.is_some() { |
51 | | - self.cur += 1; |
52 | | - break ret; |
| 65 | + ElementKind::TemplateRef { .. } |
| 66 | + | ElementKind::Include { .. } |
| 67 | + | ElementKind::Slot { .. } => None, |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 72 | + let count = match &self.parent.kind { |
| 73 | + ElementKind::Normal { children, .. } |
| 74 | + | ElementKind::Pure { children, .. } |
| 75 | + | ElementKind::For { children, .. } => children.len(), |
| 76 | + ElementKind::If { |
| 77 | + branches, |
| 78 | + else_branch, |
| 79 | + } => { |
| 80 | + branches.iter().map(|x| x.2.len()).sum::<usize>() |
| 81 | + + else_branch.iter().map(|x| x.1.len()).sum::<usize>() |
53 | 82 | } |
54 | | - self.cur_branch += 1; |
55 | | - self.cur = 0; |
| 83 | + ElementKind::TemplateRef { .. } |
| 84 | + | ElementKind::Include { .. } |
| 85 | + | ElementKind::Slot { .. } => 0, |
56 | 86 | }; |
57 | | - ret |
| 87 | + (count, Some(count)) |
58 | 88 | } |
59 | | - ElementKind::TemplateRef { .. } |
60 | | - | ElementKind::Include { .. } |
61 | | - | ElementKind::Slot { .. } => None, |
62 | 89 | } |
63 | | - } |
| 90 | + }; |
| 91 | +} |
| 92 | + |
| 93 | +iter!(ChildrenIter,); |
| 94 | +iter!(ChildrenIterMut, mut); |
| 95 | + |
| 96 | +#[cfg(test)] |
| 97 | +mod test { |
| 98 | + use crate::parse::tag::{Node, Value}; |
64 | 99 |
|
65 | | - fn size_hint(&self) -> (usize, Option<usize>) { |
66 | | - let count = match &self.parent.kind { |
67 | | - ElementKind::Normal { children, .. } |
68 | | - | ElementKind::Pure { children, .. } |
69 | | - | ElementKind::For { children, .. } => children.len(), |
70 | | - ElementKind::If { |
71 | | - branches, |
72 | | - else_branch, |
73 | | - } => { |
74 | | - branches.iter().map(|x| x.2.len()).sum::<usize>() |
75 | | - + else_branch.iter().map(|x| x.1.len()).sum::<usize>() |
| 100 | + #[test] |
| 101 | + fn iter_children() { |
| 102 | + const SRC: &'static str = r#" |
| 103 | + <div> |
| 104 | + <div>A</div> |
| 105 | + <block wx:for="123">B</block> |
| 106 | + <block wx:if="1">C</block> |
| 107 | + <block wx:elif="2">D</block> |
| 108 | + <block wx:elif="3">E</block> |
| 109 | + <block>F</block> |
| 110 | + </div> |
| 111 | + "#; |
| 112 | + let (mut template, _) = crate::parse::parse("TEST", SRC); |
| 113 | + fn rec(node: &mut Node, visited: &mut String) { |
| 114 | + if let Node::Text(v) = node { |
| 115 | + let Value::Static { value, .. } = v else { unreachable!() }; |
| 116 | + visited.push_str(&value); |
| 117 | + *value = "".into(); |
76 | 118 | } |
77 | | - ElementKind::TemplateRef { .. } |
78 | | - | ElementKind::Include { .. } |
79 | | - | ElementKind::Slot { .. } => 0, |
80 | | - }; |
81 | | - (count, Some(count)) |
| 119 | + if let Node::Element(elem) = node { |
| 120 | + for child in elem.iter_children_mut() { |
| 121 | + rec(child, visited); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + let mut visited = String::new(); |
| 126 | + for node in &mut template.content { |
| 127 | + rec(node, &mut visited); |
| 128 | + } |
| 129 | + assert_eq!(visited, "ABCDEF"); |
| 130 | + let mut visited = String::new(); |
| 131 | + for node in &mut template.content { |
| 132 | + rec(node, &mut visited); |
| 133 | + } |
| 134 | + assert_eq!(visited, ""); |
82 | 135 | } |
83 | 136 | } |
0 commit comments