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

【问题描述】

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

如下所示:

diffContent = "diff --git a/ces测试123-2.0.txt b/ces测试123-2.0.txt
index 448ad29..bed094c 100644 (file)
--- a/ces测试123-2.0.txt
+++ b/ces测试123-2.0.txt
@@ -101,7 +101,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 limitations under the License.
 
 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:
-
+4. Redistribution. You may reproduce and distribute copies o
 
 Open Source Software Licensed Under the MIT License: 
 ----------------------------------------------------------------------------------------
"

  

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

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

正常

 

不正常情况

 

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

String contentText = "context"+"
";

  

【解决方案】

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

    public static final String SPACE = " ";
    public static final String EMPTY = "";
    public static final String LF = "
";
    public static final String CR = "
";

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




----------------------------------------------------------------------------
我的小鱼你醒了,
还认识早晨吗?
昨夜你曾经说,
愿夜幕永不开启。
你的香腮边轻轻滑落的,
是你的泪,还是我的泪?
初吻吻别的那个季节,
不是已经哭过了吗?
我的指尖还记忆着,
你慌乱的心跳。
温柔的体香里,
那一缕长发飘飘。
原文地址:https://www.cnblogs.com/liuyp-ken/p/15428013.html