You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins声明式管道:为Input文本域添加多行默认值失败

问题原因

多行字符串test2包含原始换行与缩进,直接拼接到HTML的<textarea>标签中时,会干扰浏览器的HTML语法解析(多余空白字符可能破坏标签闭合逻辑),且这类动态生成的HTML异常通常不会触发浏览器报错,导致调试困难。此外,未对插入内容做HTML转义,也可能引发潜在的结构解析问题。

修复方案

  1. 清理多行字符串的冗余空白:移除test2开头空行与每行前的公共缩进,避免输出无效空白内容。
  2. 对插入内容做HTML转义:使用Groovy内置方法转义特殊字符,防止破坏HTML标签结构。
  3. 确保分支逻辑匹配需求:根据选择项正确渲染对应内容的文本域。

修复后的代码示例

def test1 = '1234'
def test2 = '''
            1234
            5678
            91011
            '''
                
properties([
    parameters([
        [$class: 'DynamicReferenceParameter', 
            choiceType: 'ET_FORMATTED_HTML', 
            description: 'enter job params',
            name: 'jobArgs', 
            referencedParameters: 'lstSelection, test1, test2',
            script: 
                [$class: 'GroovyScript', 
                script: [
                        sandbox: true, 
                        script: """
                            def html
                            // 清理test2的冗余空白:移除公共缩进与首尾空行
                            def cleanedTest2 = test2.stripIndent().trim()
                            if (lstSelection == 'build'){
                               // 对单行内容做HTML转义后插入文本域
                               html = '<textarea name="value" rows="5" class="setting-input">' + test1.encodeAsHTML() + '</textarea>'
                            }else if (lstSelection == 'deploy'){
                               // 对清理后的多行内容做HTML转义后插入文本域
                               html = '<textarea name="value" rows="5" class="setting-input">' + cleanedTest2.encodeAsHTML() + '</textarea>'
                            }else{
                                html = '<input type="text" name="value" placeholder="{args}" >'
                            }
                            return html
                        """
                    ] 
            ],
        omitValueField: true
        ],
    ])
])

关键说明

  • stripIndent():Groovy原生方法,自动移除多行字符串中每行的公共缩进,保留有效换行。
  • trim():清除字符串首尾的空行与空白字符,避免文本域开头出现无效空行。
  • encodeAsHTML():将字符串中的特殊字符(如<、>、")转义为HTML实体,既避免破坏HTML结构,也能防止XSS风险。

内容的提问来源于stack exchange,提问作者mike01010

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 20:03:30