Skip to content

Commit 0f5fd6a

Browse files
committed
feat: 插件BPMNAdapter支持特殊字符串转义
1 parent edbe193 commit 0f5fd6a

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

packages/extension/src/bpmn-elements-adapter/json2xml.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ function handleAttributes(obj: any): any {
2424
return obj
2525
}
2626

27+
function escapeXml(text: string) {
28+
if (text == null) return ''
29+
return text
30+
.toString()
31+
.replace(/&/g, '&')
32+
.replace(/</g, '&lt;')
33+
.replace(/>/g, '&gt;')
34+
.replace(/"/g, '&quot;')
35+
.replace(/'/g, '&apos;')
36+
}
37+
2738
function getAttributes(obj: any) {
2839
let tmp = obj
2940
try {
@@ -33,7 +44,7 @@ function getAttributes(obj: any) {
3344
} catch (error) {
3445
tmp = JSON.stringify(handleAttributes(obj)).replace(/"/g, "'")
3546
}
36-
return tmp
47+
return escapeXml(String(tmp))
3748
}
3849

3950
const tn = '\t\n'
@@ -44,8 +55,11 @@ function toXml(obj: any, name: string, depth: number) {
4455
let str = ''
4556
const prefix = tn + frontSpace
4657
if (name === '-json') return ''
58+
if (obj !== 0 && obj !== false && !obj) {
59+
return `${prefix}<${name} />`
60+
}
4761
if (name === '#text') {
48-
return prefix + obj
62+
return prefix + escapeXml(String(obj))
4963
}
5064
if (name === '#cdata-section') {
5165
return `${prefix}<![CDATA[${obj}]]>`
@@ -74,7 +88,7 @@ function toXml(obj: any, name: string, depth: number) {
7488
str +=
7589
attributes + (children !== '' ? `>${children}${prefix}</${name}>` : ' />')
7690
} else {
77-
str += `${prefix}<${name}>${obj.toString()}</${name}>`
91+
str += `${prefix}<${name}>${escapeXml(String(obj))}</${name}>`
7892
}
7993

8094
return str
@@ -88,4 +102,4 @@ function lfJson2Xml(obj: any) {
88102
return xmlStr
89103
}
90104

91-
export { lfJson2Xml, handleAttributes }
105+
export { lfJson2Xml, handleAttributes, escapeXml }

packages/extension/src/bpmn-elements-adapter/xml2json.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// @ts-nocheck
77

88
import { has } from 'lodash-es'
9+
import { escapeXml } from './json2xml'
910
// ========================================================================
1011
// XML.ObjTree -- XML source code from/to JavaScript object like E4X
1112
// ========================================================================
@@ -287,14 +288,7 @@ XML.ObjTree.prototype.scalar_to_xml = function (name, text) {
287288
}
288289

289290
// method: xml_escape( text )
290-
291-
XML.ObjTree.prototype.xml_escape = function (text) {
292-
return text
293-
.replace(/&/g, '&')
294-
.replace(/</g, '<')
295-
.replace(/>/g, '>')
296-
.replace(/"/g, '"')
297-
}
291+
XML.ObjTree.prototype.xml_escape = escapeXml
298292

299293
/*
300294
// ========================================================================

0 commit comments

Comments
 (0)