aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_utils/to_swc.rs
diff options
context:
space:
mode:
authorLibravatar Titus Wormer <tituswormer@gmail.com>2022-10-03 17:34:52 +0200
committerLibravatar Titus Wormer <tituswormer@gmail.com>2022-10-03 17:34:52 +0200
commit3a1fd1918f65c0a751a872d66e2d5bb01c64830c (patch)
treea10932d41f02b310b06dab8b344f6c4a35ff3ee6 /tests/test_utils/to_swc.rs
parent117cfc10c6d4a0a9346a29353860d1185d1ea224 (diff)
downloadmarkdown-rs-3a1fd1918f65c0a751a872d66e2d5bb01c64830c.tar.gz
markdown-rs-3a1fd1918f65c0a751a872d66e2d5bb01c64830c.tar.bz2
markdown-rs-3a1fd1918f65c0a751a872d66e2d5bb01c64830c.zip
Add support for creating a component, using layout
Diffstat (limited to 'tests/test_utils/to_swc.rs')
-rw-r--r--tests/test_utils/to_swc.rs53
1 files changed, 51 insertions, 2 deletions
diff --git a/tests/test_utils/to_swc.rs b/tests/test_utils/to_swc.rs
index 1d25f47..59d60ee 100644
--- a/tests/test_utils/to_swc.rs
+++ b/tests/test_utils/to_swc.rs
@@ -345,11 +345,44 @@ fn transform_root(
node: &hast::Node,
_root: &hast::Root,
) -> Result<Option<swc_ecma_ast::JSXElementChild>, String> {
- let children = all(context, node)?;
+ let mut children = all(context, node)?;
+ let mut queue = vec![];
+ let mut nodes = vec![];
+ let mut seen = false;
+
+ children.reverse();
+
+ // Remove initial/final whitespace.
+ while let Some(child) = children.pop() {
+ let mut stash = false;
+
+ if let swc_ecma_ast::JSXElementChild::JSXExprContainer(container) = &child {
+ if let swc_ecma_ast::JSXExpr::Expr(expr) = &container.expr {
+ if let swc_ecma_ast::Expr::Lit(swc_ecma_ast::Lit::Str(str)) = (*expr).as_ref() {
+ if inter_element_whitespace(str.value.as_ref()) {
+ stash = true;
+ }
+ }
+ }
+ }
+
+ if stash {
+ if seen {
+ queue.push(child);
+ }
+ } else {
+ if !queue.is_empty() {
+ nodes.append(&mut queue);
+ }
+ nodes.push(child);
+ seen = true;
+ }
+ }
+
// To do: remove whitespace?
// To do: return a single child if there is one?
Ok(Some(swc_ecma_ast::JSXElementChild::JSXFragment(
- create_fragment(children, node),
+ create_fragment(nodes, node),
)))
}
@@ -493,6 +526,22 @@ fn position_to_span(position: Option<&Position>) -> swc_common::Span {
})
}
+fn inter_element_whitespace(value: &str) -> bool {
+ let bytes = value.as_bytes();
+ let mut index = 0;
+
+ while index < bytes.len() {
+ match bytes[index] {
+ // To do: form feed.
+ b'\t' | b'\r' | b'\n' | b' ' => {}
+ _ => return false,
+ }
+ index += 1;
+ }
+
+ true
+}
+
/// Different kinds of JSX names.
enum JsxName<'a> {
// `a.b.c`