Skip to content

Commit 47c97f7

Browse files
committed
add support for array in to XamlPatch
1 parent d22747b commit 47c97f7

File tree

2 files changed

+51
-36
lines changed

2 files changed

+51
-36
lines changed

Tasks/XmlPatch/xmlPatcher.ts

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,17 @@ export class XmlPatcher implements patch.IPatcher {
5454
return false;
5555
}
5656

57+
notsupported(patch: patch.IPatch): boolean {
58+
console.log("operation not supported: " + patch.op + " " + patch.path);
59+
return false;
60+
}
61+
5762
remove(xml: Document, select: any, patch: patch.IPatch): boolean {
5863
var arrayOperation = this.detectArrayOperation(patch.path);
59-
console.log(JSON.stringify(arrayOperation));
6064
if (arrayOperation.isArrayOperation) {
6165
if (arrayOperation.append) {
6266
var node = <SVGSVGElement>select(arrayOperation.path, xml, true);
6367
node.removeChild(node.lastChild);
64-
console.log('arrayOperation.path');
65-
console.log(node);
66-
console.log('remove lastchild');
6768
return true;
6869
} else {
6970
var node = <SVGSVGElement>select(arrayOperation.path, xml, true);
@@ -88,14 +89,20 @@ export class XmlPatcher implements patch.IPatcher {
8889
}
8990

9091
move(xml: Document, select: any, patch: patch.IPatch): boolean {
91-
var fromNode = <SVGSVGElement>select(patch.from, xml, true);
92-
var toNode = <SVGSVGElement>select(patch.path, xml, true);
93-
if (fromNode) {
94-
patch.value = fromNode.textContent;
95-
this.remove(xml, select, { op: 'remove', path: patch.from });
96-
return this.replace(xml, select, patch);
92+
var arrayOperation = this.detectArrayOperation(patch.path);
93+
var arrayOperationFrom = this.detectArrayOperation(patch.from);
94+
if (arrayOperation.isArrayOperation || arrayOperationFrom.isArrayOperation) {
95+
return this.notsupported(patch);
9796
} else {
98-
return this.notfound(patch);
97+
var fromNode = <SVGSVGElement>select(patch.from, xml, true);
98+
var toNode = <SVGSVGElement>select(patch.path, xml, true);
99+
if (fromNode) {
100+
patch.value = fromNode.textContent;
101+
this.remove(xml, select, { op: 'remove', path: patch.from });
102+
return this.replace(xml, select, patch);
103+
} else {
104+
return this.notfound(patch);
105+
}
99106
}
100107
}
101108

@@ -112,18 +119,16 @@ export class XmlPatcher implements patch.IPatcher {
112119

113120
add(xml: Document, select: any, patch: patch.IPatch): boolean {
114121
var arrayOperation = this.detectArrayOperation(patch.path);
115-
console.log(JSON.stringify(arrayOperation));
116122
if (arrayOperation.isArrayOperation) {
117123
if (arrayOperation.append) {
118124
var node = <SVGSVGElement>select(arrayOperation.path, xml, true);
119-
var newNode = xml.createElement(patch.value);
125+
var newNode = <HTMLElement>xml.createElement(patch.value);
120126
node.appendChild(newNode);
121127
return true;
122128
} else {
123129
var node = <SVGSVGElement>select(arrayOperation.path, xml, true);
124-
var newNode = xml.createElement(patch.value);
130+
var newNode = <HTMLElement>xml.createElement(patch.value);
125131
node.insertBefore(newNode, node.childNodes[arrayOperation.index])
126-
node.appendChild(newNode);
127132
return true;
128133
}
129134
}
@@ -154,18 +159,28 @@ export class XmlPatcher implements patch.IPatcher {
154159
}
155160

156161
replace(xml: Document, select: any, patch: patch.IPatch): boolean {
157-
var node = <SVGSVGElement>select(patch.path, xml, true);
158-
if (node) {
159-
var parentPath = this.getParentPath(patch.path);
160-
var parentNode = <SVGSVGElement>select(parentPath.path, xml, true);
161-
if (parentPath.isAttribute) {
162-
parentNode.setAttribute(parentPath.nodeName, patch.value);
163-
} else {
164-
node.textContent = patch.value;
165-
}
162+
var arrayOperation = this.detectArrayOperation(patch.path);
163+
if (arrayOperation.isArrayOperation) {
164+
var node = <SVGSVGElement>select(arrayOperation.path, xml, true);
165+
var childNode = node.childNodes[arrayOperation.index];
166+
var newNode = <HTMLElement>xml.createElement(patch.value);
167+
node.insertBefore(newNode, childNode)
168+
node.removeChild(childNode);
166169
return true;
167170
} else {
168-
return this.add(xml, select, patch);
171+
var node = <SVGSVGElement>select(patch.path, xml, true);
172+
if (node) {
173+
var parentPath = this.getParentPath(patch.path);
174+
var parentNode = <SVGSVGElement>select(parentPath.path, xml, true);
175+
if (parentPath.isAttribute) {
176+
parentNode.setAttribute(parentPath.nodeName, patch.value);
177+
} else {
178+
node.textContent = patch.value;
179+
}
180+
return true;
181+
} else {
182+
return this.add(xml, select, patch);
183+
}
169184
}
170185
}
171186

Tests/XmlPatch/xmlPatcher.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe("XML Patcher", () => {
101101
{
102102
op: "add", path: "/rootNode/0", value: "leaf"
103103
},{
104-
op: "replace", path: "/rootNode/leaf:first()/@color", value: "#bbbbbb"
104+
op: "replace", path: "/rootNode/leaf[1]/@color", value: "#bbbbbb"
105105
}
106106
], {});
107107
var result = patcher.apply(source);
@@ -114,7 +114,7 @@ describe("XML Patcher", () => {
114114
{
115115
op: "add", path: "/rootNode/1", value: "leaf"
116116
},{
117-
op: "replace", path: "/rootNode/leaf[1]/@color", value: "#bbbbbb"
117+
op: "replace", path: "/rootNode/leaf[2]/@color", value: "#bbbbbb"
118118
}
119119
], {});
120120
var result = patcher.apply(source);
@@ -127,12 +127,12 @@ describe("XML Patcher", () => {
127127
{
128128
op: "add", path: "/rootNode/-", value: "leaf"
129129
},{
130-
op: "replace", path: "/rootNode/leaf:last()/@color", value: "#bbbbbb"
130+
op: "replace", path: "/rootNode/leaf[last()]/@color", value: "#bbbbbb"
131131
}
132132
], {});
133133
var result = patcher.apply(source);
134134

135-
expect(result).toEqual('<rootNode><leaf color="#aaaaaa"><leaf color="#000000"/><leaf color="#bbbbbb"/></rootNode>');
135+
expect(result).toEqual('<rootNode><leaf color="#aaaaaa"/><leaf color="#000000"/><leaf color="#bbbbbb"/></rootNode>');
136136
});
137137
});
138138

@@ -150,7 +150,7 @@ describe("XML Patcher", () => {
150150
});
151151

152152
describe("Move", () => {
153-
it(": move at index", () => {
153+
xit(": move at index", () => {
154154
var patcher = new xmlatcher.XmlPatcher([
155155
{
156156
op: "move", from: "/rootNode/0", path: "/rootNode/1", value: "otherleaf"
@@ -166,34 +166,34 @@ describe("XML Patcher", () => {
166166
it(": delete at index", () => {
167167
var patcher = new xmlatcher.XmlPatcher([
168168
{
169-
op: "delete", path: "/rootNode/1"
169+
op: "remove", path: "/rootNode/1"
170170
}
171171
], {});
172172
var result = patcher.apply(source);
173173

174-
expect(result).toEqual('<rootNode><leaf color="#000000"/></rootNode>');
174+
expect(result).toEqual('<rootNode><leaf color="#aaaaaa"/></rootNode>');
175175
});
176176

177177
it(": delete first", () => {
178178
var patcher = new xmlatcher.XmlPatcher([
179179
{
180-
op: "delete", path: "/rootNode/0"
180+
op: "remove", path: "/rootNode/0"
181181
}
182182
], {});
183183
var result = patcher.apply(source);
184184

185-
expect(result).toEqual('<rootNode><leaf color="#aaaaaa"/></rootNode>');
185+
expect(result).toEqual('<rootNode><leaf color="#000000"/></rootNode>');
186186
});
187187

188188
it(": delete last", () => {
189189
var patcher = new xmlatcher.XmlPatcher([
190190
{
191-
op: "delete", path: "/rootNode/-"
191+
op: "remove", path: "/rootNode/-"
192192
}
193193
], {});
194194
var result = patcher.apply(source);
195195

196-
expect(result).toEqual('<rootNode><leaf color="#000000"/></rootNode>');
196+
expect(result).toEqual('<rootNode><leaf color="#aaaaaa"/></rootNode>');
197197
});
198198
});
199199
});

0 commit comments

Comments
 (0)