Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit 6aaeedf

Browse files
author
Alexander Vakrilov
authored
refactor: emit warning for invalid & in XML (#1059)
1 parent 488730d commit 6aaeedf

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

Diff for: xml-namespace-loader.spec.ts

+37-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ interface TestSetup {
2020
expectedRegs: { name: string, path: string }[],
2121
ignore?: RegExp,
2222
assureNoDeps?: boolean,
23-
expectError?: boolean
23+
expectError?: boolean,
24+
expectWarnings?: number
2425
}
2526

2627
function getContext(
2728
done: DoneFn,
28-
{ resolveMap, expectedDeps, expectedRegs, assureNoDeps, ignore, expectError }: TestSetup) {
29+
{ resolveMap, expectedDeps, expectedRegs, assureNoDeps, ignore, expectError, expectWarnings }: TestSetup) {
2930
const actualDeps: string[] = [];
31+
const actualWarnings: Error[] =[]
3032
let callbackCalled = false;
3133

3234
const loaderContext = {
@@ -50,6 +52,10 @@ function getContext(
5052
expect(source).not.toContain("global.registerModule");
5153
}
5254

55+
if(expectWarnings){
56+
expect(actualWarnings.length).toEqual(expectWarnings);
57+
}
58+
5359
if (error && !expectError) {
5460
done.fail(error)
5561
} else if (!error && expectError) {
@@ -69,6 +75,9 @@ function getContext(
6975
addDependency: (dep: string) => {
7076
actualDeps.push(dep);
7177
},
78+
emitWarning: (err: Error) => {
79+
actualWarnings.push(err);
80+
},
7281
query: { ignore }
7382
}
7483

@@ -277,4 +286,30 @@ describe("XmlNamespaceLoader", () => {
277286

278287
xmlNsLoader.call(loaderContext, testXml);
279288
})
289+
290+
291+
it("with '&&', '||', '<=' and '>=' in binding expression, emits warnings, but does not fail", (done) => {
292+
const resolveMap = {
293+
"nativescript-ui-chart": "node_module/nativescript-ui-chart/ui-chart.js",
294+
}
295+
296+
const expectedDeps = [];
297+
298+
const expectedRegs = [
299+
{ name: "nativescript-ui-chart", path: "nativescript-ui-chart" },
300+
{ name: "nativescript-ui-chart/RadCartesianChart", path: "nativescript-ui-chart" },
301+
];
302+
303+
const testXml = `
304+
<Page xmlns="http://www.nativescript.org/tns.xsd">
305+
<StackLayout xmlns:chart="nativescript-ui-chart">
306+
<TextField text="{{ var1 && var2 || var1 >= var2 || var2 <= var1 }}" />
307+
<chart:RadCartesianChart></chart:RadCartesianChart>
308+
</StackLayout>
309+
</Page>`;
310+
311+
const loaderContext = getContext(done, { resolveMap, expectedDeps, expectedRegs, expectWarnings: 1 });
312+
313+
xmlNsLoader.call(loaderContext, testXml);
314+
})
280315
});

Diff for: xml-namespace-loader.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,17 @@ const loader: loader.Loader = function (source: string, map) {
103103

104104
saxParser.onopentag = (node: QualifiedTag) => { handleOpenTag(node.uri, node.local); };
105105
saxParser.onerror = (err) => {
106+
// Do only warning about invalid character "&"" for back-compatibility
107+
// as it is common to use it in a binding expression
108+
if (err &&
109+
err.message.indexOf("Invalid character") >= 0 &&
110+
err.message.indexOf("Char: &") >= 0) {
111+
this.emitWarning(err)
112+
} else {
113+
callbackWrapper(err);
114+
}
115+
106116
saxParser.error = null;
107-
callbackWrapper(err);
108117
};
109118
saxParser.write(source).close();
110119

0 commit comments

Comments
 (0)