@logicflow/extension 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/README.md +16 -0
  2. package/dist/index.css +1 -1
  3. package/dist/index.min.js +1 -1
  4. package/dist/index.min.js.map +1 -1
  5. package/es/NodeResize/node/RectResize.d.ts +0 -6
  6. package/es/NodeResize/node/RectResize.js +2 -11
  7. package/es/bpmn-elements-adapter/json2xml.d.ts +2 -1
  8. package/es/bpmn-elements-adapter/json2xml.js +18 -4
  9. package/es/bpmn-elements-adapter/xml2json.js +2 -7
  10. package/es/components/control/index.d.ts +1 -0
  11. package/es/components/control/index.js +24 -11
  12. package/es/components/mini-map/index.js +1 -1
  13. package/es/dynamic-group/model.d.ts +2 -1
  14. package/es/dynamic-group/model.js +28 -11
  15. package/es/dynamic-group/node.js +1 -0
  16. package/es/index.css +1 -1
  17. package/es/index.d.ts +1 -0
  18. package/es/index.js +2 -0
  19. package/es/insert-node-in-polyline/index.js +11 -35
  20. package/es/materials/curved-edge/index.js +49 -21
  21. package/es/materials/group/GroupNode.d.ts +0 -6
  22. package/es/materials/group/GroupNode.js +1 -6
  23. package/es/pool/LaneModel.d.ts +90 -0
  24. package/es/pool/LaneModel.js +252 -0
  25. package/es/pool/LaneView.d.ts +40 -0
  26. package/es/pool/LaneView.js +202 -0
  27. package/es/pool/PoolModel.d.ts +120 -0
  28. package/es/pool/PoolModel.js +586 -0
  29. package/es/pool/PoolView.d.ts +17 -0
  30. package/es/pool/PoolView.js +76 -0
  31. package/es/pool/constant.d.ts +15 -0
  32. package/es/pool/constant.js +17 -0
  33. package/es/pool/index.d.ts +89 -0
  34. package/es/pool/index.js +524 -0
  35. package/es/pool/utils.d.ts +19 -0
  36. package/es/pool/utils.js +33 -0
  37. package/es/style/index.css +1 -1
  38. package/es/style/raw.d.ts +1 -1
  39. package/es/style/raw.js +1 -1
  40. package/es/tools/label/LabelOverlay.js +3 -3
  41. package/es/tools/label/mediumEditor.d.ts +1 -1
  42. package/es/tools/label/mediumEditor.js +89 -52
  43. package/es/tools/snapshot/index.d.ts +7 -3
  44. package/es/tools/snapshot/index.js +72 -77
  45. package/lib/NodeResize/node/RectResize.d.ts +0 -6
  46. package/lib/NodeResize/node/RectResize.js +1 -10
  47. package/lib/bpmn-elements-adapter/json2xml.d.ts +2 -1
  48. package/lib/bpmn-elements-adapter/json2xml.js +19 -4
  49. package/lib/bpmn-elements-adapter/xml2json.js +2 -7
  50. package/lib/components/control/index.d.ts +1 -0
  51. package/lib/components/control/index.js +24 -11
  52. package/lib/components/mini-map/index.js +1 -1
  53. package/lib/dynamic-group/model.d.ts +2 -1
  54. package/lib/dynamic-group/model.js +28 -11
  55. package/lib/dynamic-group/node.js +1 -0
  56. package/lib/index.css +1 -1
  57. package/lib/index.d.ts +1 -0
  58. package/lib/index.js +2 -0
  59. package/lib/insert-node-in-polyline/index.js +10 -34
  60. package/lib/materials/curved-edge/index.js +49 -21
  61. package/lib/materials/group/GroupNode.d.ts +0 -6
  62. package/lib/materials/group/GroupNode.js +1 -6
  63. package/lib/pool/LaneModel.d.ts +90 -0
  64. package/lib/pool/LaneModel.js +255 -0
  65. package/lib/pool/LaneView.d.ts +40 -0
  66. package/lib/pool/LaneView.js +205 -0
  67. package/lib/pool/PoolModel.d.ts +120 -0
  68. package/lib/pool/PoolModel.js +589 -0
  69. package/lib/pool/PoolView.d.ts +17 -0
  70. package/lib/pool/PoolView.js +79 -0
  71. package/lib/pool/constant.d.ts +15 -0
  72. package/lib/pool/constant.js +20 -0
  73. package/lib/pool/index.d.ts +89 -0
  74. package/lib/pool/index.js +527 -0
  75. package/lib/pool/utils.d.ts +19 -0
  76. package/lib/pool/utils.js +38 -0
  77. package/lib/style/index.css +1 -1
  78. package/lib/style/raw.d.ts +1 -1
  79. package/lib/style/raw.js +1 -1
  80. package/lib/tools/label/LabelOverlay.js +2 -2
  81. package/lib/tools/label/mediumEditor.d.ts +1 -1
  82. package/lib/tools/label/mediumEditor.js +91 -53
  83. package/lib/tools/snapshot/index.d.ts +7 -3
  84. package/lib/tools/snapshot/index.js +72 -77
  85. package/package.json +10 -7
  86. package/.turbo/turbo-build.log +0 -38
  87. package/CHANGELOG.md +0 -1766
  88. package/__test__/bpmn-adapter.test.js +0 -227
  89. package/es/materials/curved-edge/__test__/curved-edge.test.d.ts +0 -1
  90. package/es/materials/curved-edge/__test__/curved-edge.test.js +0 -18
  91. package/jest.config.js +0 -198
  92. package/lib/materials/curved-edge/__test__/curved-edge.test.d.ts +0 -1
  93. package/lib/materials/curved-edge/__test__/curved-edge.test.js +0 -20
  94. package/rollup.config.js +0 -52
  95. package/src/NodeResize/BasicShape/Ellipse.tsx +0 -22
  96. package/src/NodeResize/BasicShape/Polygon.tsx +0 -24
  97. package/src/NodeResize/BasicShape/Rect.tsx +0 -44
  98. package/src/NodeResize/control/Control.tsx +0 -537
  99. package/src/NodeResize/control/ControlGroup.tsx +0 -76
  100. package/src/NodeResize/control/Util.ts +0 -206
  101. package/src/NodeResize/index.ts +0 -26
  102. package/src/NodeResize/node/DiamondResize.tsx +0 -149
  103. package/src/NodeResize/node/EllipseResize.tsx +0 -140
  104. package/src/NodeResize/node/HtmlResize.tsx +0 -125
  105. package/src/NodeResize/node/RectResize.tsx +0 -146
  106. package/src/NodeResize/node/index.ts +0 -4
  107. package/src/bpmn/constant.ts +0 -56
  108. package/src/bpmn/events/EndEvent.ts +0 -73
  109. package/src/bpmn/events/StartEvent.ts +0 -52
  110. package/src/bpmn/events/index.ts +0 -2
  111. package/src/bpmn/flow/SequenceFlow.ts +0 -25
  112. package/src/bpmn/flow/index.ts +0 -1
  113. package/src/bpmn/gateways/ExclusiveGateway.ts +0 -71
  114. package/src/bpmn/gateways/index.ts +0 -1
  115. package/src/bpmn/getBpmnId.ts +0 -31
  116. package/src/bpmn/index.ts +0 -60
  117. package/src/bpmn/tasks/ServiceTask.ts +0 -63
  118. package/src/bpmn/tasks/UserTask.ts +0 -64
  119. package/src/bpmn/tasks/index.ts +0 -2
  120. package/src/bpmn-adapter/bpmnIds.ts +0 -31
  121. package/src/bpmn-adapter/index.ts +0 -835
  122. package/src/bpmn-adapter/json2xml.ts +0 -127
  123. package/src/bpmn-adapter/xml2json.ts +0 -544
  124. package/src/bpmn-elements/README.md +0 -223
  125. package/src/bpmn-elements/__tests__/definition.test.js +0 -72
  126. package/src/bpmn-elements/index.d.ts +0 -26
  127. package/src/bpmn-elements/index.ts +0 -107
  128. package/src/bpmn-elements/presets/Event/EndEventFactory.ts +0 -114
  129. package/src/bpmn-elements/presets/Event/IntermediateCatchEvent.ts +0 -108
  130. package/src/bpmn-elements/presets/Event/IntermediateThrowEvent.ts +0 -109
  131. package/src/bpmn-elements/presets/Event/StartEventFactory.ts +0 -114
  132. package/src/bpmn-elements/presets/Event/boundaryEventFactory.ts +0 -117
  133. package/src/bpmn-elements/presets/Event/index.ts +0 -14
  134. package/src/bpmn-elements/presets/Flow/flow.d.ts +0 -6
  135. package/src/bpmn-elements/presets/Flow/index.ts +0 -8
  136. package/src/bpmn-elements/presets/Flow/manhattan.ts +0 -691
  137. package/src/bpmn-elements/presets/Flow/sequenceFlow.ts +0 -65
  138. package/src/bpmn-elements/presets/Gateway/gateway.ts +0 -107
  139. package/src/bpmn-elements/presets/Gateway/index.ts +0 -23
  140. package/src/bpmn-elements/presets/Pool/Lane.ts +0 -211
  141. package/src/bpmn-elements/presets/Pool/Pool.ts +0 -284
  142. package/src/bpmn-elements/presets/Pool/index.ts +0 -89
  143. package/src/bpmn-elements/presets/Task/index.ts +0 -122
  144. package/src/bpmn-elements/presets/Task/subProcess.ts +0 -189
  145. package/src/bpmn-elements/presets/Task/task.ts +0 -193
  146. package/src/bpmn-elements/presets/icons.ts +0 -155
  147. package/src/bpmn-elements/utils.ts +0 -52
  148. package/src/bpmn-elements-adapter/README.md +0 -293
  149. package/src/bpmn-elements-adapter/__tests__/adapter_in.test.js +0 -528
  150. package/src/bpmn-elements-adapter/__tests__/adapter_out.test.js +0 -569
  151. package/src/bpmn-elements-adapter/constant.ts +0 -76
  152. package/src/bpmn-elements-adapter/index.ts +0 -1134
  153. package/src/bpmn-elements-adapter/json2xml.ts +0 -91
  154. package/src/bpmn-elements-adapter/xml2json.ts +0 -548
  155. package/src/components/context-menu/index.ts +0 -253
  156. package/src/components/control/index.ts +0 -141
  157. package/src/components/dnd-panel/index.ts +0 -137
  158. package/src/components/highlight/index.ts +0 -227
  159. package/src/components/menu/index.ts +0 -748
  160. package/src/components/mini-map/index.ts +0 -686
  161. package/src/components/selection-select/index.ts +0 -387
  162. package/src/dynamic-group/index.ts +0 -775
  163. package/src/dynamic-group/model.ts +0 -562
  164. package/src/dynamic-group/node.ts +0 -288
  165. package/src/dynamic-group/utils.ts +0 -46
  166. package/src/index.less +0 -1
  167. package/src/index.ts +0 -45
  168. package/src/insert-node-in-polyline/edge.ts +0 -175
  169. package/src/insert-node-in-polyline/index.ts +0 -187
  170. package/src/materials/curved-edge/__test__/curved-edge.test.ts +0 -46
  171. package/src/materials/curved-edge/index.ts +0 -185
  172. package/src/materials/group/GroupNode.ts +0 -442
  173. package/src/materials/group/index.ts +0 -542
  174. package/src/materials/node-selection/index.ts +0 -380
  175. package/src/mindmap/fakerRoot.ts +0 -19
  176. package/src/mindmap/index.ts +0 -328
  177. package/src/mindmap/markContent.ts +0 -81
  178. package/src/mindmap/markContentOption.ts +0 -81
  179. package/src/mindmap/markEntity.ts +0 -82
  180. package/src/mindmap/markRoot.ts +0 -83
  181. package/src/mindmap/theme.ts +0 -11
  182. package/src/rect-label-node/RectLabelNodeView.ts +0 -33
  183. package/src/rect-label-node/index.ts +0 -15
  184. package/src/style/index.less +0 -342
  185. package/src/style/raw.ts +0 -295
  186. package/src/tools/auto-layout/index.ts +0 -282
  187. package/src/tools/flow-path/index.ts +0 -233
  188. package/src/tools/label/Label.tsx +0 -357
  189. package/src/tools/label/LabelModel.ts +0 -83
  190. package/src/tools/label/LabelOverlay.tsx +0 -158
  191. package/src/tools/label/algorithm.ts +0 -42
  192. package/src/tools/label/index.ts +0 -479
  193. package/src/tools/label/mediumEditor.ts +0 -94
  194. package/src/tools/label/utils.ts +0 -395
  195. package/src/tools/proximity-connect/index.ts +0 -435
  196. package/src/tools/snapshot/README.md +0 -145
  197. package/src/tools/snapshot/index.ts +0 -718
  198. package/src/tools/snapshot/utils.ts +0 -163
  199. package/src/turbo-adapter/index.ts +0 -212
  200. package/stats.html +0 -4842
  201. package/tsconfig.json +0 -18
