mirror of
https://github.com/storytold/designcraft.git
synced 2026-11-16 07:12:43 +00:00
Skip quoted attribute values when finding the end of an xref tag
Found while testing crates/doc/src/xref.rs:85-91 (expand) Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ea08e4c2ba
commit
3fec4d32d8
1 file changed
+28
-1
+28
-1
@@ -84,7 +84,7 @@ pub fn expand(definition: &str, v: &XrefValues) -> String {
|
||||
let mut rest = definition;
|
||||
while let Some(i) = rest.find('<') {
|
||||
out.push_str(&rest[..i]);
|
||||
let Some(j) = rest[i..].find('>') else {
|
||||
let Some(j) = tag_end(&rest[i..]) else {
|
||||
out.push_str(&rest[i..]);
|
||||
return out;
|
||||
};
|
||||
@@ -120,6 +120,20 @@ pub fn expand(definition: &str, v: &XrefValues) -> String {
|
||||
typographic_quotes(&out)
|
||||
}
|
||||
|
||||
/// Byte index of the `>` that ends the tag starting at `s`, skipping quoted attribute values.
|
||||
/// An unterminated quote falls back to the first `>`, so only that malformed tag is affected.
|
||||
fn tag_end(s: &str) -> Option<usize> {
|
||||
let mut in_quote = false;
|
||||
for (k, c) in s.char_indices() {
|
||||
match c {
|
||||
'"' => in_quote = !in_quote,
|
||||
'>' if !in_quote => return Some(k),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
s.find('>')
|
||||
}
|
||||
|
||||
/// Straight double quotes in format text become typographer's quotes.
|
||||
fn typographic_quotes(s: &str) -> String {
|
||||
let mut out = String::with_capacity(s.len());
|
||||
@@ -388,6 +402,19 @@ pub fn clean(s: &str) -> String {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn gt_inside_quoted_attribute_does_not_end_tag() {
|
||||
let v = XrefValues { para: "Intro > the start".into(), ..Default::default() };
|
||||
assert_eq!(expand("<partialPara delim=\">\" includeDelim=\"false\" />|", &v), "Intro |");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unterminated_quote_only_affects_its_own_tag() {
|
||||
let v = XrefValues { para: "Intro > the start".into(), ..Default::default() };
|
||||
let out = expand("<x a=\"b> <fullPara />", &v);
|
||||
assert!(out.contains("Intro > the start"), "{out}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn formats_expand() {
|
||||
let v =
|
||||
|
||||
Reference in new issue
Block a user