Merge pull request #821 from NativeScript/atanasovg/webview-fix

Extracts common logic into web-view-common. The check for http/https …
This commit is contained in:
Vladimir Enchev
2015-10-01 09:14:33 +03:00
7 changed files with 133 additions and 97 deletions

View File

@ -188,6 +188,7 @@
</TypeScriptCompile>
<TypeScriptCompile Include="apps\ui-tests-app\pages\handlers.ts" />
<TypeScriptCompile Include="apps\ui-tests-app\pages\htmlview.ts" />
<TypeScriptCompile Include="color\known-colors.d.ts" />
<TypeScriptCompile Include="ui\animation\animation.d.ts" />
<TypeScriptCompile Include="ui\animation\animation-common.ts">
<DependentUpon>animation.d.ts</DependentUpon>
@ -577,6 +578,11 @@
<TypeScriptCompile Include="ui\action-bar\action-bar.ios.ts">
<DependentUpon>action-bar.d.ts</DependentUpon>
</TypeScriptCompile>
<TypeScriptCompile Include="ui\builder\binding-builder.d.ts" />
<TypeScriptCompile Include="ui\builder\special-properties.d.ts" />
<TypeScriptCompile Include="ui\builder\special-properties.ts" />
<TypeScriptCompile Include="ui\styling\style-scope.d.ts" />
<TypeScriptCompile Include="ui\styling\style.d.ts" />
<TypeScriptCompile Include="ui\ui.d.ts" />
<TypeScriptCompile Include="ui\html-view\html-view-common.ts" />
<TypeScriptCompile Include="ui\html-view\html-view.android.ts" />
@ -1923,6 +1929,7 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="ui\package.json" />
<Content Include="tsconfig.json" />
</ItemGroup>
<ItemGroup>
<None Include="build\tslint.json" />

View File