@@ -1,127 +0,0 @@
1
- function type(obj) {
2
- return Object.prototype.toString.call(obj)
3
- }
4
-
5
- function addSpace(depth) {
6
- return ' '.repeat(depth)
7
- }
8
-
9
- function handleAttributes(o: any) {
10
- let t = o
11
- if (type(o) === '[object Object]') {
12
- t = {}
13
- Object.keys(o).forEach((k) => {
14
- let tk = k
15
- if (k.charAt(0) === '-') {
16
- tk = k.substring(1)
17
- }
18
- t[tk] = handleAttributes(o[k])
19
- })
20
- } else if (Array.isArray(o)) {
21
- t = []
22
- o.forEach((item, index) => {
23
- t[index] = handleAttributes(item)
24
- })
25
- }
26
- return t
27
- }
28
-
29
- /**
30
- * 将普通文本中的一些特殊字符进行转移,保障文本安全地嵌入 XML:
31
- * - 空值(`null/undefined`)返回空字符串,避免输出非法字面量;
32
- * - 按顺序转义 XML 保留字符:`&`, `<`, `>`, `"`, `'`;
33
- * 注意优先转义 `&`,避免后续生成的实体被再次转义。
34
- * @param text 原始文本
35
- * @returns 已完成 XML 转义的字符串
36
- */
37
- function escapeXml(text: string) {
38
- // 空值直接返回空字符串,防止在 XML 中出现 "null"/"undefined"
39
- if (text == null) return ''
40
- return (
41
- text
42
- .toString()
43
- // & 必须先转义,避免影响后续 < > " ' 的实体
44
- .replace(/&/g, '&amp;')
45
- // 小于号与大于号,用于标签边界
46
- .replace(/</g, '&lt;')
47
- .replace(/>/g, '&gt;')
48
- // 双引号与单引号,用于属性值的包裹
49
- .replace(/"/g, '&quot;')
50
- .replace(/'/g, '&apos;')
51
- )
52
- }
53
-
54
- function getAttributes(obj: any) {
55
- let tmp = obj
56
- try {
57
- if (typeof tmp !== 'string') {
58
- tmp = JSON.parse(obj)
59
- }
60
- } catch (error) {
61
- tmp = JSON.stringify(handleAttributes(obj)).replace(/"/g, "'")
62
- }
63
- // 确保属性值中的特殊字符被正确转义
64
- return escapeXml(String(tmp))
65
- }
66
-
67
- const tn = '\t\n'
68
-
69
- // @see issue https://github.com/didi/LogicFlow/issues/718, refactoring of function toXml
70
- function toXml(obj: string | any[] | Object, name: string, depth: number) {
71
- const frontSpace = addSpace(depth)
72
-
73
- // 假值除 0、false 外 -> 直接返回空元素 <prop />
74
- if (obj !== 0 && obj !== false && !obj) {
75
- return tn + frontSpace + `<${name} />`
76
- }
77
-
78
- let str = ''
79
- if (name === '#text') {
80
- return tn + frontSpace + escapeXml(String(obj))
81
- } else if (name === '#cdata-section') {
82
- return tn + frontSpace + '<![CDATA[' + obj + ']]>'
83
- } else if (name === '#comment') {
84
- return tn + frontSpace + '<!--' + obj + '-->'
85
- }
86
- if (`${name}`.charAt(0) === '-') {
87
- return ' ' + name.substring(1) + '="' + getAttributes(obj) + '"'
88
- } else {
89
- if (Array.isArray(obj)) {
90
- obj.forEach((item) => {
91
- str += toXml(item, name, depth + 1)
92
- })
93
- } else if (type(obj) === '[object Object]') {
94
- const keys = Object.keys(obj)
95
- let attributes = ''
96
- let children = ''
97
- str += (depth === 0 ? '' : tn + frontSpace) + '<' + name
98
- keys.forEach((k) => {
99
- k.charAt(0) === '-'
100
- ? (attributes += toXml(obj[k], k, depth + 1))
101
- : (children += toXml(obj[k], k, depth + 1))
102
- })
103
- str +=
104
- attributes +
105
- (children !== '' ? `>${children}${tn + frontSpace}</${name}>` : ' />')
106
- } else {
107
- str += tn + frontSpace + `<${name}>${escapeXml(String(obj))}</${name}>`
108
- }
109
- }
110
-
111
- return str
112
- }
113
-
114
- /**
115
- * json 转 xml
116
- * @param o object
117
- * @returns
118
- */
119
- function lfJson2Xml(o: Object) {
120
- let xmlStr = ''
121
- for (var m in o) {
122
- xmlStr += toXml(o[m], m, 0)
123
- }
124
- return xmlStr
125
- }
126
-
127
- export { lfJson2Xml, handleAttributes, escapeXml }
@@ -1,544 +0,0 @@
1
- // @ts-nocheck
2
-
3
- // ========================================================================
4
- // XML.ObjTree -- XML source code from/to JavaScript object like E4X
5
- // ========================================================================
6
- import { escapeXml } from './json2xml'
7
-
8
- let XML = function () {}
9
-
10
- // constructor
11
- XML.ObjTree = function () {
12
- // @ts-ignore
13
- return this
14
- }
15
-
16
- // class variables
17
-
18
- XML.ObjTree.VERSION = '0.23'
19
-
20
- // object prototype
21
-
22
- XML.ObjTree.prototype.xmlDecl = '<?xml version="1.0" encoding="UTF-8" ?>\n'
23
- XML.ObjTree.prototype.attr_prefix = '-'
24
-
25
- // method: parseXML( xmlsource )
26
-
27
- XML.ObjTree.prototype.parseXML = function (xml) {
28
- var root
29
- if (window.DOMParser) {
30
- var xmldom = new DOMParser()
31
- // xmldom.async = false; // DOMParser is always sync-mode
32
- var dom = xmldom.parseFromString(xml, 'application/xml')
33
- if (!dom) return
34
- root = dom.documentElement
35
- } else if (window.ActiveXObject) {
36
- xmldom = new ActiveXObject('Microsoft.XMLDOM')
37
- xmldom.async = false
38
- xmldom.loadXML(xml)
39
- root = xmldom.documentElement
40
- }
41
- if (!root) return
42
- return this.parseDOM(root)
43
- }
44
-
45
- // method: parseHTTP( url, options, callback )
46
-
47
- XML.ObjTree.prototype.parseHTTP = function (url, options, callback) {
48
- var myOpt = {}
49
- for (var key in options) {
50
- myOpt[key] = options[key] // copy object
51
- }
52
- if (!myOpt.method) {
53
- if (
54
- typeof myOpt.postBody == 'undefined' &&
55
- typeof myOpt.postbody == 'undefined' &&
56
- typeof myOpt.parameters == 'undefined'
57
- ) {
58
- myOpt.method = 'get'
59
- } else {
60
- myOpt.method = 'post'
61
- }
62
- }
63
- if (callback) {
64
- myOpt.asynchronous = true // async-mode
65
- var __this = this
66
- var __func = callback
67
- var __save = myOpt.onComplete
68
- myOpt.onComplete = function (trans) {
69
- var tree
70
- if (trans && trans.responseXML && trans.responseXML.documentElement) {
71
- tree = __this.parseDOM(trans.responseXML.documentElement)
72
- }
73
- __func(tree, trans)
74
- if (__save) __save(trans)
75
- }
76
- } else {
77
- myOpt.asynchronous = false // sync-mode
78
- }
79
- var trans
80
- if (typeof HTTP != 'undefined' && HTTP.Request) {
81
- myOpt.uri = url
82
- var req = new HTTP.Request(myOpt)
83
- if (req) trans = req.transport
84
- } else if (typeof Ajax != 'undefined' && Ajax.Request) {
85
- var req = new Ajax.Request(url, myOpt)
86
- if (req) trans = req.transport
87
- }
88
- if (callback) return trans
89
- if (trans && trans.responseXML && trans.responseXML.documentElement) {
90
- return this.parseDOM(trans.responseXML.documentElement)
91
- }
92
- }
93
-
94
- XML.ObjTree.prototype.parseDOM = function (root) {
95
- if (!root) return
96
-
97
- this.__force_array = {}
98
- if (this.force_array) {
99
- for (var i = 0; i < this.force_array.length; i++) {
100
- this.__force_array[this.force_array[i]] = 1
101
- }
102
- }
103
-
104
- var json = this.parseElement(root) // parse root node
105
- if (this.__force_array[root.nodeName]) {
106
- json = [json]
107
- }
108
- if (root.nodeType != 11) {
109
- // DOCUMENT_FRAGMENT_NODE
110
- var tmp = {}
111
- tmp[root.nodeName] = json // root nodeName
112
- json = tmp
113
- }
114
- return json
115
- }
116
-
117
- // method: parseElement( element )
118
- /**
119
- * @reference node type reference https://www.w3schools.com/xml/dom_nodetype.asp
120
- */
121
- XML.ObjTree.prototype.parseElement = function (elem) {
122
- // PROCESSING_INSTRUCTION_NODE
123
- if (elem.nodeType == 7) {
124
- return
125
- }
126
-
127
- // TEXT_NODE CDATA_SECTION_NODE COMMENT_NODE
128
- if (elem.nodeType == 3 || elem.nodeType == 4 || elem.nodeType == 8) {
129
- var bool = elem.nodeValue.match(/[^\x00-\x20]/)
130
- if (bool == null) return // ignore white spaces
131
- return elem.nodeValue
132
- }
133
-
134
- var retVal = null
135
- var cnt = {}
136
-
137
- if (elem.attributes && elem.attributes.length) {
138
- retVal = {}
139
- for (var i = 0; i < elem.attributes.length; i++) {
140
- var key = elem.attributes[i].nodeName
141
- if (typeof key != 'string') continue
142
- var val = elem.attributes[i].nodeValue
143
- try {
144
- val = JSON.parse(elem.attributes[i].nodeValue.replace(/'/g, '"'))
145
- } catch (error) {
146
- val = elem.attributes[i].nodeValue
147
- }
148
- if (!val) continue
149
- key = this.attr_prefix + key
150
- if (typeof cnt[key] == 'undefined') cnt[key] = 0
151
- cnt[key]++
152
- this.addNode(retVal, key, cnt[key], val)
153
- }
154
- }
155
-
156
- // parse child nodes (recursive)
157
- if (elem.childNodes && elem.childNodes.length) {
158
- var textOnly = true
159
- if (retVal) textOnly = false // some attributes exists
160
- for (var i = 0; i < elem.childNodes.length && textOnly; i++) {
161
- var nType = elem.childNodes[i].nodeType
162
- if (nType == 3 || nType == 4 || nType == 8) continue
163
- textOnly = false
164
- }
165
- if (textOnly) {
166
- if (!retVal) retVal = ''
167
- for (var i = 0; i < elem.childNodes.length; i++) {
168
- retVal += elem.childNodes[i].nodeValue
169
- }
170
- } else {
171
- if (!retVal) retVal = {}
172
- for (var i = 0; i < elem.childNodes.length; i++) {
173
- var key = elem.childNodes[i].nodeName
174
- if (typeof key != 'string') continue
175
- var val = this.parseElement(elem.childNodes[i])
176
- if (!val) continue
177
- if (typeof cnt[key] == 'undefined') cnt[key] = 0
178
- cnt[key]++
179
- this.addNode(retVal, key, cnt[key], val)
180
- }
181
- }
182
- } else {
183
- // @see issue https://github.com/didi/LogicFlow/issues/1068
184
- // if retVal is null, that means the elem doesn't have any attributes and children,
185
- // the elem would be like: <a /> or <a></a>, so set retVal to empty object {}
186
- retVal === null && (retVal = {})
187
- }
188
- return retVal
189
- }
190
-
191
- // method: addNode( hash, key, count, value )
192
-
193
- XML.ObjTree.prototype.addNode = function (hash, key, counts, val) {
194
- if (this.__force_array[key]) {
195
- if (counts == 1) hash[key] = []
196
- hash[key][hash[key].length] = val // push
197
- } else if (counts == 1) {
198
- // 1st sibling
199
- hash[key] = val
200
- } else if (counts == 2) {
201
- // 2nd sibling
202
- hash[key] = [hash[key], val]
203
- } else {
204
- // 3rd sibling and more
205
- hash[key][hash[key].length] = val
206
- }
207
- }
208
-
209
- // method: writeXML( tree )
210
-
211
- XML.ObjTree.prototype.writeXML = function (tree) {
212
- var xml = this.hash_to_xml(null, tree)
213
- return this.xmlDecl + xml
214
- }
215
-
216
- // method: hash_to_xml( tagName, tree )
217
-
218
- XML.ObjTree.prototype.hash_to_xml = function (name, tree) {
219
- var elem = []
220
- var attr = []
221
- for (var key in tree) {
222
- if (!tree.hasOwnProperty(key)) continue
223
- var val = tree[key]
224
- if (key.charAt(0) != this.attr_prefix) {
225
- if (typeof val == 'undefined' || val == null) {
226
- elem[elem.length] = '<' + key + ' />'
227
- } else if (typeof val == 'object' && val.constructor == Array) {
228
- elem[elem.length] = this.array_to_xml(key, val)
229
- } else if (typeof val == 'object') {
230
- elem[elem.length] = this.hash_to_xml(key, val)
231
- } else {
232
- elem[elem.length] = this.scalar_to_xml(key, val)
233
- }
234
- } else {
235
- attr[attr.length] =
236
- ' ' + key.substring(1) + '="' + this.xml_escape(val) + '"'
237
- }
238
- }
239
- var jattr = attr.join('')
240
- var jelem = elem.join('')
241
- if (typeof name == 'undefined' || name == null) {
242
- // no tag
243
- } else if (elem.length > 0) {
244
- if (jelem.match(/\n/)) {
245
- jelem = '<' + name + jattr + '>\n' + jelem + '</' + name + '>\n'
246
- } else {
247
- jelem = '<' + name + jattr + '>' + jelem + '</' + name + '>\n'
248
- }
249
- } else {
250
- jelem = '<' + name + jattr + ' />\n'
251
- }
252
- return jelem
253
- }
254
-
255
- // method: array_to_xml( tagName, array )
256
-
257
- XML.ObjTree.prototype.array_to_xml = function (name, array) {
258
- var out = []
259
- for (var i = 0; i < array.length; i++) {
260
- var val = array[i]
261
- if (typeof val == 'undefined' || val == null) {
262
- out[out.length] = '<' + name + ' />'
263
- } else if (typeof val == 'object' && val.constructor == Array) {
264
- out[out.length] = this.array_to_xml(name, val)
265
- } else if (typeof val == 'object') {
266
- out[out.length] = this.hash_to_xml(name, val)
267
- } else {
268
- out[out.length] = this.scalar_to_xml(name, val)
269
- }
270
- }
271
- return out.join('')
272
- }
273
-
274
- // method: scalar_to_xml( tagName, text )
275
-
276
- XML.ObjTree.prototype.scalar_to_xml = function (name, text) {
277
- if (name == '#text') {
278
- return this.xml_escape(text)
279
- } else {
280
- return '<' + name + '>' + this.xml_escape(text) + '</' + name + '>\n'
281
- }
282
- }
283
-
284
- // method: xml_escape( text )
285
-
286
- XML.ObjTree.prototype.xml_escape = escapeXml
287
-
288
- /*
289
- // ========================================================================
290
-
291
- =head1 NAME
292
-
293
- XML.ObjTree -- XML source code from/to JavaScript object like E4X
294
-
295
- =head1 SYNOPSIS
296
-
297
- var xotree = new XML.ObjTree();
298
- var tree1 = {
299
- root: {
300
- node: "Hello, World!"
301
- }
302
- };
303
- var xml1 = xotree.writeXML( tree1 ); // object tree to XML source
304
- alert( "xml1: "+xml1 );
305
-
306
- var xml2 = '<?xml version="1.0"?><response><error>0</error></response>';
307
- var tree2 = xotree.parseXML( xml2 ); // XML source to object tree
308
- alert( "error: "+tree2.response.error );
309
-
310
- =head1 DESCRIPTION
311
-
312
- XML.ObjTree class is a parser/generator between XML source code
313
- and JavaScript object like E4X, ECMAScript for XML.
314
- This is a JavaScript version of the XML::TreePP module for Perl.
315
- This also works as a wrapper for XMLHTTPRequest and successor to JKL.ParseXML class
316
- when this is used with prototype.js or JSAN's HTTP.Request class.
317
-
318
- =head2 JavaScript object tree format
319
-
320
- A sample XML source:
321
-
322
- <?xml version="1.0" encoding="UTF-8"?>
323
- <family name="Kawasaki">
324
- <father>Yasuhisa</father>
325
- <mother>Chizuko</mother>
326
- <children>
327
- <girl>Shiori</girl>
328
- <boy>Yusuke</boy>
329
- <boy>Kairi</boy>
330
- </children>
331
- </family>
332
-
333
- Its JavaScript object tree like JSON/E4X:
334
-
335
- {
336
- 'family': {
337
- '-name': 'Kawasaki',
338
- 'father': 'Yasuhisa',
339
- 'mother': 'Chizuko',
340
- 'children': {
341
- 'girl': 'Shiori'
342
- 'boy': [
343
- 'Yusuke',
344
- 'Kairi'
345
- ]
346
- }
347
- }
348
- };
349
-
350
- Each elements are parsed into objects:
351
-
352
- tree.family.father; # the father's given name.
353
-
354
- Prefix '-' is inserted before every attributes' name.
355
-
356
- tree.family["-name"]; # this family's family name
357
-
358
- A array is used because this family has two boys.
359
-
360
- tree.family.children.boy[0]; # first boy's name
361
- tree.family.children.boy[1]; # second boy's name
362
- tree.family.children.girl; # (girl has no other sisters)
363
-
364
- =head1 METHODS
365
-
366
- =head2 xotree = new XML.ObjTree()
367
-
368
- This constructor method returns a new XML.ObjTree object.
369
-
370
- =head2 xotree.force_array = [ "rdf:li", "item", "-xmlns" ];
371
-
372
- This property allows you to specify a list of element names
373
- which should always be forced into an array representation.
374
- The default value is null, it means that context of the elements
375
- will determine to make array or to keep it scalar.
376
-
377
- =head2 xotree.attr_prefix = '@';
378
-
379
- This property allows you to specify a prefix character which is
380
- inserted before each attribute names.
381
- Instead of default prefix '-', E4X-style prefix '@' is also available.
382
- The default character is '-'.
383
- Or set '@' to access attribute values like E4X, ECMAScript for XML.
384
- The length of attr_prefix must be just one character and not be empty.
385
-
386
- =head2 tree = xotree.parseXML( xmlSrc );
387
-
388
- This method loads an XML document using the supplied string
389
- and returns its JavaScript object converted.
390
-
391
- =head2 tree = xotree.parseDOM( domNode );
392
-
393
- This method parses a DOM tree (ex. responseXML.documentElement)
394
- and returns its JavaScript object converted.
395
-
396
- =head2 tree = xotree.parseHTTP( url, options );
397
-
398
- This method loads a XML file from remote web server
399
- and returns its JavaScript object converted.
400
- XMLHTTPRequest's synchronous mode is always used.
401
- This mode blocks the process until the response is completed.
402
-
403
- First argument is a XML file's URL
404
- which must exist in the same domain as parent HTML file's.
405
- Cross-domain loading is not available for security reasons.
406
-
407
- Second argument is options' object which can contains some parameters:
408
- method, postBody, parameters, onLoading, etc.
409
-
410
- This method requires JSAN's L<HTTP.Request> class or prototype.js's Ajax.Request class.
411
-
412
- =head2 xotree.parseHTTP( url, options, callback );
413
-
414
- If a callback function is set as third argument,
415
- XMLHTTPRequest's asynchronous mode is used.
416
-
417
- This mode calls a callback function with XML file's JavaScript object converted
418
- after the response is completed.
419
-
420
- =head2 xmlSrc = xotree.writeXML( tree );
421
-
422
- This method parses a JavaScript object tree
423
- and returns its XML source generated.
424
-
425
- =head1 EXAMPLES
426
-
427
- =head2 Text node and attributes
428
-
429
- If a element has both of a text node and attributes
430
- or both of a text node and other child nodes,
431
- text node's value is moved to a special node named "#text".
432
-
433
- var xotree = new XML.ObjTree();
434
- var xmlSrc = '<span class="author">Kawasaki Yusuke</span>';
435
- var tree = xotree.parseXML( xmlSrc );
436
- var class = tree.span["-class"]; # attribute
437
- var name = tree.span["#text"]; # text node
438
-
439
- =head2 parseHTTP() method with HTTP-GET and sync-mode
440
-
441
- HTTP/Request.js or prototype.js must be loaded before calling this method.
442
-
443
- var xotree = new XML.ObjTree();
444
- var url = "http://example.com/index.html";
445
- var tree = xotree.parseHTTP( url );
446
- xotree.attr_prefix = '@'; // E4X-style
447
- alert( tree.html["@lang"] );
448
-
449
- This code shows C<lang=""> attribute from a X-HTML source code.
450
-
451
- =head2 parseHTTP() method with HTTP-POST and async-mode
452
-
453
- Third argument is a callback function which is called on onComplete.
454
-
455
- var xotree = new XML.ObjTree();
456
- var url = "http://example.com/mt-tb.cgi";
457
- var opts = {
458
- postBody: "title=...&excerpt=...&url=...&blog_name=..."
459
- };
460
- var func = function ( tree ) {
461
- alert( tree.response.error );
462
- };
463
- xotree.parseHTTP( url, opts, func );
464
-
465
- This code send a track back ping and shows its response code.
466
-
467
- =head2 Simple RSS reader
468
-
469
- This is a RSS reader which loads RDF file and displays all items.
470
-
471
- var xotree = new XML.ObjTree();
472
- xotree.force_array = [ "rdf:li", "item" ];
473
- var url = "http://example.com/news-rdf.xml";
474
- var func = function( tree ) {
475
- var elem = document.getElementById("rss_here");
476
- for( var i=0; i<tree["rdf:RDF"].item.length; i++ ) {
477
- var divTag = document.createElement( "div" );
478
- var aTag = document.createElement( "a" );
479
- aTag.href = tree["rdf:RDF"].item[i].link;
480
- var title = tree["rdf:RDF"].item[i].title;
481
- var tNode = document.createTextNode( title );
482
- aTag.appendChild( tNode );
483
- divTag.appendChild( aTag );
484
- elem.appendChild( divTag );
485
- }
486
- };
487
- xotree.parseHTTP( url, {}, func );
488
-
489
- =head2 XML-RPC using writeXML, prototype.js and parseDOM
490
-
491
- If you wish to use prototype.js's Ajax.Request class by yourself:
492
-
493
- var xotree = new XML.ObjTree();
494
- var reqTree = {
495
- methodCall: {
496
- methodName: "weblogUpdates.ping",
497
- params: {
498
- param: [
499
- { value: "Kawa.net xp top page" }, // 1st param
500
- { value: "http://www.kawa.net/" } // 2nd param
501
- ]
502
- }
503
- }
504
- };
505
- var reqxml = xotree.writeXML( reqTree ); // JS-Object to XML code
506
- var url = "http://example.com/xmlrpc";
507
- var func = function( req ) {
508
- var resDom = req.responseXML.documentElement;
509
- xotree.force_array = [ "member" ];
510
- var resTree = xotree.parseDOM( resDom ); // XML-DOM to JS-Object
511
- alert( resTree.methodResponse.params.param.value.struct.member[0].value.string );
512
- };
513
- var opt = {
514
- method: "post",
515
- postBody: reqxml,
516
- asynchronous: true,
517
- onComplete: func
518
- };
519
- new Ajax.Request( url, opt );
520
-
521
- =head1 AUTHOR
522
-
523
- Yusuke Kawasaki http://www.kawa.net/
524
- =head1 COPYRIGHT AND LICENSE
525
-
526
- Copyright (c) 2005-2006 Yusuke Kawasaki. All rights reserved.
527
- This program is free software; you can redistribute it and/or
528
- modify it under the Artistic license. Or whatever license I choose,
529
- which I will do instead of keeping this documentation like it is.
530
-
531
- =cut
532
- // ========================================================================
533
- */
534
-
535
- /**
536
- * xml 转 json
537
- * @param xmlData
538
- * @returns
539
- */
540
- const lfXml2Json = (xmlData) => {
541
- return new XML.ObjTree().parseXML(xmlData)
542
- }
543
-
544
- export { lfXml2Json }