Skip to content
This repository was archived by the owner on Jan 10, 2024. It is now read-only.

Commit 4c5f5ed

Browse files
committed
Merge branch 'master' of github.com:Notalib/nativescript-webview-ext
2 parents 8c1a323 + c75ddb2 commit 4c5f5ed

File tree

11 files changed

+218
-116
lines changed

11 files changed

+218
-116
lines changed

src/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nota/nativescript-webview-ext",
3-
"version": "0.3.3",
3+
"version": "0.4.1",
44
"description": "Extended WebView for NativeScript which adds 'x-local' scheme for local-files. events between WebView and native-layer, javascript execution, injecting CSS and JS-files.",
55
"main": "webview-ext",
66
"typings": "webview-ext.d.ts",

src/webview-ext-common.ts

+19-16
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,11 @@ export class WebViewExtBase extends View {
567567
const reqId = `${Math.round(Math.random() * 1000)}`;
568568
const eventName = `tmp-promise-event-${reqId}`;
569569

570-
const scriptHeader = `(function() {
570+
const scriptHeader = `
571571
var promises = [];
572572
var p = Promise.resolve();
573573
var i = 0;
574-
`;
574+
`.trim();
575575

576576
const scriptBody = [] as string[];
577577

@@ -581,27 +581,30 @@ export class WebViewExtBase extends View {
581581
return ${scriptCode.trim()};
582582
});
583583
promises.push(p);
584-
`);
584+
`.trim());
585585
}
586586

587587
const scriptFooter = `
588588
return Promise.all(promises);
589-
})()`;
589+
`.trim();
590590

591-
const scriptCode = `${scriptHeader}
592-
${scriptBody.join(';')}
593-
${scriptFooter}`;
594-
const trimmedScriptCode = scriptCode.trim();
591+
const scriptCode = `(function() {
592+
${scriptHeader}
593+
${scriptBody.join(';')}
594+
${scriptFooter}
595+
})()`.trim();
595596

596597
const promiseScriptCode = `
597-
var eventName = ${JSON.stringify(eventName)};
598-
try {
599-
var promise = (function() {return ${trimmedScriptCode}})();
600-
window.nsWebViewBridge.executePromise(promise, eventName);
601-
} catch (err) {
602-
window.nsWebViewBridge.emitError(err, eventName);
603-
}
604-
`;
598+
(function() {
599+
var eventName = ${JSON.stringify(eventName)};
600+
try {
601+
var promise = (function() {return ${scriptCode}})();
602+
window.nsWebViewBridge.executePromise(promise, eventName);
603+
} catch (err) {
604+
window.nsWebViewBridge.emitError(err, eventName);
605+
}
606+
})();
607+
`.trim();
605608

