Skip to content

Commit

Permalink
fix(pkgs): Try to fix potential style escape in block quote
Browse files Browse the repository at this point in the history
Try to fix potential style escape in block quote:
* Some styles in block quote affects contents outside block quote when
  unexpectedlly truncated and lost the closed tag. It is not possible to
  fix this after content rendered into html because we can not tell a
  style covers all following contents in the post is user added or the
  issue above.
* This commit only add save-and-restore process around parsing block
  quote node to avoid potential similar issues.
  • Loading branch information
realth000 committed Dec 11, 2023
1 parent 34afb1a commit b33ce44
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/packages/html_muncher/lib/src/html_muncher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,35 @@ class MunchState {
final fontSizeStack = <double>[];
String? tapUrl;

/// An internal field to save field current values.
MunchState? _reservedState;

/// Save current state [_reservedState].
void save() {
_reservedState = this;
}

/// Restore state from [_reservedState].
void restore() {
if (_reservedState != null) {
return;
}
bold = _reservedState!.bold;
underline = _reservedState!.underline;
lineThrough = _reservedState!.lineThrough;
center = _reservedState!.center;
textAlign = _reservedState!.textAlign;
colorStack
..clear()
..addAll(_reservedState!.colorStack);
fontSizeStack
..clear()
..addAll(_reservedState!.fontSizeStack);
tapUrl = _reservedState!.tapUrl;

_reservedState = null;
}

@override
String toString() {
return 'MunchState {bold=$bold, underline=$underline, lineThrough=$lineThrough, color=$colorStack}';
Expand Down Expand Up @@ -290,7 +319,16 @@ class Muncher {
}

InlineSpan _buildBlockQuote(uh.Element element) {
// Try isolate the munch state inside quoted message.
// Bug is that when the original quoted message "truncated" at unclosed tags like "foo[s]bar...", the unclosed tag
// will affect all following contents in current post, that is, all texts are marked with line through.
// This is unfixable after rendered into html because we do not know whether a whole decoration tag (e.g. <strike>)
// contains the all following post messages is user added or caused by the bug above.
// Here just try to save and restore munch state to avoid potential issued about "styles inside quoted blocks
// affects outside main content".
state.save();
final ret = _munch(element);
state.restore();
return TextSpan(children: [
WidgetSpan(
child: Card(
Expand Down

0 comments on commit b33ce44

Please sign in to comment.