zoukankan      html  css  js  c++  java
  • 接口输出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 = "
    ";
    

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




    ----------------------------------------------------------------------------
    我的小鱼你醒了,
    还认识早晨吗?
    昨夜你曾经说,
    愿夜幕永不开启。
    你的香腮边轻轻滑落的,
    是你的泪,还是我的泪?
    初吻吻别的那个季节,
    不是已经哭过了吗?
    我的指尖还记忆着,
    你慌乱的心跳。
    温柔的体香里,
    那一缕长发飘飘。
  • 相关阅读:
    PostgreSQL在Update时使用Substring函数截取字符串并且加上CASE WHEN THEN条件判断
    清理Visual Studio 2017的项目历史记录或手工修改Visual Studio 2017的注册表设置
    基于ABP模块组件与依赖注入组件的项目插件开发
    jenkins git can't work ERROR: Timeout after 10 minutes ERROR: Error fetching remote repo 'origin'
    SV randomize
    SV class
    SV coverage
    uvm设计分析——reg
    shell bash-shell
    scrapy的安装
  • 原文地址:https://www.cnblogs.com/liuyp-ken/p/15428013.html
Copyright © 2011-2022 走看看