606609
return new Promise<T>((resolve, reject) => {
607610
let timer: any;

src/webview-ext.ios.ts

+32-8
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,30 @@ export class WebViewExt extends WebViewExtBase {
9393
}
9494

9595
public executeJavaScript<T>(scriptCode: string, stringifyResult = true): Promise<T> {
96+
scriptCode = scriptCode.trim();
97+
9698
if (stringifyResult) {
9799
scriptCode = `
98-
var result = ${scriptCode.trim()};
100+
(function(window) {
101+
var result = null;
102+
103+
try {
104+
result = ${scriptCode.trim()};
105+
} catch (err) {
106+
return JSON.stringify({
107+
error: true,
108+
message: err.message,
109+
stack: err.stack
110+
});
111+
}
99112
100-
try {
101-
JSON.stringify(result);
102-
} catch (err) {
103-
result;
104-
}
105-
`;
113+
try {
114+
return JSON.stringify(result);
115+
} catch (err) {
116+
return result;
117+
}
118+
})(window);
119+
`.trim();
106120
}
107121

108122
return new Promise((resolve, reject) => {
@@ -123,7 +137,17 @@ export class WebViewExt extends WebViewExtBase {
123137
}
124138
}
125139
})
126-
.then((result): T => this.parseWebViewJavascriptResult(result));
140+
.then((result): T => this.parseWebViewJavascriptResult(result))
141+
.then((result) => {
142+
const r = result as any;
143+
if (r && typeof r === 'object' && r.error) {
144+
const error = new Error(r.message);
145+
(error as any).webStack = r.stack;
146+
return Promise.reject(error);
147+
}
148+
149+
return Promise.resolve(result);
150+
});
127151
}
128152

129153
@profile

tests/app/App_Resources/iOS/Info.plist

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<key>CFBundleDevelopmentRegion</key>
66
<string>en</string>
77
<key>CFBundleDisplayName</key>
8-
<string>$WebViewExt - UnitTestApp</string>
8+
<string>WebViewExt - UnitTestApp</string>
99
<key>CFBundleExecutable</key>
1010
<string>${EXECUTABLE_NAME}</string>
1111
<key>CFBundleIconFile</key>

tests/app/TKUnit.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const sdkVersion = parseInt(platform.device.sdkVersion);
2323

2424
trace.enable();
2525

26+
const env = require('./environment.json');
27+
console.log(env);
28+
2629
export interface TestInfoEntry {
2730
testFunc: () => void;
2831
instance: Object;
@@ -45,7 +48,7 @@ export function time(): number {
4548

4649
export function write(message: string, type?: number) {
4750
//console.log(message);
48-
trace.write(message, trace.categories.Test, type);
51+
trace.write(`${env.testUUID || ''} ${message}`.trim(), trace.categories.Test, type);
4952
}
5053

5154
function runTest(testInfo: TestInfoEntry) {

tests/app/app/mainPage.ts

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1-
import * as trace from "tns-core-modules/trace";
2-
import { Page } from "tns-core-modules/ui/page";
1+
import { Page } from "tns-core-modules/ui/page";
32
import * as tests from "../testRunner";
43

5-
trace.enable();
6-
trace.addCategories(trace.categories.Test + "," + trace.categories.Error);
7-
8-
// When debugging
9-
// trace.setCategories(trace.categories.concat(
10-
// trace.categories.Test,
11-
// trace.categories.Navigation,
12-
// trace.categories.Transition,
13-
// trace.categories.NativeLifecycle,
14-
// trace.categories.ViewHierarchy,
15-
// trace.categories.VisualTreeEvents
16-
// ));
17-
184
function runTests() {
195
setTimeout(() => tests.runAll(''), 10);
206
}

tests/app/app/testRunnerPage.ts

-59
This file was deleted.

tests/app/testRunner.ts

+35-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
1-
/* tslint:disable:ordered-imports */
2-
/* tslint:disable:prefer-template */
3-
import * as TKUnit from "./TKUnit";
1+
/* tslint:disable:prefer-template */
2+
import { exit } from 'nativescript-exit';
43
import { _resetRootView, getRootView } from "tns-core-modules/application";
4+
import * as fs from "tns-core-modules/file-system";
5+
import * as platform from "tns-core-modules/platform";
6+
import * as trace from "tns-core-modules/trace";
57
import { messageType } from "tns-core-modules/trace";
6-
import { topmost, Frame, NavigationEntry } from "tns-core-modules/ui/frame";
7-
import { Page } from "tns-core-modules/ui/page";
8-
import { TextView } from "tns-core-modules/ui/text-view";
98
import { Button } from "tns-core-modules/ui/button";
10-
import { StackLayout } from "tns-core-modules/ui/layouts/stack-layout";
11-
import * as platform from "tns-core-modules/platform";
12-
import "./ui-test";
13-
import * as fs from "tns-core-modules/file-system";
149
import { unsetValue } from "tns-core-modules/ui/core/properties";
10+
import { Frame, NavigationEntry, topmost } from "tns-core-modules/ui/frame";
11+
import { StackLayout } from "tns-core-modules/ui/layouts/stack-layout";
12+
import { Page } from "tns-core-modules/ui/page";
13+
import { TextView } from "tns-core-modules/ui/text-view";
1514
import { ad } from "tns-core-modules/utils/utils";
16-
import { exit } from 'nativescript-exit';
15+
16+
import * as TKUnit from "./TKUnit";
17+
import "./ui-test";
18+
19+
// When debugging
20+
// trace.setCategories(trace.categories.concat(
21+
// trace.categories.Test,
22+
// trace.categories.Navigation,
23+
// trace.categories.Transition,
24+
// trace.categories.NativeLifecycle,
25+
// trace.categories.ViewHierarchy,
26+
// trace.categories.VisualTreeEvents
27+
// ));
1728

1829
const env = require('./environment.json');
1930

31+
const traceCategories = [
32+
trace.categories.Test,
33+
trace.categories.Error,
34+
];
35+
36+
if (!env.ci) {
37+
traceCategories.push("NOTA");
38+
}
39+
40+
trace.enable();
41+
trace.addCategories(traceCategories.join(","));
42+
2043
Frame.defaultAnimatedNavigation = false;
2144

2245
export function isRunningOnEmulator(): boolean {
@@ -212,7 +235,7 @@ export function runAll(testSelector?: string) {
212235
}
213236
}
214237

215-
console.log(`TESTS: ${singleModuleName || ''} ${singleTestName || ''}`);
238+
TKUnit.write(`TESTS: ${singleModuleName || ''} ${singleTestName || ''}`);
216239

217240
const totalSuccess = 0;
218241
const totalFailed: Array<TKUnit.TestFailure> = [];

tests/app/ui-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class UITest<T extends View> implements trace.TraceWriter {
3232
}
3333

3434
public create(): T {
35-
throw new Error(this + " should implement Create method.");
35+
throw new Error(`${this} should implement Create method.`);
3636
}
3737

3838
public setUpModule(): void {

tests/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
"url": "^0.11.0"
2222
},
2323
"devDependencies": {
24+
"@types/byline": "^4.2.31",
2425
"@types/node": "^10.1.2",
2526
"babel-traverse": "6.9.0",
2627
"babel-types": "6.9.0",
2728
"babylon": "6.8.0",
29+
"byline": "^5.0.0",
2830
"filewalker": "0.1.2",
2931
"lazy": "1.0.11",
3032
"nativescript-dev-typescript": "^0.7.1",
@@ -33,6 +35,7 @@
3335
"ts-node": "^6.0.3",
3436
"tslib": "^1.9.1",
3537
"tslint": "^5.10.0",
36-
"typescript": "~2.6.1"
38+
"typescript": "~2.6.1",
39+
"uuid": "^3.2.1"
3740
}
3841
}

0 commit comments

Comments
 (0)