@logicflow/extension 1.2.3 → 1.2.5

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.
@@ -1,4 +1,28 @@
1
1
  "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ extendStatics(d, b);
11
+ function __() { this.constructor = d; }
12
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+ };
14
+ })();
15
+ var __assign = (this && this.__assign) || function () {
16
+ __assign = Object.assign || function(t) {
17
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
18
+ s = arguments[i];
19
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20
+ t[p] = s[p];
21
+ }
22
+ return t;
23
+ };
24
+ return __assign.apply(this, arguments);
25
+ };
2
26
  var __read = (this && this.__read) || function (o, n) {
3
27
  var m = typeof Symbol === "function" && o[Symbol.iterator];
4
28
  if (!m) return o;
@@ -16,7 +40,7 @@ var __read = (this && this.__read) || function (o, n) {
16
40
  return ar;
17
41
  };
18
42
  Object.defineProperty(exports, "__esModule", { value: true });
19
- exports.BpmnXmlAdapter = exports.BpmnAdapter = void 0;
43
+ exports.toNormalJson = exports.toXmlJson = exports.BpmnXmlAdapter = exports.BpmnAdapter = void 0;
20
44
  var bpmnIds_1 = require("./bpmnIds");
21
45
  var json2xml_1 = require("./json2xml");
22
46
  var xml2json_1 = require("./xml2json");
@@ -30,30 +54,62 @@ var BpmnElements;
30
54
  BpmnElements["SYSTEM"] = "bpmn:serviceTask";
31
55
  BpmnElements["FLOW"] = "bpmn:sequenceFlow";
32
56
  })(BpmnElements || (BpmnElements = {}));
33
- var defaultAttrs = ['-name', '-id', 'bpmn:incoming', 'bpmn:outgoing', '-sourceRef', '-targetRef'];
57
+ var defaultAttrs = [
58
+ '-name',
59
+ '-id',
60
+ 'bpmn:incoming',
61
+ 'bpmn:outgoing',
62
+ '-sourceRef',
63
+ '-targetRef',
64
+ ];
34
65
  /**
35
66
  * 将普通json转换为xmlJson
36
67
  * xmlJson中property会以“-”开头
37
68
  * 如果没有“-”表示为子节点
69
+ * fix issue https://github.com/didi/LogicFlow/issues/718, contain the process of #text/#cdata and array
70
+ * @param retainedFields retainedField会和默认的defaultRetainedFields:
71
+ * ["properties", "startPoint", "endPoint", "pointsList"]合并
72
+ * 这意味着出现在这个数组里的字段当它的值是数组或是对象时不会被视为一个节点而是一个属性
73
+ * @reference node type reference https://www.w3schools.com/xml/dom_nodetype.asp
38
74
  */
