Updated Branches: refs/heads/master da4150649 -> ee2c1d20c
[CB-4420] Add a helper function for resolving relative URLs. Note that the tests required a newer version of jsdom. Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/2ec92f7b Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/2ec92f7b Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/2ec92f7b Branch: refs/heads/master Commit: 2ec92f7b412a3bda84e44f156b9b371c9f94a501 Parents: da41506 Author: Andrew Grieve <[email protected]> Authored: Mon Jul 29 15:22:49 2013 -0400 Committer: Andrew Grieve <[email protected]> Committed: Mon Jul 29 15:22:49 2013 -0400 ---------------------------------------------------------------------- lib/common/urlutil.js | 32 +++++++++++++++++++++++ package.json | 2 +- test/runner.js | 2 +- test/test.urlutil.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-js/blob/2ec92f7b/lib/common/urlutil.js ---------------------------------------------------------------------- diff --git a/lib/common/urlutil.js b/lib/common/urlutil.js new file mode 100644 index 0000000..4741a51 --- /dev/null +++ b/lib/common/urlutil.js @@ -0,0 +1,32 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +var urlutil = exports; +var anchorEl = document.createElement('a'); + +/** + * For already absolute URLs, returns what is passed in. + * For relative URLs, converts them to absolute ones. + */ +urlutil.makeAbsolute = function(url) { + anchorEl.href = url; + return anchorEl.href; +}; http://git-wip-us.apache.org/repos/asf/cordova-js/blob/2ec92f7b/package.json ---------------------------------------------------------------------- diff --git a/package.json b/package.json index f0a82de..b81f0f5 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ } ], "dependencies": { - "jsdom": "0.2.14", + "jsdom": "0.8.2", "connect": "1.8.5" }, "devDependencies": { http://git-wip-us.apache.org/repos/asf/cordova-js/blob/2ec92f7b/test/runner.js ---------------------------------------------------------------------- diff --git a/test/runner.js b/test/runner.js index 6ca0c01..0005731 100644 --- a/test/runner.js +++ b/test/runner.js @@ -49,7 +49,7 @@ module.exports = { try { jsdom = require("jsdom").jsdom; - document = jsdom("<html><head></head></html>"); + document = jsdom(null, null, { url: 'http://jsdomtest.info/a?b#c' }); window = document.createWindow(); } catch (e) { //no jsDom (some people don't have compilers) http://git-wip-us.apache.org/repos/asf/cordova-js/blob/2ec92f7b/test/test.urlutil.js ---------------------------------------------------------------------- diff --git a/test/test.urlutil.js b/test/test.urlutil.js new file mode 100644 index 0000000..c4f9acb --- /dev/null +++ b/test/test.urlutil.js @@ -0,0 +1,65 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +describe('urlutil', function () { + var urlutil = require('cordova/urlutil'); + + it('can handle absolute URLs', function () { + expect(urlutil.makeAbsolute('http://www.foo.com')).toBe('http://www.foo.com/'); + expect(urlutil.makeAbsolute('http://www.foo.com?foo#bar')).toBe('http://www.foo.com/?foo#bar'); + expect(urlutil.makeAbsolute('http://www.foo.com/%20hi')).toBe('http://www.foo.com/%20hi'); + }); + + function testRootRelative(url) { + var rootUrl = url.slice(0, 10) + url.slice(10).replace(/[?#].*/, '').replace(/\/.*/, '') + '/'; + expect(urlutil.makeAbsolute('/')).toBe(rootUrl); + expect(urlutil.makeAbsolute('/foo?a#b')).toBe(rootUrl + 'foo?a#b'); + expect(urlutil.makeAbsolute('/foo/b%20ar')).toBe(rootUrl + 'foo/b%20ar'); + } + it('can handle root-relative URLs', function () { + testRootRelative(window.location.href); + }); + + it('can handle relative URLs', function () { + var rootUrl = window.location.href.replace(/[?#].*/, '').replace(/[^\/]*$/, ''); + expect(urlutil.makeAbsolute('foo?a#b')).toBe(rootUrl + 'foo?a#b'); + expect(urlutil.makeAbsolute('foo/b%20ar')).toBe(rootUrl + 'foo/b%20ar'); + }); + + it('can handle relative URLs with base tags', function () { + var rootUrl = 'http://base.com/esab/'; + var baseTag = document.createElement('base'); + baseTag.href = rootUrl; + document.head.appendChild(baseTag); + this.after(function() { + document.head.removeChild(baseTag); + }); + expect(urlutil.makeAbsolute('foo?a#b')).toBe(rootUrl + 'foo?a#b'); + expect(urlutil.makeAbsolute('foo/b%20ar')).toBe(rootUrl + 'foo/b%20ar'); + testRootRelative(rootUrl); + }); + + it('can handle scheme-relative URLs', function () { + var rootUrl = window.location.href.replace(/:.*/, ''); + expect(urlutil.makeAbsolute('//www.foo.com/baz%20?foo#bar')).toBe(rootUrl + '://www.foo.com/baz%20?foo#bar'); + }); + +});
