Skip to content

Commit a1af9d9

Browse files
committed
leading slash check #34
1 parent 174f3fc commit a1af9d9

File tree

5 files changed

+80
-9
lines changed

5 files changed

+80
-9
lines changed

Common/Node/jsonPatcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var jsonPatch = require('fast-json-patch');
44

55
export class JsonPatcher implements patch.IPatcher {
66
constructor(
7-
private patches: patch.IPatch[]
7+
public patches: patch.IPatch[]
88
) {
99
}
1010

Common/Node/patch.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface IPatch{
88
}
99

1010
export interface IPatcher {
11+
patches: IPatch[];
1112
apply(content: string): string;
1213
}
1314

Common/Node/patchProcess.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ export function apply(patcher: patch.IPatcher, workingDirectory: string, filters
2424
outputPatchedFile: boolean, failIfNoPatchApplied: boolean, skipErrors: boolean) {
2525
var files = matcher.getMatches(workingDirectory, filters);
2626

27+
for (var index = 0; index < patcher.patches.length; index++) {
28+
var patch = patcher.patches[index];
29+
if (patch.path && patch.path[0] != '/'
30+
|| patch.from && patch.from[0] != '/')
31+
{
32+
throw new Error("All path must start with a leading slash. Please verify patch at index " + String(index));
33+
}
34+
}
35+
2736
tl.debug("Attempt to patch " + String(files.length) + "files");
2837

2938
var filePatched = 0;

Tasks/XmlPatch/xmlPatcher.ts

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,39 @@ import XRegExp = require('xregexp');
55

66
export class XmlPatcher implements patch.IPatcher {
77
constructor(
8-
private patches: patch.IPatch[],
8+
public patches: patch.IPatch[],
99
private namespaces: { [tag: string]: string }
1010
) {
1111
}
1212

13+
detectArrayOperation(path: string): { path: string, isArrayOperation: boolean, append?: boolean, index?: number } {
14+
var lastSlash = path.lastIndexOf('/');
15+
var lastFragment = path.substr(lastSlash + 1);
16+
var remainingPath = path.substr(0, lastSlash);
17+
18+
console.log(lastFragment);
19+
if (lastFragment == '-') {
20+
return {
21+
path: remainingPath,
22+
isArrayOperation: true,
23+
append: true
24+
};
25+
}
26+
27+
if (XRegExp.match(lastFragment, /^\d+$/g)) {
28+
return {
29+
path: remainingPath,
30+
isArrayOperation: true,
31+
index: parseInt(lastFragment)
32+
};
33+
}
34+
35+
return {
36+
path: path,
37+
isArrayOperation: false
38+
};
39+
}
40+
1341
getParentPath(path: string): { path: string, nodeName: string, isAttribute: boolean } {
1442
var lastSlash = path.lastIndexOf('/');
1543
var nodeName = path.substr(lastSlash + 1);
@@ -27,11 +55,28 @@ export class XmlPatcher implements patch.IPatcher {
2755
}
2856

2957
remove(xml: Document, select: any, patch: patch.IPatch): boolean {
58+
var arrayOperation = this.detectArrayOperation(patch.path);
59+
console.log(JSON.stringify(arrayOperation));
60+
if (arrayOperation.isArrayOperation) {
61+
if (arrayOperation.append) {
62+
var node = <SVGSVGElement>select(arrayOperation.path, xml, true);
63+
node.removeChild(node.lastChild);
64+
console.log('arrayOperation.path');
65+
console.log(node);
66+
console.log('remove lastchild');
67+
return true;
68+
} else {
69+
var node = <SVGSVGElement>select(arrayOperation.path, xml, true);
70+
node.removeChild(node.childNodes[arrayOperation.index]);
71+
return true;
72+
}
73+
}
74+
3075
var node = <SVGSVGElement>select(patch.path, xml, true);
3176
if (node) {
3277
var parentPath = this.getParentPath(patch.path);
3378
var parentNode = <SVGSVGElement>select(parentPath.path, xml, true);
34-
if (parentPath.isAttribute){
79+
if (parentPath.isAttribute) {
3580
parentNode.removeAttribute(parentPath.nodeName);
3681
} else {
3782
node.parentNode.removeChild(node);
@@ -47,7 +92,7 @@ export class XmlPatcher implements patch.IPatcher {
4792
var toNode = <SVGSVGElement>select(patch.path, xml, true);
4893
if (fromNode) {
4994
patch.value = fromNode.textContent;
50-
this.remove(xml, select, { op: 'remove', path : patch.from });
95+
this.remove(xml, select, { op: 'remove', path: patch.from });
5196
return this.replace(xml, select, patch);
5297
} else {
5398
return this.notfound(patch);
@@ -66,6 +111,23 @@ export class XmlPatcher implements patch.IPatcher {
66111
}
67112

68113
add(xml: Document, select: any, patch: patch.IPatch): boolean {
114+
var arrayOperation = this.detectArrayOperation(patch.path);
115+
console.log(JSON.stringify(arrayOperation));
116+
if (arrayOperation.isArrayOperation) {
117+
if (arrayOperation.append) {
118+
var node = <SVGSVGElement>select(arrayOperation.path, xml, true);
119+
var newNode = xml.createElement(patch.value);
120+
node.appendChild(newNode);
121+
return true;
122+
} else {
123+
var node = <SVGSVGElement>select(arrayOperation.path, xml, true);
124+
var newNode = xml.createElement(patch.value);
125+
node.insertBefore(newNode, node.childNodes[arrayOperation.index])
126+
node.appendChild(newNode);
127+
return true;
128+
}
129+
}
130+
69131
var node = <SVGSVGElement>select(patch.path, xml, true);
70132
if (node) {
71133
node.textContent = patch.value;
@@ -96,10 +158,10 @@ export class XmlPatcher implements patch.IPatcher {
96158
if (node) {
97159
var parentPath = this.getParentPath(patch.path);
98160
var parentNode = <SVGSVGElement>select(parentPath.path, xml, true);
99-
if (parentPath.isAttribute){
161+
if (parentPath.isAttribute) {
100162
parentNode.setAttribute(parentPath.nodeName, patch.value);
101163
} else {
102-
node.textContent = patch.value;
164+
node.textContent = patch.value;
103165
}
104166
return true;
105167
} else {
@@ -135,12 +197,12 @@ export class XmlPatcher implements patch.IPatcher {
135197
} else if (patch.op == 'test') {
136198
operation = this.test.bind(this);
137199
}
138-
200+
139201
if (!operation(xml, select, patch)) {
140202
throw new Error("Failed to patch xml file");
141203
}
142204
}
143-
205+
144206
return new xmldom.XMLSerializer().serializeToString(xml);
145207
}
146208
}

Tests/XmlPatch/xmlPatcher.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ describe("XML Patcher", () => {
219219
}
220220
], {});
221221
var result = patcher.apply(source);
222-
console.log(result);
223222
expect(result).not.toContain('#should_be_replaced#');
224223
});
225224
});

0 commit comments

Comments
 (0)