@ -30,7 +30,7 @@ var _createWebViewFunc = function (): webViewModule.WebView {
return webView;
}
export var testLoadExistingUrl = function () {
function prepare(): webViewModule.WebView {
var newPage: page.Page;
var webView = _createWebViewFunc();
var pageFactory = function (): page.Page {
@ -41,6 +41,12 @@ export var testLoadExistingUrl = function () {
helper.navigate(pageFactory);
return webView;
}
export var testLoadExistingUrl = function () {
var webView = prepare();
var testFinished = false;
var actualUrl;
var actualError;
@ -80,15 +86,7 @@ export var testLoadExistingUrl = function () {
}
export var testLoadLocalFile = function () {
var newPage: page.Page;
var webView = _createWebViewFunc();
var pageFactory = function (): page.Page {
newPage = new page.Page();
newPage.content = webView;
return newPage;
};
helper.navigate(pageFactory);
var webView = prepare();
var testFinished = false;
var actualHtml;
@ -142,15 +140,7 @@ export var testLoadLocalFile = function () {
}
export var testLoadHTMLString = function () {
var newPage: page.Page;
var webView = _createWebViewFunc();
var pageFactory = function (): page.Page {
newPage = new page.Page();
newPage.content = webView;
return newPage;
};
helper.navigate(pageFactory);
var webView = prepare();
var testFinished = false;
var actualHtml;
@ -204,15 +194,7 @@ export var testLoadHTMLString = function () {
}
export var testLoadInvalidUrl = function () {
var newPage: page.Page;
var webView = _createWebViewFunc();
var pageFactory = function (): page.Page {
newPage = new page.Page();
newPage.content = webView;
return newPage;
};
helper.navigate(pageFactory);
var webView = prepare();
var testFinished = false;
var actualError;
@ -237,3 +219,31 @@ export var testLoadInvalidUrl = function () {
TKUnit.assert(false, "TIMEOUT");
}
}
export var testLoadUpperCaseSrc = function () {
var webView = prepare();
var testFinished = false;
var actualSrc;
var actualError;
webView.on(webViewModule.WebView.loadFinishedEvent, function (args: webViewModule.LoadEventData) {
actualSrc = args.url;
actualError = args.error;
testFinished = true;
});
var targetSrc = "HTTP://nsbuild01.telerik.com/docs/";
webView.src = targetSrc;
TKUnit.wait(4);
helper.goBack();
if (testFinished) {
TKUnit.assert(actualSrc === targetSrc.toLowerCase(), "args.url should equal '" + targetSrc.toLowerCase() + "'");
TKUnit.assert(actualError === undefined, actualError);
}
else {
TKUnit.assert(false, "TIMEOUT");
}
}

View File

@ -1,5 +1,5 @@
{
"version": "1.5.0-beta",
"version": "1.6.2",
"compilerOptions": {
"moduleResolution": "classic",
"target": "es5",
@ -8,7 +8,8 @@
"noImplicitAny": false,
"removeComments": true,
"noLib": true,
"outDir": "dist"
"outDir": "dist",
"experimentalDecorators": true
},
"filesGlob": [
"./**/*.ts",

View File

@ -2,6 +2,9 @@
import view = require("ui/core/view");
import dependencyObservable = require("ui/core/dependency-observable");
import proxy = require("ui/core/proxy");
import utils = require("utils/utils");
import fs = require("file-system");
import trace = require("trace");
var urlProperty = new dependencyObservable.Property(
"url",
@ -35,13 +38,32 @@ function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) {
return;
}
webView._loadSrc(data.newValue);
webView.stopLoading();
var src = <string>data.newValue;
trace.write("WebView._loadSrc(" + src + ")", trace.categories.Debug);
if (utils.isFileOrResourcePath(src)) {
if (src.indexOf("~/") === 0) {
src = fs.path.join(fs.knownFolders.currentApp().path, src.replace("~/", ""));
}
if (fs.File.exists(src)) {
var file = fs.File.fromPath(src);
var content = file.readTextSync();
webView._loadFileOrResource(src, content);
}
} else if (src.toLowerCase().indexOf("http://") === 0 || src.toLowerCase().indexOf("https://") === 0) {
webView._loadHttp(src);
} else {
webView._loadData(src);
}
}
// register the setNativeValue callback
(<proxy.PropertyMetadata>srcProperty.metadata).onSetNativeValue = onSrcPropertyChanged;
export class WebView extends view.View implements definition.WebView {
export abstract class WebView extends view.View implements definition.WebView {
public static loadStartedEvent = "loadStarted";
public static loadFinishedEvent = "loadFinished";
@ -97,13 +119,15 @@ export class WebView extends view.View implements definition.WebView {
this.notify(args);
}
public _loadUrl(url: string) {
throw new Error("This member is abstract.");
}
abstract _loadUrl(url: string): void;
public _loadSrc(src: string) {
throw new Error("This member is abstract.");
}
abstract _loadFileOrResource(path: string, content: string): void;
abstract _loadHttp(src: string): void;
abstract _loadData(src: string): void;
abstract stopLoading(): void;
get canGoBack(): boolean {
throw new Error("This member is abstract.");
@ -113,15 +137,9 @@ export class WebView extends view.View implements definition.WebView {
throw new Error("This member is abstract.");
}
public goBack() {
throw new Error("This member is abstract.");
}
abstract goBack(): void;
public goForward() {
throw new Error("This member is abstract.");
}
abstract goForward(): void;
public reload() {
throw new Error("This member is abstract.");
}
abstract reload(): void;
}

View File

@ -1,7 +1,5 @@
import common = require("./web-view-common");
import trace = require("trace");
import utils = require("utils/utils");
import fs = require("file-system");
global.moduleMerge(common, exports);
@ -70,41 +68,50 @@ export class WebView extends common.WebView {
}
public _loadUrl(url: string) {
if (!this._android) {
return;
}
trace.write("WebView._loadUrl(" + url + ")", trace.categories.Debug);
this._android.stopLoading();
this._android.loadUrl(url);
}
public _loadSrc(src: string) {
trace.write("WebView._loadSrc(" + src + ")", trace.categories.Debug);
this._android.stopLoading();
this._android.loadUrl("about:blank");
if (utils.isFileOrResourcePath(src)) {
if (src.indexOf("~/") === 0) {
src = fs.path.join(fs.knownFolders.currentApp().path, src.replace("~/", ""));
public _loadFileOrResource(path: string, content: string) {
if (!this._android) {
return;
}
var file = fs.File.fromPath(src);
if (file) {
var baseUrl = `file:///${src.substring(0, src.lastIndexOf('/') + 1)}`;
file.readText().then(r => {
this._android.loadDataWithBaseURL(baseUrl, r, "text/html; charset=utf-8", "utf-8", null);
});
var baseUrl = `file:///${path.substring(0, path.lastIndexOf('/') + 1) }`;
this._android.loadDataWithBaseURL(baseUrl, content, "text/html; charset=utf-8", "utf-8", null);
}
} else if (src.indexOf("http://") === 0 || src.indexOf("https://") === 0) {
public _loadHttp(src: string) {
if (!this._android) {
return;
}
this._android.loadUrl(src);
} else {
this._android.loadData(src, "text/html; charset=utf-8", "utf-8");
}
public _loadData(src: string) {
if (!this._android) {
return;
}
this._android.loadData(src, "text/html; charset=utf-8", "utf-8");
}
get canGoBack(): boolean {
return this._android.canGoBack();
}
public stopLoading() {
if (this._android) {
this._android.stopLoading();
}
}
get canGoForward(): boolean {
return this._android.canGoForward();
}

View File

@ -55,6 +55,11 @@ declare module "ui/web-view" {
*/
canGoForward: boolean;
/**
* Stops loading the current content (if any).
*/
stopLoading(): void;
/**
* Navigates back.
*/

View File

@ -1,7 +1,5 @@
import common = require("./web-view-common");
import trace = require("trace");
import utils = require("utils/utils");
import fs = require("file-system");
global.moduleMerge(common, exports);
@ -73,6 +71,10 @@ export class WebView extends common.WebView {
return this._ios;
}
public stopLoading() {
this._ios.stopLoading();
}
public _loadUrl(url: string) {
trace.write("WebView._loadUrl(" + url + ")", trace.categories.Debug);
@ -82,31 +84,17 @@ export class WebView extends common.WebView {
this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(url)));
}
public _loadSrc(src: string) {
trace.write("WebView._loadSrc(" + src + ")", trace.categories.Debug);
if (this._ios.loading) {
this._ios.stopLoading();
public _loadFileOrResource(path: string, content: string) {
var baseURL = NSURL.fileURLWithPath(NSString.stringWithString(path).stringByDeletingLastPathComponent);
this._ios.loadHTMLStringBaseURL(content, baseURL);
}
if (utils.isFileOrResourcePath(src)) {
if (src.indexOf("~/") === 0) {
src = fs.path.join(fs.knownFolders.currentApp().path, src.replace("~/", ""));
}
var file = fs.File.fromPath(src);
if (file) {
var baseURL = NSURL.fileURLWithPath(NSString.stringWithString(src).stringByDeletingLastPathComponent);
file.readText().then((r) => {
this._ios.loadHTMLStringBaseURL(r, baseURL);
});
}
} else if (src.indexOf("http://") === 0 || src.indexOf("https://") === 0) {
public _loadHttp(src: string) {
this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(src)));
} else {
this._ios.loadHTMLStringBaseURL(src, null);
}
public _loadData(src: string) {
this._ios.loadHTMLStringBaseURL(src, null);
}
get canGoBack(): boolean {