Skip to content

Commit

Permalink
First attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
passy committed Jan 6, 2014
0 parents commit d46c18b
Show file tree
Hide file tree
Showing 9 changed files with 406 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .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
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
20 changes: 20 additions & 0 deletions .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
}
3 changes: 3 additions & 0 deletions .travis.yml
@@ -0,0 +1,3 @@
language: node_js
node_js:
- '0.10'
19 changes: 19 additions & 0 deletions 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.
12 changes: 12 additions & 0 deletions 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)
116 changes: 116 additions & 0 deletions 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
};
38 changes: 38 additions & 0 deletions 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"
}
}

0 comments on commit d46c18b

Please sign in to comment.