Skip to content

Commit

Permalink
Fix: RawText to_source implementation on stable
Browse files Browse the repository at this point in the history
  • Loading branch information
vldm committed May 13, 2023
1 parent 6296d78 commit ded41ca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
16 changes: 12 additions & 4 deletions examples/html-to-string-macro/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ pub mod docs {
}
#[test]
fn test() {

let nightly_unqoted = " Hello world with spaces ";
let stable_unqoted = "Hello world with spaces";
let unquoted_text = if cfg!(feature = "nightly") {
nightly_unqoted
} else {
stable_unqoted
};
let world = "planet";
assert_eq!(
html! {
Expand All @@ -22,14 +30,14 @@ fn test() {
<div hello={world} />
<>
<div>"1"</div>
<div>"2"</div>
<div> Hello world with spaces </div>
<div>"3"</div>
<div {"some-attribute-from-rust-block"}/>
</>
</body>
</html>
},
r#"
format!(r#"
<!DOCTYPE html>
<html>
<head>
Expand All @@ -39,12 +47,12 @@ fn test() {
<!-- comment -->
<div hello="planet"/>
<div>1</div>
<div>2</div>
<div>{}</div>
<div>3</div>
<div some-attribute-from-rust-block/>
</body>
</html>
"#
"#, unquoted_text)
.split('\n')
.map(|line| line.trim())
.collect::<Vec<&str>>()
Expand Down
18 changes: 17 additions & 1 deletion src/node/raw_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,25 @@ impl RawText {
debug_assert!(full_text.starts_with(&start_text));
Some(full_text[start_text.len()..(full_text.len() - end_text.len())].to_string())
} else {
self.token_stream.span().source_text()
self.join_spans()?.source_text()
}
}

/// Return Spans for all unquoted text or nothing.
/// Usefull to detect is `Span::join` is available or not.
pub fn join_spans(&self) -> Option<Span> {
let mut span:Option<Span> = None;
for tt in self.token_stream.clone().into_iter() {
let joined = if let Some(span) = span {
span.join(tt.span())?
} else {
tt.span()
};
span = Some(joined);
}
return span
}

pub fn is_empty(&self) -> bool {
self.token_stream.is_empty()
}
Expand Down

0 comments on commit ded41ca

Please sign in to comment.