flutter_html icon indicating copy to clipboard operation
flutter_html copied to clipboard

[BUG]Underlines not Shown in <u>&nbsp;</u>

Open WuzhouDu opened this issue 2 years ago • 3 comments

Describe the bug:

The &nbsp; in the <u></u> tag cannot be seen properly.

  1. HTML passed to the data of Html widget: '<u>&nbsp; &nbsp; &nbsp;</u>' Nothing will show.

  2. HTML passed to the data of Html widget: '<u>test &nbsp; &nbsp; &nbsp;</u>' image

  3. HTML passed to the data of Html widget: 'test<u>&nbsp; &nbsp; &nbsp;</u>' image

  4. HTML passed to the data of Html widget: 'test<u>&nbsp; &nbsp; &nbsp;</u>test' image

HTML to reproduce the issue:

As stated above.

Html widget configuration:

class MyHtmlText extends StatelessWidget {
  String text;
  final TextStyle textStyle;

  MyHtmlText({
    required this.text,
    required this.textStyle,
  });

  /// replace all ' ' and '&nbsp;' with '_'
  String replaceUnderlines(String htmlText) {
    return htmlText.replaceAllMapped(RegExp(r'<u>(?:&nbsp;|\s)*<\/u>'),
        (match) {
      String content = match.group(0)!; // 获取<u>标签内部的内容(&nbsp; 和空格)
      int underscoreCount =
          content.trim().replaceAll('&nbsp;', ' ').length; // 计算下划线的数量
      return '_' * underscoreCount; // 返回相应数量的下划线
    });
  }

  @override
  Widget build(BuildContext context) {
    // text = replaceUnderlines(text);
    print(text);
    return Container(
      padding: EdgeInsets.all(0),
      child: Html(
        data: text,
        style: {
          "text": Style(),
          "body": Style(
            margin: Margins.all(0),
            padding: HtmlPaddings.all(0),
            fontSize: FontSize(textStyle.fontSize!),
            lineHeight: LineHeight(textStyle.height),
          ),
          "p": Style(
            fontSize: FontSize(textStyle.fontSize!),
            lineHeight: LineHeight(textStyle.height),
          ),
          "h5": Style(
            fontSize: FontSize(textStyle.fontSize! - 4.sp),
            fontWeight: FontWeight.bold,
            lineHeight: LineHeight(textStyle.height),
          ),
          "h4": Style(
            fontSize: FontSize(textStyle.fontSize!),
            fontWeight: FontWeight.bold,
            lineHeight: LineHeight(textStyle.height),
          ),
          "h3": Style(
            fontSize: FontSize(textStyle.fontSize! + 2.sp),
            fontWeight: FontWeight.bold,
            lineHeight: LineHeight(textStyle.height),
          ),
          "h2": Style(
            fontSize: FontSize(textStyle.fontSize! + 8.sp),
            fontWeight: FontWeight.bold,
            lineHeight: LineHeight(textStyle.height),
          ),
          "h1": Style(
            fontSize: FontSize(textStyle.fontSize! + 16.sp),
            fontWeight: FontWeight.bold,
            lineHeight: LineHeight(textStyle.height),
          ),
        },
      ),
    );
  }
}

Expected behavior:

The &nbsp; in the <u> tag can be shown properly as the case in the browser.

Device details and Flutter/Dart/flutter_html versions:

Flutter 3.7.12 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 4d9e56e694 (4 months ago) • 2023-04-17 21:47:46 -0400
Engine • revision 1a65d409c7
Tools • Dart 2.19.6 • DevTools 2.20.1

Additional info:

I temporarily use Regular Expression to handle the raw string contents of this issue.

  /// replace all ' ' and '&nbsp;' with '_'
  String replaceUnderlines(String htmlText) {
    return htmlText.replaceAllMapped(RegExp(r'<u>(?:&nbsp;|\s)*<\/u>'),
        (match) {
      String content = match.group(0)!; // get the contents in tag <u>(&nbsp; and white space)
      int underscoreCount =
          content.trim().replaceAll('&nbsp;', ' ').length; // calculate the number of underscore
      return '_' * underscoreCount;
    });
  }

A picture of a cute animal (not mandatory but encouraged)

image

WuzhouDu avatar Aug 23 '23 07:08 WuzhouDu

Then wrap them with  

&nbsp;<u>&nbsp; &nbsp; &nbsp;</u>&nbsp;

thanhle7 avatar Aug 26 '23 19:08 thanhle7

Then wrap them with  

&nbsp;<u>&nbsp; &nbsp; &nbsp;</u>&nbsp;

Yeah, it works. But maybe it's not reasonable for the users of this lib to consider this.

WuzhouDu avatar Aug 28 '23 03:08 WuzhouDu