Skip to content

Commit f399f6c

Browse files
EddyVerbruggenAlexander Vakrilov
authored andcommitted
Ability to load local files in a WebView on iOS devices (#4444)
* Ability to load local files in a WebView on iOS devices * Ability to load local files in a WebView on iOS devices (removed doc-comments in test)
1 parent 1420fa8 commit f399f6c

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>MyTitle</title>
5+
<meta charset="utf-8" />
6+
</head>
7+
<body>
8+
<span style="color:red">TestÖ with Spaces</span>
9+
</body>
10+
</html>

tests/app/ui/web-view/web-view-tests.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,40 @@ export class WebViewTest extends testModule.UITest<webViewModule.WebView> {
8787
// << webview-localfile
8888
}
8989

90+
public testLoadLocalFileWithSpaceInPath(done) {
91+
let webView = this.testView;
92+
93+
webView.on(webViewModule.WebView.loadFinishedEvent, function (args: webViewModule.LoadEventData) {
94+
let actual;
95+
let expectedTitle = 'MyTitle';
96+
let expectedHtml = '<span style="color:red">TestÖ with Spaces</span>';
97+
98+
if (webView.ios) {
99+
actual = webView.ios.stringByEvaluatingJavaScriptFromString("document.body.innerHTML").trim();
100+
} else if (webView.android) {
101+
actual = webView.android.getTitle();
102+
}
103+
104+
try {
105+
TKUnit.assertNull(args.error, args.error);
106+
TKUnit.assertEqual(actual, webView.ios ? expectedHtml : expectedTitle, "File ~/ui/web-view/test.html not loaded properly.");
107+
done(null);
108+
}
109+
catch (e) {
110+
done(e);
111+
}
112+
113+
let message;
114+
if (!args.error) {
115+
message = "WebView finished loading " + args.url;
116+
}
117+
else {
118+
message = "Error loading " + args.url + ": " + args.error;
119+
}
120+
});
121+
webView.src = "~/ui/web-view/test with spaces.html";
122+
}
123+
90124
public testLoadHTMLString(done) {
91125
let webView = this.testView;
92126

tns-core-modules/ui/web-view/web-view-common.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ export abstract class WebViewBase extends View implements WebViewDefinition {
7171
src = "file://" + src;
7272
}
7373

74+
// loading local files from paths with spaces may fail
75+
if (src.toLowerCase().indexOf("file:///") === 0) {
76+
src = encodeURI(src);
77+
}
78+
7479
if (src.toLowerCase().indexOf("http://") === 0 ||
7580
src.toLowerCase().indexOf("https://") === 0 ||
7681
src.toLowerCase().indexOf("file:///") === 0) {

0 commit comments

Comments
 (0)