Skip to content

Commit 6da1954

Browse files
Collapse nested markup with surrounding text; leave nested <sup> alone (BL-16387)
Responds to review feedback on PR #8019: the collapse regex previously only removed markup doubled exactly at the boundary (<b><b>...</b></b>). It now also collapses cases with text inside the outer element but outside the inner one, e.g. <b>abc<b>def</b>ghi</b> -> <b>abcdefghi</b>, since the inner tag adds no formatting and can cause surprises when toggling the style. Also drop <sup> from the collapse set: nested superscript is semantically meaningful, so those are left as-is (attributes are still stripped from the inner tag). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 4ffb167 commit 6da1954

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

src/BloomExe/Book/Book.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,8 +2698,8 @@ public static void UpdateCharacterStyleMarkup(HtmlDom bookDOM)
26982698
// Only one level of nesting is handled, but that should (almost always) be enough.
26992699
inner = Regex.Replace(
27002700
inner,
2701-
@"<(strong|em|sup|u)><\1>(.*?)</\1></\1>",
2702-
"<$1>$2</$1>",
2701+
@"<(strong|em|u)>([^<>]*)<\1>([^<>]*)</\1>([^<>]*)</\1>",
2702+
"<$1>$2$3$4</$1>",
27032703
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase
27042704
);
27052705
// Remove empty (or essentially empty) character markup tags. (BL-16387)

src/BloomTests/Book/BookTests.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3183,12 +3183,12 @@ public void UpdateCharacterStyleMarkup_LeavesPlainTextUnchanged()
31833183
public void UpdateCharacterStyleMarkup_ReducesNestingOfSameTags()
31843184
{
31853185
var dom = new HtmlDom(
3186-
"<html><body><div class='bloom-editable'><p><b><b style='color:red'>text</b></b></p></div></body></html>"
3186+
"<html><body><div class='bloom-editable'><p><b>some <b style='color:red'>text</b> here</b></p></div></body></html>"
31873187
);
31883188
Bloom.Book.Book.UpdateCharacterStyleMarkup(dom);
31893189
Assert.That(
31903190
GetFirstEditableParagraph(dom).InnerXml,
3191-
Is.EqualTo("<strong>text</strong>")
3191+
Is.EqualTo("<strong>some text here</strong>")
31923192
);
31933193
var dom1 = new HtmlDom(
31943194
"<html><body><div class='bloom-editable'><p><i><i style='color:red'>text</i></i></p></div></body></html>"
@@ -3199,7 +3199,11 @@ public void UpdateCharacterStyleMarkup_ReducesNestingOfSameTags()
31993199
"<html><body><div class='bloom-editable'><p><sup><sup style='color:red'>text</sup></sup></p></div></body></html>"
32003200
);
32013201
Bloom.Book.Book.UpdateCharacterStyleMarkup(dom2);
3202-
Assert.That(GetFirstEditableParagraph(dom2).InnerXml, Is.EqualTo("<sup>text</sup>"));
3202+
// <sup> does have a nesting effect, so we don't collapse them. We just remove the attributes from the inner tag.
3203+
Assert.That(
3204+
GetFirstEditableParagraph(dom2).InnerXml,
3205+
Is.EqualTo("<sup><sup>text</sup></sup>")
3206+
);
32033207
var dom3 = new HtmlDom(
32043208
"<html><body><div class='bloom-editable'><p>This is <u><u style='color:red'>some text.</u></u></p></div></body></html>"
32053209
);

0 commit comments

Comments
 (0)