39
- function toXmlJson(json) {
40
- var xmlJson = {};
41
- Object.entries(json).forEach(function (_a) {
42
- var _b = __read(_a, 2), key = _b[0], value = _b[1];
43
- if (typeof value !== 'object') {
44
- if (key.indexOf('-') === 0) { // 如果本来就是“-”开头的了,那就不处理了。
45
- xmlJson[key] = value;
75
+ var defaultRetainedFields = ['properties', 'startPoint', 'endPoint', 'pointsList'];
76
+ function toXmlJson(retainedFields) {
77
+ var fields = retainedFields
78
+ ? defaultRetainedFields.concat(retainedFields) : defaultRetainedFields;
79
+ return function (json) {
80
+ function ToXmlJson(obj) {
81
+ var xmlJson = {};
82
+ if (typeof obj === 'string') {
83
+ return obj;
46
84
  }
47
- else {
48
- xmlJson["-" + key] = value;
85
+ if (Array.isArray(obj)) {
86
+ return obj.map(function (j) { return ToXmlJson(j); });
49
87
  }
88
+ Object.entries(obj).forEach(function (_a) {
89
+ var _b = __read(_a, 2), key = _b[0], value = _b[1];
90
+ if (typeof value !== 'object') {
91
+ // node type reference https://www.w3schools.com/xml/dom_nodetype.asp
92
+ if (key.indexOf('-') === 0
93
+ || ['#text', '#cdata-section', '#comment'].includes(key)) {
94
+ xmlJson[key] = value;
95
+ }
96
+ else {
97
+ xmlJson["-" + key] = value;
98
+ }
99
+ }
100
+ else if (fields.includes(key)) {
101
+ xmlJson["-" + key] = ToXmlJson(value);
102
+ }
103
+ else {
104
+ xmlJson[key] = ToXmlJson(value);
105
+ }
106
+ });
107
+ return xmlJson;
50
108
  }
51
- else {
52
- xmlJson[key] = toXmlJson(value);
53
- }
54
- });
55
- return xmlJson;
109
+ return ToXmlJson(json);
110
+ };
56
111
  }
112
+ exports.toXmlJson = toXmlJson;
57
113
  /**
58
114
  * 将xmlJson转换为普通的json,在内部使用。
59
115
  */
@@ -61,23 +117,26 @@ function toNormalJson(xmlJson) {
61
117
  var json = {};
62
118
  Object.entries(xmlJson).forEach(function (_a) {
63
119
  var _b = __read(_a, 2), key = _b[0], value = _b[1];
64
- if (typeof value === 'string') {
65
- if (key.indexOf('-') === 0) { // 如果本来就是“-”开头的了,那就不处理了。
66
- json[key.substr(1)] = value;
67
- }
68
- else {
69
- json[key] = value;
70
- }
120
+ if (key.indexOf('-') === 0) {
121
+ json[key.substring(1)] = json2xml_1.handleAttributes(value);
122
+ }
123
+ else if (typeof value === 'string') {
124
+ json[key] = value;
71
125
  }
72
- else if (typeof value === 'object') {
126
+ else if (Object.prototype.toString.call(value) === '[object Object]') {
73
127
  json[key] = toNormalJson(value);
74
128
  }
129
+ else if (Array.isArray(value)) {
130
+ // contain the process of array
131
+ json[key] = value.map(function (v) { return toNormalJson(v); });
132
+ }
75
133
  else {
76
134
  json[key] = value;
77
135
  }
78
136
  });
79
137
  return json;
80
138
  }
139
+ exports.toNormalJson = toNormalJson;
81
140
  /**
82
141
  * 设置bpmn process信息
83
142
  * 目标格式请参考examples/bpmn.json
@@ -97,28 +156,25 @@ function convertLf2ProcessData(bpmnProcessData, data) {
97
156
  processNode['-name'] = node.text.value;
98
157
  }
99
158
  if (node.properties) {
100
- var properties = toXmlJson(node.properties);
159
+ var properties = toXmlJson()(node.properties);
101
160
  Object.assign(processNode, properties);
102
161
  }
103
162
  nodeMap.set(node.id, processNode);
104
163
  if (!bpmnProcessData[node.type]) {
105
164
  bpmnProcessData[node.type] = processNode; // 如果只有一个子元素,json中表示为正常属性
106
165
  }
107
- else if (Array.isArray(bpmnProcessData[node.type])) { // 如果是多个子元素,json中使用数组存储
166
+ else if (Array.isArray(bpmnProcessData[node.type])) {
167
+ // 如果是多个子元素,json中使用数组存储
108
168
  bpmnProcessData[node.type].push(processNode);
109
169
  }
110
- else { // 如果是多个子元素,json中使用数组存储
111
- bpmnProcessData[node.type] = [
112
- bpmnProcessData[node.type],
113
- processNode,
114
- ];
170
+ else {
171
+ // 如果是多个子元素,json中使用数组存储
172
+ bpmnProcessData[node.type] = [bpmnProcessData[node.type], processNode];
115
173
  }
116
174
  });
117
175
  var sequenceFlow = data.edges.map(function (edge) {
118
176
  var _a, _b;
119
177
  var targetNode = nodeMap.get(edge.targetNodeId);
120
- // @see https://github.com/didi/LogicFlow/issues/325
121
- // 需要保证incomming在outgoing之前
122
178
  if (!targetNode['bpmn:incoming']) {
123
179
  targetNode['bpmn:incoming'] = edge.id;
124
180
  }
@@ -126,23 +182,7 @@ function convertLf2ProcessData(bpmnProcessData, data) {
126
182
  targetNode['bpmn:incoming'].push(edge.id);
127
183
  }
128
184
  else {
129
- targetNode['bpmn:incoming'] = [
130
- targetNode['bpmn:incoming'],
131
- edge.id,
132
- ];
133
- }
134
- var sourceNode = nodeMap.get(edge.sourceNodeId);
135
- if (!sourceNode['bpmn:outgoing']) {
136
- sourceNode['bpmn:outgoing'] = edge.id;
137
- }
138
- else if (Array.isArray(sourceNode['bpmn:outgoing'])) {
139
- sourceNode['bpmn:outgoing'].push(edge.id);
140
- }
141
- else { // 字符串转数组
142
- sourceNode['bpmn:outgoing'] = [
143
- sourceNode['bpmn:outgoing'],
144
- edge.id,
145
- ];
185
+ targetNode['bpmn:incoming'] = [targetNode['bpmn:incoming'], edge.id];
146
186
  }
147
187
  var edgeConfig = {
148
188
  '-id': edge.id,
@@ -153,11 +193,26 @@ function convertLf2ProcessData(bpmnProcessData, data) {
153
193
  edgeConfig['-name'] = (_b = edge.text) === null || _b === void 0 ? void 0 : _b.value;
154
194
  }
155
195
  if (edge.properties) {
156
- var properties = toXmlJson(edge.properties);
196
+ var properties = toXmlJson()(edge.properties);
157
197
  Object.assign(edgeConfig, properties);
158
198
  }
159
199
  return edgeConfig;
160
200
  });
201
+ // @see https://github.com/didi/LogicFlow/issues/325
202
+ // 需要保证incoming在outgoing之前
203
+ data.edges.forEach(function (edge) {
204
+ var sourceNode = nodeMap.get(edge.sourceNodeId);
205
+ if (!sourceNode['bpmn:outgoing']) {
206
+ sourceNode['bpmn:outgoing'] = edge.id;
207
+ }
208
+ else if (Array.isArray(sourceNode['bpmn:outgoing'])) {
209
+ sourceNode['bpmn:outgoing'].push(edge.id);
210
+ }
211
+ else {
212
+ // 字符串转数组
213
+ sourceNode['bpmn:outgoing'] = [sourceNode['bpmn:outgoing'], edge.id];
214
+ }
215
+ });
161
216
  bpmnProcessData[BpmnElements.FLOW] = sequenceFlow;
162
217
  }
163
218
  /**
@@ -169,7 +224,10 @@ function convertLf2DiagramData(bpmnDiagramData, data) {
169
224
  var edgeId = edge.id;
170
225
  var pointsList = edge.pointsList.map(function (_a) {
171
226
  var x = _a.x, y = _a.y;
172
- return ({ '-x': x, '-y': y });
227
+ return ({
228
+ '-x': x,
229
+ '-y': y,
230
+ });
173
231
  });
174
232
  var diagramData = {
175
233
  '-id': edgeId + "_di",
@@ -255,7 +313,8 @@ function convertBpmn2LfData(bpmnData) {
255
313
  }
256
314
  function getLfNodes(value, shapes, key) {
257
315
  var nodes = [];
258
- if (Array.isArray(value)) { // 数组
316
+ if (Array.isArray(value)) {
317
+ // 数组
259
318
  value.forEach(function (val) {
260
319
  var shapeValue;
261
320
  if (Array.isArray(shapes)) {
@@ -311,7 +370,8 @@ function getNodeConfig(shapeValue, type, processValue) {
311
370
  value: name,
312
371
  };
313
372
  // 自定义文本位置
314
- if (shapeValue['bpmndi:BPMNLabel'] && shapeValue['bpmndi:BPMNLabel']['dc:Bounds']) {
373
+ if (shapeValue['bpmndi:BPMNLabel']
374
+ && shapeValue['bpmndi:BPMNLabel']['dc:Bounds']) {
315
375
  var textBounds = shapeValue['bpmndi:BPMNLabel']['dc:Bounds'];
316
376
  text.x = Number(textBounds['-x']) + Number(textBounds['-width']) / 2;
317
377
  text.y = Number(textBounds['-y']) + Number(textBounds['-height']) / 2;
@@ -402,53 +462,59 @@ function getEdgeConfig(edgeValue, processValue) {
402
462
  }
403
463
  return edge;
404
464
  }
405
- var BpmnAdapter = {
406
- pluginName: 'bpmn-adapter',
407
- install: function (lf) {
408
- lf.adapterIn = this.adapterIn;
409
- lf.adapterOut = this.adapterOut;
410
- },
411
- shapeConfigMap: new Map(),
412
- setCustomShape: function (key, val) {
413
- this.shapeConfigMap.set(key, val);
414
- },
415
- adapterOut: function (data) {
416
- var bpmnProcessData = {
417
- '-id': "Process_" + bpmnIds_1.getBpmnId(),
418
- '-isExecutable': 'false',
465
+ var BpmnAdapter = /** @class */ (function () {
466
+ function BpmnAdapter(_a) {
467
+ var _this = this;
468
+ var lf = _a.lf;
469
+ this.adapterOut = function (data) {
470
+ var bpmnProcessData = __assign({}, _this.processAttributes);
471
+ convertLf2ProcessData(bpmnProcessData, data);
472
+ var bpmnDiagramData = {
473
+ '-id': 'BPMNPlane_1',
474
+ '-bpmnElement': bpmnProcessData['-id'],
475
+ };
476
+ convertLf2DiagramData(bpmnDiagramData, data);
477
+ var definitions = _this.definitionAttributes;
478
+ definitions['bpmn:process'] = bpmnProcessData;
479
+ definitions['bpmndi:BPMNDiagram'] = {
480
+ '-id': 'BPMNDiagram_1',
481
+ 'bpmndi:BPMNPlane': bpmnDiagramData,
482
+ };
483
+ var bpmnData = {
484
+ 'bpmn:definitions': definitions,
485
+ };
486
+ return bpmnData;
419
487
  };
420
- convertLf2ProcessData(bpmnProcessData, data);
421
- var bpmnDiagramData = {
422
- '-id': 'BPMNPlane_1',
423
- '-bpmnElement': bpmnProcessData['-id'],
488
+ this.adapterIn = function (bpmnData) {
489
+ if (bpmnData) {
490
+ return convertBpmn2LfData(bpmnData);
491
+ }
424
492
  };
425
- convertLf2DiagramData(bpmnDiagramData, data);
426
- var bpmnData = {
427
- 'bpmn:definitions': {
428
- '-id': "Definitions_" + bpmnIds_1.getBpmnId(),
429
- '-xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
430
- '-xmlns:bpmn': 'http://www.omg.org/spec/BPMN/20100524/MODEL',
431
- '-xmlns:bpmndi': 'http://www.omg.org/spec/BPMN/20100524/DI',
432
- '-xmlns:dc': 'http://www.omg.org/spec/DD/20100524/DC',
433
- '-xmlns:di': 'http://www.omg.org/spec/DD/20100524/DI',
434
- '-targetNamespace': 'http://bpmn.io/schema/bpmn',
435
- '-exporter': 'bpmn-js (https://demo.bpmn.io)',
436
- '-exporterVersion': '7.3.0',
437
- 'bpmn:process': bpmnProcessData,
438
- 'bpmndi:BPMNDiagram': {
439
- '-id': 'BPMNDiagram_1',
440
- 'bpmndi:BPMNPlane': bpmnDiagramData,
441
- },
442
- },
493
+ lf.adapterIn = function (data) { return _this.adapterIn(data); };
494
+ lf.adapterOut = function (data) { return _this.adapterOut(data); };
495
+ this.processAttributes = {
496
+ '-isExecutable': 'true',
497
+ '-id': "Process_" + bpmnIds_1.getBpmnId(),
443
498
  };
444
- return bpmnData;
445
- },
446
- adapterIn: function (bpmnData) {
447
- if (bpmnData) {
448
- return convertBpmn2LfData(bpmnData);
449
- }
450
- },
451
- };
499
+ this.definitionAttributes = {
500
+ '-id': "Definitions_" + bpmnIds_1.getBpmnId(),
501
+ '-xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
502
+ '-xmlns:bpmn': 'http://www.omg.org/spec/BPMN/20100524/MODEL',
503
+ '-xmlns:bpmndi': 'http://www.omg.org/spec/BPMN/20100524/DI',
504
+ '-xmlns:dc': 'http://www.omg.org/spec/DD/20100524/DC',
505
+ '-xmlns:di': 'http://www.omg.org/spec/DD/20100524/DI',
506
+ '-targetNamespace': 'http://logic-flow.org',
507
+ '-exporter': 'logicflow',
508
+ '-exporterVersion': '1.2.0',
509
+ };
510
+ }
511
+ BpmnAdapter.prototype.setCustomShape = function (key, val) {
512
+ BpmnAdapter.shapeConfigMap.set(key, val);
513
+ };
514
+ BpmnAdapter.pluginName = 'bpmn-adapter';
515
+ BpmnAdapter.shapeConfigMap = new Map();
516
+ return BpmnAdapter;
517
+ }());
452
518
  exports.BpmnAdapter = BpmnAdapter;
453
519
  BpmnAdapter.shapeConfigMap.set(BpmnElements.START, {
454
520
  width: constant_1.StartEventConfig.width,
@@ -470,20 +536,25 @@ BpmnAdapter.shapeConfigMap.set(BpmnElements.USER, {
470
536
  width: constant_1.UserTaskConfig.width,
471
537
  height: constant_1.UserTaskConfig.height,
472
538
  });
473
- var BpmnXmlAdapter = {
474
- pluginName: 'bpmnXmlAdapter',
475
- install: function (lf) {
476
- lf.adapterIn = this.adapterXmlIn;
477
- lf.adapterOut = this.adapterXmlOut;
478
- },
479
- adapterXmlIn: function (bpmnData) {
480
- var json = xml2json_1.lfXml2Json(bpmnData);
481
- return BpmnAdapter.adapterIn(json);
482
- },
483
- adapterXmlOut: function (data) {
484
- var outData = BpmnAdapter.adapterOut(data);
485
- return json2xml_1.lfJson2Xml(outData);
486
- },
487
- };
539
+ var BpmnXmlAdapter = /** @class */ (function (_super) {
540
+ __extends(BpmnXmlAdapter, _super);
541
+ function BpmnXmlAdapter(data) {
542
+ var _this = _super.call(this, data) || this;
543
+ _this.adapterXmlIn = function (bpmnData) {
544
+ var json = xml2json_1.lfXml2Json(bpmnData);
545
+ return _this.adapterIn(json);
546
+ };
547
+ _this.adapterXmlOut = function (data) {
548
+ var outData = _this.adapterOut(data);
549
+ return json2xml_1.lfJson2Xml(outData);
550
+ };
551
+ var lf = data.lf;
552
+ lf.adapterIn = _this.adapterXmlIn;
553
+ lf.adapterOut = _this.adapterXmlOut;
554
+ return _this;
555
+ }
556
+ BpmnXmlAdapter.pluginName = 'bpmnXmlAdapter';
557
+ return BpmnXmlAdapter;
558
+ }(BpmnAdapter));
488
559
  exports.BpmnXmlAdapter = BpmnXmlAdapter;
489
560
  exports.default = BpmnAdapter;
@@ -1,62 +1,94 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.lfJson2Xml = void 0;
4
- /**
5
- * This work is licensed under Creative Commons GNU LGPL License.
6
- * License:
7
- * Version: 0.9
8
- * Author: Stefan Goessner/2006
9
- * Web: http://goessner.net/
10
- */
11
- function addIndSpace(ind, deep) {
12
- for (var i = 0; i < deep; i++) {
13
- ind += ' ';
14
- }
15
- return ind;
3
+ exports.handleAttributes = exports.lfJson2Xml = void 0;
4
+ function type(obj) {
5
+ return Object.prototype.toString.call(obj);
16
6
  }
17
- function toXml(v, name, ind, deep) {
18
- var xml = "";
19
- if (v instanceof Array) {
20
- for (var i = 0, n = v.length; i < n; i++) {
21
- xml += addIndSpace(ind, deep) + toXml(v[i], name, ind, deep + 1);
7
+ function addSpace(depth) {
8
+ return " ".repeat(depth);
9
+ }
10
+ function handleAttributes(o) {
11
+ var t = o;
12
+ if (type(o) === "[object Object]") {
13
+ t = {};
14
+ Object.keys(o).forEach(function (k) {
15
+ var tk = k;
16
+ if (k.charAt(0) === "-") {
17
+ tk = k.substring(1);
18
+ }
19
+ t[tk] = handleAttributes(o[k]);
20
+ });
21
+ }
22
+ else if (Array.isArray(o)) {
23
+ t = [];
24
+ o.forEach(function (item, index) {
25
+ t[index] = handleAttributes(item);
26
+ });
27
+ }
28
+ return t;
29
+ }
30
+ exports.handleAttributes = handleAttributes;
31
+ ;
32
+ function getAttributes(obj) {
33
+ var tmp = obj;
34
+ try {
35
+ if (typeof tmp !== "string") {
36
+ tmp = JSON.parse(obj);
22
37
  }
23
38
  }
24
- else if (typeof (v) == "object") {
25
- var hasChild = false;
26
- xml += addIndSpace(ind, deep) + "<" + name;
27
- for (var m in v) {
28
- if (m.charAt(0) == "-")
29
- xml += " " + m.substr(1) + "=\"" + v[m].toString() + "\"";
30
- else
31
- hasChild = true;
39
+ catch (error) {
40
+ tmp = JSON.stringify(handleAttributes(obj)).replace(/"/g, "'");
41
+ }
42
+ return tmp;
43
+ }
44
+ var tn = "\t\n";
45
+ // @see issue https://github.com/didi/LogicFlow/issues/718, refactoring of function toXml
46
+ function toXml(obj, name, depth) {
47
+ var frontSpace = addSpace(depth);
48
+ var str = "";
49
+ if (name === "#text") {
50
+ return tn + frontSpace + obj;
51
+ }
52
+ else if (name === "#cdata-section") {
53
+ return tn + frontSpace + "<![CDATA[" + obj + "]]>";
54
+ }
55
+ else if (name === "#comment") {
56
+ return tn + frontSpace + "<!--" + obj + "-->";
57
+ }
58
+ if (("" + name).charAt(0) === "-") {
59
+ return " " + name.substring(1) + '="' + getAttributes(obj) + '"';
60
+ }
61
+ else {
62
+ if (Array.isArray(obj)) {
63
+ obj.forEach(function (item) {
64
+ str += toXml(item, name, depth + 1);
65
+ });
32
66
  }
33
- xml += hasChild ? ">" : " />";
34
- if (hasChild) {
35
- for (var m in v) {
36
- if (m == "#text")
37
- xml += v[m];
38
- else if (m == "#cdata")
39
- xml += "<![CDATA[" + v[m] + "]]>";
40
- else if (m.charAt(0) != "-")
41
- xml += toXml(v[m], m, ind, deep + 1);
42
- }
43
- xml += addIndSpace(ind, deep) + "</" + name + ">";
67
+ else if (type(obj) === "[object Object]") {
68
+ var keys = Object.keys(obj);
69
+ var attributes_1 = "";
70
+ var children_1 = "";
71
+ str += (depth === 0 ? "" : tn + frontSpace) + "<" + name;
72
+ keys.forEach(function (k) {
73
+ k.charAt(0) === "-"
74
+ ? (attributes_1 += toXml(obj[k], k, depth + 1))
75
+ : (children_1 += toXml(obj[k], k, depth + 1));
76
+ });
77
+ str +=
78
+ attributes_1 +
79
+ (children_1 !== "" ? ">" + children_1 + (tn + frontSpace) + "</" + name + ">" : " />");
44
80
  }
45
81
  else {
46
- // xml += addIndSpace(ind, deep);
82
+ str += tn + frontSpace + ("<$" + name + ">" + obj.toString() + "</" + name + ">");
47
83
  }
48
84
  }
49
- else {
50
- xml += addIndSpace(ind, deep) + "<" + name + ">" + v.toString() + "</" + name + ">";
51
- }
52
- return xml;
85
+ return str;
53
86
  }
54
- ;
55
87
  function lfJson2Xml(o) {
56
- var xmlStr = "";
88
+ var xmlStr = "<LogicFlow>\t\n";
57
89
  for (var m in o) {
58
- xmlStr += toXml(o[m], m, "\t\n", 0);
90
+ xmlStr += toXml(o[m], m, 0);
59
91
  }
60
- return xmlStr;
92
+ return xmlStr + "\t\n</LogicFlow>";
61
93
  }
62
94
  exports.lfJson2Xml = lfJson2Xml;
@@ -106,35 +106,43 @@ XML.ObjTree.prototype.parseDOM = function (root) {
106
106
  tmp[root.nodeName] = json; // root nodeName
107
107
  json = tmp;
108
108
  }
109
- return json;
109
+ return json["LogicFlow"];
110
110
  };
111
111
  // method: parseElement( element )
112
+ /**
113
+ * @reference node type reference https://www.w3schools.com/xml/dom_nodetype.asp
114
+ */
112
115
  XML.ObjTree.prototype.parseElement = function (elem) {
113
- // COMMENT_NODE
116
+ // PROCESSING_INSTRUCTION_NODE
114
117
  if (elem.nodeType == 7) {
115
118
  return;
116
119
  }
117
- // TEXT_NODE CDATA_SECTION_NODE
118
- if (elem.nodeType == 3 || elem.nodeType == 4) {
120
+ // TEXT_NODE CDATA_SECTION_NODE COMMENT_NODE
121
+ if (elem.nodeType == 3 || elem.nodeType == 4 || elem.nodeType == 8) {
119
122
  var bool = elem.nodeValue.match(/[^\x00-\x20]/);
120
123
  if (bool == null)
121
124
  return; // ignore white spaces
122
125
  return elem.nodeValue;
123
126
  }
124
- var retVal;
127
+ var retVal = null;
125
128
  var cnt = {};
126
- // parse attributes
127
129
  if (elem.attributes && elem.attributes.length) {
128
130
  retVal = {};
129
131
  for (var i = 0; i < elem.attributes.length; i++) {
130
132
  var key = elem.attributes[i].nodeName;
131
- if (typeof (key) != "string")
133
+ if (typeof key != "string")
132
134
  continue;
133
135
  var val = elem.attributes[i].nodeValue;
136
+ try {
137
+ val = JSON.parse(elem.attributes[i].nodeValue.replace(/'/g, '"'));
138
+ }
139
+ catch (error) {
140
+ val = elem.attributes[i].nodeValue;
141
+ }
134
142
  if (!val)
135
143
  continue;
136
144
  key = this.attr_prefix + key;
137
- if (typeof (cnt[key]) == "undefined")
145
+ if (typeof cnt[key] == "undefined")
138
146
  cnt[key] = 0;
139
147
  cnt[key]++;
140
148
  this.addNode(retVal, key, cnt[key], val);
@@ -147,7 +155,7 @@ XML.ObjTree.prototype.parseElement = function (elem) {
147
155
  textOnly = false; // some attributes exists
148
156
  for (var i = 0; i < elem.childNodes.length && textOnly; i++) {
149
157
  var nType = elem.childNodes[i].nodeType;
150
- if (nType == 3 || nType == 4)
158
+ if (nType == 3 || nType == 4 || nType == 8)
151
159
  continue;
152
160
  textOnly = false;
153
161
  }
@@ -175,6 +183,12 @@ XML.ObjTree.prototype.parseElement = function (elem) {
175
183
  }
176
184
  }
177
185
  }
186
+ else {
187
+ // @see issue https://github.com/didi/LogicFlow/issues/1068
188
+ // if retVal is null, that means the elem doesn't have any attributes and children,
189
+ // the elem would be like: <a /> or <a></a>, so set retVal to empty object {}
190
+ retVal === null && (retVal = {});
191
+ }
178
192
  return retVal;
179
193
  };
180
194
  // method: addNode( hash, key, count, value )