From 1a76b58768e2223d5191a6651e451336228e8a40 Mon Sep 17 00:00:00 2001 From: atanasovg Date: Thu, 24 Sep 2015 18:02:51 +0300 Subject: [PATCH 1/4] Extracts common logic into web-view-common. The check for http/https is now case insensitive. Fixes #819 --- ui/web-view/web-view-common.ts | 38 ++++++++++++++++++++-- ui/web-view/web-view.android.ts | 57 ++++++++++++++++++--------------- ui/web-view/web-view.d.ts | 5 +++ ui/web-view/web-view.ios.ts | 38 ++++++++-------------- 4 files changed, 86 insertions(+), 52 deletions(-) diff --git a/ui/web-view/web-view-common.ts b/ui/web-view/web-view-common.ts index 1d96e1b69..c81fc5d60 100644 --- a/ui/web-view/web-view-common.ts +++ b/ui/web-view/web-view-common.ts @@ -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,7 +38,26 @@ function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) { return; } - webView._loadSrc(data.newValue); + webView.stopLoading(); + + var src = 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 @@ -101,7 +123,19 @@ export class WebView extends view.View implements definition.WebView { throw new Error("This member is abstract."); } - public _loadSrc(src: string) { + public _loadFileOrResource(path: string, content: string) { + throw new Error("This member is abstract."); + } + + public _loadHttp(src: string) { + throw new Error("This member is abstract."); + } + + public _loadData(src: string) { + throw new Error("This member is abstract."); + } + + public stopLoading(): void { throw new Error("This member is abstract."); } diff --git a/ui/web-view/web-view.android.ts b/ui/web-view/web-view.android.ts index 5eb7fc0e6..f836bb38d 100644 --- a/ui/web-view/web-view.android.ts +++ b/ui/web-view/web-view.android.ts @@ -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("~/", "")); - } - - 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); - }); - } - } else if (src.indexOf("http://") === 0 || src.indexOf("https://") === 0) { - this._android.loadUrl(src); - } else { - this._android.loadData(src, "text/html; charset=utf-8", "utf-8"); + public _loadFileOrResource(path: string, content: string) { + if (!this._android) { + return; } + + var baseUrl = `file:///${path.substring(0, path.lastIndexOf('/') + 1) }`; + this._android.loadDataWithBaseURL(baseUrl, content, "text/html", null, null); + } + + public _loadHttp(src: string) { + if (!this._android) { + return; + } + + this._android.loadUrl(src); + } + + public _loadData(src: string) { + if (!this._android) { + return; + } + + this._android.loadData(src, "text/html", null); } get canGoBack(): boolean { return this._android.canGoBack(); } + public stopLoading() { + if (this._android) { + this._android.stopLoading(); + } + } + get canGoForward(): boolean { return this._android.canGoForward(); } diff --git a/ui/web-view/web-view.d.ts b/ui/web-view/web-view.d.ts index d8f6e5305..038085ae5 100644 --- a/ui/web-view/web-view.d.ts +++ b/ui/web-view/web-view.d.ts @@ -55,6 +55,11 @@ declare module "ui/web-view" { */ canGoForward: boolean; + /** + * Stops loading the current content (if any). + */ + stopLoading(): void; + /** * Navigates back. */ diff --git a/ui/web-view/web-view.ios.ts b/ui/web-view/web-view.ios.ts index 0f1937abf..7d642a8d9 100644 --- a/ui/web-view/web-view.ios.ts +++ b/ui/web-view/web-view.ios.ts @@ -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); + public _loadFileOrResource(path: string, content: string) { + var baseURL = NSURL.fileURLWithPath(NSString.stringWithString(path).stringByDeletingLastPathComponent); + this._ios.loadHTMLStringBaseURL(content, baseURL); + } - if (this._ios.loading) { - this._ios.stopLoading(); - } + public _loadHttp(src: string) { + this._ios.loadRequest(NSURLRequest.requestWithURL(NSURL.URLWithString(src))); + } - 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) { - 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 { From 198f4f1dbc0a342a35bcecb5714121cee15b8c5e Mon Sep 17 00:00:00 2001 From: atanasovg Date: Wed, 30 Sep 2015 14:33:03 +0300 Subject: [PATCH 2/4] Add WebView test. Make its members abstract. --- CrossPlatformModules.csproj | 13 +++-- apps/tests/ui/web-view/web-view-tests.ts | 66 ++++++++++++++---------- tsconfig.json | 5 +- ui/web-view/web-view-common.ts | 34 ++++-------- 4 files changed, 60 insertions(+), 58 deletions(-) diff --git a/CrossPlatformModules.csproj b/CrossPlatformModules.csproj index 786adf343..51a081878 100644 --- a/CrossPlatformModules.csproj +++ b/CrossPlatformModules.csproj @@ -183,10 +183,11 @@ - nordic.xml - + nordic.xml + + animation.d.ts @@ -576,6 +577,11 @@ action-bar.d.ts + + + + + @@ -1922,6 +1928,7 @@ PreserveNewest + @@ -1984,7 +1991,7 @@ False - + \ No newline at end of file diff --git a/apps/tests/ui/web-view/web-view-tests.ts b/apps/tests/ui/web-view/web-view-tests.ts index 3acb0b6b8..6ef3890df 100644 --- a/apps/tests/ui/web-view/web-view-tests.ts +++ b/apps/tests/ui/web-view/web-view-tests.ts @@ -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, "args.url should equal '" + targetSrc + "'"); + TKUnit.assert(actualError === undefined, actualError); + } + else { + TKUnit.assert(false, "TIMEOUT"); + } +} diff --git a/tsconfig.json b/tsconfig.json index 03d1ff2e0..27cdead8f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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", diff --git a/ui/web-view/web-view-common.ts b/ui/web-view/web-view-common.ts index c81fc5d60..ccb87fdba 100644 --- a/ui/web-view/web-view-common.ts +++ b/ui/web-view/web-view-common.ts @@ -63,7 +63,7 @@ function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) { // register the setNativeValue callback (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"; @@ -119,25 +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 _loadFileOrResource(path: string, content: string) { - throw new Error("This member is abstract."); - } + abstract _loadFileOrResource(path: string, content: string): void; - public _loadHttp(src: string) { - throw new Error("This member is abstract."); - } + abstract _loadHttp(src: string): void; - public _loadData(src: string) { - throw new Error("This member is abstract."); - } + abstract _loadData(src: string): void; - public stopLoading(): void { - throw new Error("This member is abstract."); - } + abstract stopLoading(): void; get canGoBack(): boolean { throw new Error("This member is abstract."); @@ -147,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; } \ No newline at end of file From 4c37907d8f16ac6a71ee62cc4a6b252e6bd293eb Mon Sep 17 00:00:00 2001 From: atanasovg Date: Wed, 30 Sep 2015 18:03:12 +0300 Subject: [PATCH 3/4] Fix failing test. --- apps/tests/ui/web-view/web-view-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/tests/ui/web-view/web-view-tests.ts b/apps/tests/ui/web-view/web-view-tests.ts index 6ef3890df..598922ebd 100644 --- a/apps/tests/ui/web-view/web-view-tests.ts +++ b/apps/tests/ui/web-view/web-view-tests.ts @@ -240,7 +240,7 @@ export var testLoadUpperCaseSrc = function () { helper.goBack(); if (testFinished) { - TKUnit.assert(actualSrc === targetSrc, "args.url should equal '" + targetSrc + "'"); + TKUnit.assert(actualSrc === targetSrc.toLowerCase(), "args.url should equal '" + targetSrc.toLowerCase() + "'"); TKUnit.assert(actualError === undefined, actualError); } else { From bd97f22683570c8a9e74208325799dc8cd58105b Mon Sep 17 00:00:00 2001 From: atanasovg Date: Wed, 30 Sep 2015 18:06:43 +0300 Subject: [PATCH 4/4] Integrate #845 into this pull. --- ui/web-view/web-view.android.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ui/web-view/web-view.android.ts b/ui/web-view/web-view.android.ts index f836bb38d..89087fe5d 100644 --- a/ui/web-view/web-view.android.ts +++ b/ui/web-view/web-view.android.ts @@ -83,7 +83,7 @@ export class WebView extends common.WebView { } var baseUrl = `file:///${path.substring(0, path.lastIndexOf('/') + 1) }`; - this._android.loadDataWithBaseURL(baseUrl, content, "text/html", null, null); + this._android.loadDataWithBaseURL(baseUrl, content, "text/html; charset=utf-8", "utf-8", null); } public _loadHttp(src: string) { @@ -99,7 +99,7 @@ export class WebView extends common.WebView { return; } - this._android.loadData(src, "text/html", null); + this._android.loadData(src, "text/html; charset=utf-8", "utf-8"); } get canGoBack(): boolean {