Fix missing line in HTML output

This commit is contained in:
Fabrice Reix 2022-11-01 19:01:49 +01:00
parent c69ebea464
commit 2653401819
No known key found for this signature in database
GPG Key ID: BF5213154B2E7155
2 changed files with 50 additions and 34 deletions

View File

@ -0,0 +1,19 @@
<pre><code class="language-hurl"><span class="hurl-entry"><span class="request"><span class="line"></span><span class="comment"># Testing trailing whitespace</span>
<span class="line"></span><span class="comment"># Response '&lt;p&gt;Hello&lt;/p&gt;\n\n'</span>
<span class="line"></span>
<span class="line"></span><span class="comment"># Testing with a raw string</span>
<span class="line"></span><span class="comment"># check exactly the whitespace</span>
<span class="line"></span><span class="comment"># It should produce an assert error</span>
<span class="line"><span class="method">GET</span> <span class="url">http://localhost:8000/error-assert-newline</span></span>
</span><span class="response"><span class="line"><span class="version">HTTP/1.0</span> <span class="number">200</span></span>
<span class="raw"><span class="line">```&lt;p&gt;Hello&lt;/p&gt;</span>
<span class="line">```</span></span>
</span></span><span class="line"></span>
<span class="line"></span><span class="comment"># TODO</span>
<span class="line"></span><span class="comment"># Testing with XML</span>
<span class="line"></span><span class="comment"># Ignore trailing whitespace (semantic comparison)</span>
<span class="line"></span><span class="comment"># It should not produce an error</span>
<span class="line"></span><span class="comment"># GET http://localhost:8000/error-assert-newline</span>
<span class="line"></span><span class="comment"># HTTP/1.0 200</span>
<span class="line"></span><span class="comment"># &lt;p&gt;Hello&lt;/p&gt;</span>
</code></pre>

View File

@ -861,40 +861,11 @@ impl Htmlable for RawString {
fn to_html(&self) -> String { fn to_html(&self) -> String {
let mut buffer = "".to_string(); let mut buffer = "".to_string();
buffer.push_str("<span class=\"raw\">"); buffer.push_str("<span class=\"raw\">");
buffer.push_str("<span class=\"line\">```"); let mut s = "```".to_string();
s.push_str(self.newline.value.as_str());
if !self.newline.value.as_str().is_empty() { s.push_str(self.value.to_string().as_str());
buffer.push_str( s.push_str("```");
format!( buffer.push_str(multilines(s).as_str());
"</span>{}<span class=\"line\">",
self.newline.value.as_str()
)
.as_str(),
);
}
let mut lines: Vec<String> = regex::Regex::new(r"\n|\r\n")
.unwrap()
.split(self.value.to_string().trim())
.map(|l| xml_escape(l.to_string()))
.filter(|l| !l.is_empty())
.collect();
if lines.is_empty() {
buffer.push_str("```</span>");
} else if lines.len() == 1 {
buffer.push_str(encode_html(lines.get(0).unwrap().to_string()).as_str());
buffer.push_str("```</span>");
} else {
buffer.push_str(encode_html(lines.remove(0)).as_str());
buffer.push_str("</span>\n");
for line in lines {
buffer.push_str("<span class=\"line\">");
buffer.push_str(encode_html(line).as_str());
buffer.push_str("</span>\n");
}
buffer.push_str("<span class=\"line\">```</span>");
}
buffer.push_str("</span>"); buffer.push_str("</span>");
buffer buffer
} }
@ -1167,6 +1138,27 @@ mod tests {
raw_string.to_html(), raw_string.to_html(),
"<span class=\"raw\"><span class=\"line\">```</span>\n<span class=\"line\">line1</span>\n<span class=\"line\">line2</span>\n<span class=\"line\">```</span></span>".to_string() "<span class=\"raw\"><span class=\"line\">```</span>\n<span class=\"line\">line1</span>\n<span class=\"line\">line2</span>\n<span class=\"line\">```</span></span>".to_string()
); );
// ```Hello
// ```
let raw_string = RawString {
newline: Whitespace {
value: "".to_string(),
source_info: SourceInfo::new(0, 0, 0, 0),
},
value: Template {
quotes: false,
elements: vec![TemplateElement::String {
value: "Hello\n".to_string(),
encoded: "unused".to_string(),
}],
source_info: SourceInfo::new(0, 0, 0, 0),
},
};
assert_eq!(
raw_string.to_html(),
"<span class=\"raw\"><span class=\"line\">```Hello</span>\n<span class=\"line\">```</span></span>".to_string()
);
} }
#[test] #[test]
@ -1179,6 +1171,11 @@ mod tests {
multilines("<?xml version=\"1.0\"?>\n<drink>café</drink>".to_string()), multilines("<?xml version=\"1.0\"?>\n<drink>café</drink>".to_string()),
"<span class=\"line\">&lt;?xml version=\"1.0\"?&gt;</span>\n<span class=\"line\">&lt;drink&gt;café&lt;/drink&gt;</span>" "<span class=\"line\">&lt;?xml version=\"1.0\"?&gt;</span>\n<span class=\"line\">&lt;drink&gt;café&lt;/drink&gt;</span>"
); );
assert_eq!(
multilines("Hello\n".to_string()),
"<span class=\"line\">Hello</span>\n<span class=\"line\"></span>"
);
} }
#[test] #[test]