>>梦想乡<<

接口输出Diff格式的字符串到前端显示时,上下文无法显示

【问题描述】

最近在开发的时候遇到这么一个问题,后端将diff格式的字符串发送到前端,前端则将该字符串通过控件diff-to-html来显示。

如下所示:

diffContent = "diff --git a/ces测试123-2.0.txt b/ces测试123-2.0.txt\r\nindex 448ad29..bed094c 100644 (file)\r\n--- a/ces测试123-2.0.txt\r\n+++ b/ces测试123-2.0.txt\r\n@@ -101,7 +101,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n limitations under the License.\r\n \r\n 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:\r\n-\r\n+4. Redistribution. You may reproduce and distribute copies o\r\n \r\n Open Source Software Licensed Under the MIT License: \r\n ----------------------------------------------------------------------------------------\r"

  

将该字符串直接在前端写死,显示也是没有问题的。

但是,如果是在后端将该字符串通过接口的方式给前端,则上下文无法显示。

正常

 

 

不正常情况

 

 后端代码是通过拼接字符串,通过换行的话,是通过拼接   /r/n 来实现的,看起来貌似都没问题。

String contentText = "context"+"\r\n";

  

【解决方案】

在偶然的机会,发现StringUtils中有一个静态属性 LR,如下所示:

    public static final String SPACE = " ";
    public static final String EMPTY = "";
    public static final String LF = "\n";
    public static final String CR = "\r";

  走投无路,将\r\n以及空格全部都换了,打印至前台发现,居然有效....

posted @ 2021-10-20 11:01  文苏  阅读(160)  评论(0编辑  收藏  举报