From d46c18b2d494e245a283554ac6da32897ff78004 Mon Sep 17 00:00:00 2001 From: Pascal Hartig Date: Mon, 6 Jan 2014 15:58:55 +0000 Subject: [PATCH] First attempt --- .editorconfig | 13 ++++ .gitignore | 1 + .jshintrc | 20 ++++++ .travis.yml | 3 + LICENSE | 19 ++++++ README.md | 12 ++++ index.js | 116 +++++++++++++++++++++++++++++++ package.json | 38 +++++++++++ test.js | 184 ++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 406 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 .jshintrc create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json create mode 100644 test.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..5d12634 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..cb47ee9 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,20 @@ +{ + "node": true, + "esnext": true, + "bitwise": true, + "camelcase": true, + "curly": true, + "eqeqeq": true, + "immed": true, + "indent": 2, + "latedef": true, + "newcap": true, + "noarg": true, + "quotmark": "single", + "regexp": true, + "undef": true, + "unused": true, + "strict": true, + "trailing": true, + "smarttabs": true +} diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..244b7e8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: + - '0.10' diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d84ccdb --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (C) 2014 Pascal Hartig + + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a1e2cbc --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +Node DOM URLs +============= + +A partial implementation of the [W3C URL Spec Draft](https://dvcs.w3.org/hg/url/raw-file/tip/Overview.html) for Node. + +If you find incompatabilities, please [report them](https://github.com/passy/node-dom-urls/issues). Error handling is currently very different from the spec. + +Browser Polyfills +----------------- + + - [Joshua Bell's Polyfill](https://github.com/inexorabletash/polyfill/blob/master/url.js) + - [Eric Arvidsson's Polyfill](https://github.com/arv/DOM-URL-Polyfill) diff --git a/index.js b/index.js new file mode 100644 index 0000000..07d6caa --- /dev/null +++ b/index.js @@ -0,0 +1,116 @@ +'use strict'; + +var url = require('url'); + +function _parseURL(urlStr, base) { + var _url; + + if (base) { + urlStr = url.resolve(base, urlStr); + } + + _url = url.parse( + urlStr, + /* parseQueryString */ true, + /* slashesDenoteHost */ true + ); + + if (!_url.protocol) { + throw new TypeError('Failed to construct URL: Invalid URL'); + } + + return _url; +} + +function URL(urlStr, base) { + this._url = _parseURL(urlStr, base); +} + +URL.prototype = { + toString: function () { + return this.href; + }, + + get href() { + return this._url.format(); + }, + + set href(value) { + this._url = _parseURL(value); + }, + + get host() { + return this._url.host; + }, + + set host(value) { + // The host value affects multiple attributes, but the `url` module + // doesn't cascade the changes, so we manually update them. + var newUrl = url.parse('proto:' + value); + + if (newUrl.port) { + this._url.port = newUrl.port; + this._url.host = newUrl.host; + } else if (this._url.port) { + this._url.host = newUrl.host + ':' + this._url.port; + } else { + this._url.host = newUrl.host; + } + + this._url.hostname = newUrl.hostname; + }, + + get hostname() { + return this._url.hostname; + }, + + set hostname(value) { + // Replace a port if it's in place and treat it like a host change + // afterwards. + this.host = value.replace(/(\:.*)?$/, ''); + }, + + get protocol() { + return this._url.protocol; + }, + + set protocol(value) { + // Strip the colon, including anything following it and replace it with a + // single one. + this._url.protocol = value.replace(/(\:.*)?$/, ':'); + }, + + get port() { + // Port should be an empty string (zero ASCII digits) instead of null in DOM + // land. (Section 4.1) + return this._url.port || ''; + }, + + set port(value) { + this._url.port = value; + }, + + get pathname() { + return this._url.pathname; + }, + + set pathname(value) { + + } +}; + +[ +].forEach(function (property) { + Object.defineProperty(URL.prototype, property, { + get: function () { + return this._url[property]; + }, + set: function (value) { + this._url[property] = value; + } + }); +}); + +module.exports = { + URL: URL +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..fb13529 --- /dev/null +++ b/package.json @@ -0,0 +1,38 @@ +{ + "name": "dom-urls", + "version": "0.0.0", + "description": "DOM URLs for Node", + "main": "index.js", + "files": [ + "index.js" + ], + "scripts": { + "test": "mocha -u tdd" + }, + "repository": { + "type": "git", + "url": "git://github.com/passy/node-dom-urls" + }, + "keywords": [ + "dom", + "url", + "urls" + ], + "author": { + "name": "Pascal Hartig", + "email": "passy@twitter.com", + "url": "http://passy.me/" + }, + "license": "MIT", + "bugs": { + "url": "https://github.com/passy/node-dom-urls/issues" + }, + "devDependencies": { + "mocha": "~1.16.2", + "chai": "~1.8.1" + }, + "dependencies": { + "extend": "~1.2.1", + "URIjs": "~1.11.2" + } +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..17d1e6b --- /dev/null +++ b/test.js @@ -0,0 +1,184 @@ +/*global suite,test */ +// Test suite by Erik Arvidsson +// released under Apache 2 License +// https://github.com/arv/DOM-URL-Polyfill + + +'use strict'; + +var URL = require('./index').URL; +var assert = require('chai').assert; + +suite('Properties', function () { + + test('constructor', function () { + assert.throws(function () { + new URL(); + }, TypeError); + assert.throws(function () { + new URL('relative'); + }, TypeError); + }); + + test('base', function () { + var url = new URL('relative', 'http://base/'); + assert.equal(url.toString(), 'http://base/relative'); + + url = new URL('relative', 'http://base/sub'); + assert.equal(url.toString(), 'http://base/relative'); + + url = new URL('relative', 'http://base/sub/'); + assert.equal(url.toString(), 'http://base/sub/relative'); + + url = new URL('/absolute', 'http://base/sub/'); + assert.equal(url.toString(), 'http://base/absolute'); + }); + + test('protocol', function () { + var url = new URL('mailto:abc@def.com'); + assert.equal(url.protocol, 'mailto:'); + + url = new URL('http://www.example.com/'); + assert.equal(url.protocol, 'http:'); + + url.protocol = 'https'; + assert.equal(url.protocol, 'https:'); + + url.protocol = 'foo:'; + assert.equal(url.protocol, 'foo:'); + assert.equal(url.href, 'foo://www.example.com/'); + + url.protocol = 'foo:followedbyinvalidstuff'; + assert.equal(url.protocol, 'foo:'); + assert.equal(url.href, 'foo://www.example.com/'); + }); + + test('username', function () { + // not implemented + }); + + test('password', function () { + // not implemented + }); + + test('host', function () { + var url = new URL('http://www.example.com/'); + assert.equal(url.host, 'www.example.com'); + + url = new URL('http://www.example.com:8080/'); + assert.equal(url.host, 'www.example.com:8080'); + + url.host = 'changed'; + assert.equal(url.host, 'changed:8080'); + assert.equal(url.href, 'http://changed:8080/'); + + url.host = 'again:99'; + assert.equal(url.host, 'again:99'); + assert.equal(url.href, 'http://again:99/'); + }); + + test('hostname', function () { + var url = new URL('http://www.example.com/'); + assert.equal(url.hostname, 'www.example.com'); + + url = new URL('http://www.example.com:8080/'); + assert.equal(url.hostname, 'www.example.com'); + + url.hostname = 'changed'; + assert.equal(url.hostname, 'changed'); + assert.equal(url.host, 'changed:8080'); + assert.equal(url.href, 'http://changed:8080/'); + }); + + test('port', function () { + var url = new URL('http://www.example.com/'); + assert.equal(url.port, ''); + + url = new URL('http://www.example.com:8080/'); + assert.equal(url.port, '8080'); + + url.port = '99'; + assert.equal(url.port, '99'); + assert.equal(url.href, 'http://www.example.com:99/'); + }); + + test('pathname', function () { + var url = new URL('http://www.example.com/'); + assert.equal(url.pathname, '/'); + + url = new URL('http://www.example.com'); + assert.equal(url.pathname, '/'); + + url = new URL('http://www.example.com/a/b'); + assert.equal(url.pathname, '/a/b'); + + url = new URL('http://www.example.com/a/b/'); + assert.equal(url.pathname, '/a/b/'); + + url.pathname = ''; + assert.equal(url.pathname, '/'); + + url.pathname = 'a/b/c'; + assert.equal(url.pathname, '/a/b/c'); + }); + + test('search', function () { + var url = new URL('http://www.example.com/'); + assert.equal(url.search, ''); + + url = new URL('http://www.example.com/?'); + assert.equal(url.search, ''); + + url = new URL('http://www.example.com/?a'); + assert.equal(url.search, '?a'); + + url.search = 'b'; + assert.equal(url.search, '?b'); + + url.search = '?b'; + assert.equal(url.search, '?b'); + + url.search = ''; + assert.equal(url.search, ''); + + url.search = '?'; + assert.equal(url.search, ''); + }); + + test('hash', function () { + var url = new URL('http://www.example.com/'); + assert.equal(url.hash, ''); + + url = new URL('http://www.example.com/#'); + assert.equal(url.hash, ''); + + url = new URL('http://www.example.com/#a'); + assert.equal(url.hash, '#a'); + + url.hash = 'b'; + assert.equal(url.hash, '#b'); + + url.hash = '#b'; + assert.equal(url.hash, '#b'); + + url.hash = ''; + assert.equal(url.hash, ''); + + url.hash = '#'; + assert.equal(url.hash, ''); + }); + + test('origin', function () { + // not implemented + }); + + test('href', function () { + var url = new URL('http://www.example.com/'); + assert.equal(url.href, 'http://www.example.com/'); + assert.equal(url.href, url.toString()); + + url.href = 'https://changed/'; + assert.equal(url.href, 'https://changed/'); + }); + +});