FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge pull request #179 from NativeScript/webview-src · NativeScript/NativeScript@ae2f4df · GitHub

Commit ae2f4df

Browse files
Vladimir Enchev
committed
Merge pull request #179 from NativeScript/webview-src
Implemented #172.
2 parents 75253f0 + 9a5a9c7 commit ae2f4df

11 files changed

Lines changed: 257 additions & 18 deletions

File tree

‎apps/tests/ui/web-view/test.html‎

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</span>
9+
</body>
10+
</html>

‎apps/tests/ui/web-view/web-view-tests.ts‎

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import webViewModule = require("ui/web-view");
1313
// ### Declaring a WebView.
1414
//```XML
1515
// <Page>
16-
// <WebView url="{{ someUrl }}" />
16+
// <WebView src="{{ someUrl | pathToLocalFile | htmlString }}" />
1717
// </Page>
1818
//```
19+
1920
// </snippet>
2021

2122
var _createWebViewFunc = function (): webViewModule.WebView {
@@ -76,6 +77,128 @@ export var testLoadExistingUrl = function () {
7677
}
7778
}
7879

80+
export var testLoadLocalFile = function () {
81+
var newPage: page.Page;
82+
var webView = _createWebViewFunc();
83+
var pageFactory = function (): page.Page {
84+
newPage = new page.Page();
85+
newPage.content = webView;
86+
return newPage;
87+
};
88+
89+
helper.navigate(pageFactory);
90+
91+
var testFinished = false;
92+
var actualHtml;
93+
var actualTitle;
94+
var actualError;
95+
96+
var expectedTitle = 'MyTitle';
97+
var expectedHtml = '<span style="color:red">Test</span>';
98+
99+
// <snippet module="ui/web-view" title="WebView">
100+
// ### Using WebView
101+
// ``` JavaScript
102+
webView.on(webViewModule.WebView.loadFinishedEvent, function (args: webViewModule.LoadEventData) {
103+
// <hide>
104+
if (webView.ios) {
105+
actualHtml = webView.ios.stringByEvaluatingJavaScriptFromString("document.body.innerHTML").trim();
106+
} else if (webView.android) {
107+
actualTitle = webView.android.getTitle()
108+
}
109+
110+
actualError = args.error;
111+
testFinished = true;
112+
// </hide>
113+
var 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+
//console.log(message);
121+
});
122+
webView.src = "~/ui/web-view/test.html";
123+
124+
TKUnit.wait(4);
125+
126+
helper.goBack();
127+
128+
if (testFinished) {
129+
if (webView.ios) {
130+
TKUnit.assert(actualHtml === expectedHtml, "File ~/ui/web-view/test.html not loaded properly. Actual: " + actualHtml);
131+
} else if (webView.android) {
132+
TKUnit.assert(actualTitle === expectedTitle, "File ~/ui/web-view/test.html not loaded properly. Actual: " + actualTitle);
133+
}
134+
TKUnit.assert(actualError === undefined, actualError);
135+
}
136+
else {
137+
TKUnit.assert(false, "TIMEOUT");
138+
}
139+
}
140+
141+
export var testLoadHTMLString = function () {
142+
var newPage: page.Page;
143+
var webView = _createWebViewFunc();
144+
var pageFactory = function (): page.Page {
145+
newPage = new page.Page();
146+
newPage.content = webView;
147+
return newPage;
148+
};
149+
150+
helper.navigate(pageFactory);
151+
152+
var testFinished = false;
153+
var actualHtml;
154+
var actualTitle;
155+
var actualError;
156+
157+
var expectedTitle = 'MyTitle';
158+
var expectedHtml = '<span style="color:red">Test</span>';
159+
160+
// <snippet module="ui/web-view" title="WebView">
161+
// ### Using WebView
162+
// ``` JavaScript
163+
webView.on(webViewModule.WebView.loadFinishedEvent, function (args: webViewModule.LoadEventData) {
164+
// <hide>
165+
if (webView.ios) {
166+
actualHtml = webView.ios.stringByEvaluatingJavaScriptFromString("document.body.innerHTML").trim();
167+
} else if (webView.android) {
168+
actualTitle = webView.android.getTitle()
169+
}
170+
171+
actualError = args.error;
172+
testFinished = true;
173+
// </hide>
174+
var message;
175+
if (!args.error) {
176+
message = "WebView finished loading " + args.url;
177+
}
178+
else {
179+
message = "Error loading " + args.url + ": " + args.error;
180+
}
181+
//console.log(message);
182+
});
183+
webView.src = '<!DOCTYPE html><html><head><title>MyTitle</title><meta charset="utf-8" /></head><body><span style="color:red">Test</span></body></html>';
184+
185+
TKUnit.wait(4);
186+
187+
helper.goBack();
188+
189+
if (testFinished) {
190+
if (webView.ios) {
191+
TKUnit.assert(actualHtml === expectedHtml, "HTML string not loaded properly. Actual: " + actualHtml);
192+
} else if (webView.android) {
193+
TKUnit.assert(actualTitle === expectedTitle, "HTML string not loaded properly. Actual: " + actualTitle);
194+
}
195+
TKUnit.assert(actualError === undefined, actualError);
196+
}
197+
else {
198+
TKUnit.assert(false, "TIMEOUT");
199+
}
200+
}
201+
79202
export var testLoadInvalidUrl = function () {
80203
var newPage: page.Page;
81204
var webView = _createWebViewFunc();

‎apps/tests/xml-declaration/mainPage.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<TabViewItem.view>
88
<StackLayout>
99

10+
<WebView src="&lt;html&gt;&lt;body&gt;&lt;span style='color:red'&gt;Test&lt;/span&gt;&lt;/body&gt;&lt;/html&gt;" />
11+
1012
<!--<ToolBar>
1113
<ToolBar.items>
1214
<ToolBarItem>

‎image-source/image-source-common.ts‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import http = require("http");
2-
import types = require("utils/types");
2+
import utils = require("utils/utils");
33

44
// This is used for definition purposes only, it does not generate JavaScript for it.
55
import definition = require("image-source");
66

7-
var RESOURCE_PREFIX = "res://";
8-
97
export function fromResource(name: string): definition.ImageSource {
108
var image = new definition.ImageSource();
119
return image.loadFromResource(name) ? image : null;
@@ -40,18 +38,12 @@ export function fromFileOrResource(path: string): definition.ImageSource {
4038
throw new Error("Path \"" + "\" is not a valid file or resource.");
4139
}
4240

43-
if (path.indexOf(RESOURCE_PREFIX) === 0) {
44-
return fromResource(path.substr(RESOURCE_PREFIX.length));
41+
if (path.indexOf(utils.RESOURCE_PREFIX) === 0) {
42+
return fromResource(path.substr(utils.RESOURCE_PREFIX.length));
4543
}
4644
return fromFile(path);
4745
}
4846

4947
export function isFileOrResourcePath(path: string): boolean {
50-
if (!types.isString(path)) {
51-
return false;
52-
}
53-
54-
return path.indexOf("~/") === 0 || // relative to AppRoot
55-
path.indexOf("/") === 0 || // absolute path
56-
path.indexOf(RESOURCE_PREFIX) === 0; // resource
48+
return utils.isFileOrResourcePath(path);
5749
}

‎image-source/image-source.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ declare module "image-source" {
118118
export function fromFileOrResource(path: string): ImageSource;
119119

120120
/**
121-
* Returns true if the specified path points to a resource or local file.
121+
* [Obsolete. Please use utils.isFileOrResourcePath instead!] Returns true if the specified path points to a resource or local file.
122122
* @param path The path.
123123
*/
124124
export function isFileOrResourcePath(path: string): boolean

‎ui/web-view/web-view-common.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,31 @@ function onUrlPropertyChanged(data: dependencyObservable.PropertyChangeData) {
2222
// register the setNativeValue callback
2323
(<proxy.PropertyMetadata>urlProperty.metadata).onSetNativeValue = onUrlPropertyChanged;
2424

25+
var srcProperty = new dependencyObservable.Property(
26+
"src",
27+
"WebView",
28+
new proxy.PropertyMetadata("")
29+
);
30+
31+
function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) {
32+
var webView = <WebView>data.object;
33+
34+
if (webView._suspendLoading) {
35+
return;
36+
}
37+
38+
webView._loadSrc(data.newValue);
39+
}
40+
41+
// register the setNativeValue callback
42+
(<proxy.PropertyMetadata>srcProperty.metadata).onSetNativeValue = onSrcPropertyChanged;
43+
2544
export class WebView extends view.View implements definition.WebView {
2645
public static loadStartedEvent = "loadStarted";
2746
public static loadFinishedEvent = "loadFinished";
2847

2948
public static urlProperty = urlProperty;
49+
public static srcProperty = srcProperty;
3050

3151
public _suspendLoading: boolean;
3252

@@ -42,6 +62,14 @@ export class WebView extends view.View implements definition.WebView {
4262
this._setValue(WebView.urlProperty, value);
4363
}
4464

65+
get src(): string {
66+
return this._getValue(WebView.srcProperty);
67+
}
68+
69+
set src(value: string) {
70+
this._setValue(WebView.srcProperty, value);
71+
}
72+
4573
public _onLoadFinished(url: string, error?: string) {
4674

4775
this._suspendLoading = true;
@@ -73,6 +101,10 @@ export class WebView extends view.View implements definition.WebView {
73101
throw new Error("This member is abstract.");
74102
}
75103

104+
public _loadSrc(src: string) {
105+
throw new Error("This member is abstract.");
106+
}
107+
76108
get canGoBack(): boolean {
77109
throw new Error("This member is abstract.");
78110
}

‎ui/web-view/web-view.android.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import common = require("ui/web-view/web-view-common");
22
import trace = require("trace");
3+
import utils = require("utils/utils");
4+
import fs = require("file-system");
35

46
declare var exports;
57
require("utils/module-merge").merge(common, exports);
@@ -74,6 +76,31 @@ export class WebView extends common.WebView {
7476
this._android.loadUrl(url);
7577
}
7678

79+
public _loadSrc(src: string) {
80+
trace.write("WebView._loadSrc(" + src + ")", trace.categories.Debug);
81+
82+
this._android.stopLoading();
83+
this._android.loadUrl("about:blank");
84+
85+
if (utils.isFileOrResourcePath(src)) {
86+
87+
if (src.indexOf("~/") === 0) {
88+
src = fs.path.join(fs.knownFolders.currentApp().path, src.replace("~/", ""));
89+
}
90+
91+
var file = fs.File.fromPath(src);
92+
if (file) {
93+
file.readText().then((r) => {
94+
this._android.loadData(r, "text/html", null);
95+
});
96+
}
97+
} else if (src.indexOf("http://") === 0 || src.indexOf("https://") === 0) {
98+
this._android.loadUrl(src);
99+
} else {
100+
this._android.loadData(src, "text/html", null);
101+
}
102+
}
103+
77104
get canGoBack(): boolean {
78105
return this._android.canGoBack();
79106
}

‎ui/web-view/web-view.d.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ declare module "ui/web-view" {
3636
ios: UIWebView;
3737

3838
/**
39-
* Gets or sets the url displayed by this instance.
39+
* [Obsolete. Please use src instead!] Gets or sets the url displayed by this instance.
4040
*/
4141
url: string;
4242

43+
/**
44+
* Gets or sets the url, local file path or HTML string.
45+
*/
46+
src: string;
47+
4348
/**
4449
* Gets a value indicating whether the WebView can navigate back.
4550
*/

‎ui/web-view/web-view.ios.ts‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import common = require("ui/web-view/web-view-common");
22
import trace = require("trace");
3+
import utils = require("utils/utils");
4+
import fs = require("file-system");
35

46
declare var exports;
57
require("utils/module-merge").merge(common, exports);
@@ -17,16 +19,16 @@ class UIWebViewDelegateImpl extends NSObject implements UIWebViewDelegate {
1719
this._owner = owner;
1820
return this;
1921
}
20-
22+
2123
public webViewShouldStartLoadWithRequestNavigationType(webView: UIWebView, request: NSURLRequest, navigationType: number) {
2224
if (request.URL) {
2325
trace.write("UIWebViewDelegateClass.webViewShouldStartLoadWithRequestNavigationType(" + request.URL.absoluteString + ", " + navigationType + ")", trace.categories.Debug);
2426
this._owner._onLoadStarted(request.URL.absoluteString);
2527
}
26-
28+
2729
return true;
2830
}
29-
31+
3032
public webViewDidStartLoad(webView: UIWebView) {
3133
trace.write("UIWebViewDelegateClass.webViewDidStartLoad(" + webView.request.URL + ")", trace.categories.Debug);
3234
}
@@ -81,6 +83,32 @@ export class WebView extends common.WebView {
8183
this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(url)));
8284
}
8385

86+
public _loadSrc(src: string) {
87+
trace.write("WebView._loadSrc(" + src + ")", trace.categories.Debug);
88+
89+
if (this._ios.loading) {
90+
this._ios.stopLoading();
91+
}
92+
93+
if (utils.isFileOrResourcePath(src)) {
94+
95+
if (src.indexOf("~/") === 0) {
96+
src = fs.path.join(fs.knownFolders.currentApp().path, src.replace("~/", ""));
97+
}
98+
99+
var file = fs.File.fromPath(src);
100+
if (file) {
101+
file.readText().then((r) => {
102+
this._ios.loadHTMLStringBaseURL(r, null);
103+
});
104+
}
105+
} else if (src.indexOf("http://") === 0 || src.indexOf("https://") === 0) {
106+
this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(src)));
107+
} else {
108+
this._ios.loadHTMLStringBaseURL(src, null);
109+
}
110+
}
111+
84112
get canGoBack(): boolean {
85113
return this._ios.canGoBack;
86114
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL