Skip to content

Commit

Permalink
fixing URI constructor / href() to accept DOM elements - closes #77
Browse files Browse the repository at this point in the history
  • Loading branch information
rodneyrehm committed Aug 3, 2013
1 parent 3a94df3 commit 67d297c
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 1 deletion.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,26 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m

## Changelog ##

### `[dev-version]` (master branch) ###

* fixing [URI constructor](http://medialize.github.io/URI.js/docs.html#constructor) to accept the following elements - ([Issue #77](https://github.com/medialize/URI.js/issues/77))
* [`<a href="...">`](http://www.w3.org/html/wg/drafts/html/master/text-level-semantics.html#the-a-element)
* [`<blockquote cite="...">`](http://www.w3.org/html/wg/drafts/html/master/grouping-content.html#the-blockquote-element)
* [`<link href="...">`](http://www.w3.org/html/wg/drafts/html/master/document-metadata.html#the-link-element)
* [`<base href="...">`](http://www.w3.org/html/wg/drafts/html/master/document-metadata.html#the-base-element)
* [`<script src="...">`](http://www.w3.org/html/wg/drafts/html/master/scripting-1.html#script)
* [`<form action="...">`](http://www.w3.org/html/wg/drafts/html/master/forms.html#the-form-element)
* [`<input type="image" src="...">`](http://www.w3.org/html/wg/drafts/html/master/forms.html#the-input-element)
* [`<img src="...">`](http://www.w3.org/html/wg/drafts/html/master/embedded-content-0.html#the-img-element)
* [`<area href="...">`](http://www.w3.org/html/wg/drafts/html/master/embedded-content-0.html#the-area-element)
* [`<iframe src="...">`](http://www.w3.org/html/wg/drafts/html/master/embedded-content-0.html#the-iframe-element)
* [`<embed src="...">`](http://www.w3.org/html/wg/drafts/html/master/embedded-content-0.html#the-embed-element)
* [`<source src="...">`](http://www.w3.org/html/wg/drafts/html/master/embedded-content-0.html#the-source-element)
* [`<track src="...">`](http://www.w3.org/html/wg/drafts/html/master/embedded-content-0.html#the-track-element)


### 1.10.2 (April 15th 2013) ###

* fixing [`relativeTo()`](http://medialize.github.com/URI.js/docs.html#relativeto) - ([Issue #75](https://github.com/medialize/URI.js/issues/75))
* fixing [`normalizePath()`](http://medialize.github.com/URI.js/docs.html#normalize-path) to not prepend ./ to relative paths - ([Issue #76](https://github.com/medialize/URI.js/issues/76))

Expand Down
25 changes: 25 additions & 0 deletions docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,31 @@ <h3 id="constructor">URI Constructor</h3>

// and any combination of the above…</pre>

<p>using DOM elements:</p>
<pre class="prettyprint lang-js">var element = document.createElement('a');
element.href = 'http://example.org';
var uri = new URI(element);
// uri.domain() === 'example.org';</pre>
<pre class="prettyprint lang-html">The following DOM elements can be parsed:

&lt;a href=&quot;...&quot;&gt;
&lt;blockquote cite=&quot;...&quot;&gt;
&lt;link href=&quot;...&quot;&gt;
&lt;base href=&quot;...&quot;&gt;
&lt;script src=&quot;...&quot;&gt;
&lt;form action=&quot;...&quot;&gt;
&lt;input type=&quot;image&quot; src=&quot;...&quot;&gt;
&lt;img src=&quot;...&quot;&gt;
&lt;area href=&quot;...&quot;&gt;
&lt;iframe src=&quot;...&quot;&gt;
&lt;embed src=&quot;...&quot;&gt;
&lt;source src=&quot;...&quot;&gt;
&lt;track src=&quot;...&quot;&gt;


any other element yields URI("")
</pre>

<h3 id="clone">cloning URIs</h3>
<p>Get a copy of the current URI instance</p>
<pre class="prettyprint lang-js">var uri = new URI("http://example.org"),
Expand Down
35 changes: 34 additions & 1 deletion src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,35 @@ URI.defaultPorts = {
// ALPHA DIGIT "-" "." "_" "~" "!" "$" "&" "'" "(" ")" "*" "+" "," ";" "=" %encoded
// I've never seen a (non-IDN) hostname other than: ALPHA DIGIT . -
URI.invalid_hostname_characters = /[^a-zA-Z0-9\.-]/;
// map DOM Elements to their URI attribute
URI.domAttributes = {
'a': 'href',
'blockquote': 'cite',
'link': 'href',
'base': 'href',
'script': 'src',
'form': 'action',
'img': 'src',
'area': 'href',
'iframe': 'src',
'embed': 'src',
'source': 'src',
'track': 'src',
'input': 'src' // but only if type="image"
};
URI.getDomAttribute = function(node) {
if (!node || !node.nodeName) {
return undefined;
}

var nodeName = node.nodeName.toLowerCase();
// <input> should only expose src for type="image"
if (nodeName === 'input' && node.type !== 'image') {
return undefined;
}

return URI.domAttributes[nodeName];
};
// encoding / decoding according to RFC3986
function strictEncodeURIComponent(string) {
// see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent
Expand Down Expand Up @@ -821,7 +850,11 @@ p.href = function(href, build) {

var _URI = href instanceof URI;
var _object = typeof href === "object" && (href.hostname || href.path || href.pathname);

if (href.nodeName) {
var attribute = URI.getDomAttribute(href);
href = href[attribute] || "";
_object = false;
}

// window.location is reported to be an object, but it's not the sort
// of object we're looking for:
Expand Down
52 changes: 52 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,58 @@ test("new URI(Location)", function () {
var u = new URI(location);
equal(u.href(), String(location.href), "location object");
});
(function() {
var element;

for (var nodeName in URI.domAttributes) {
if (!Object.prototype.hasOwnProperty.call(URI.domAttributes, nodeName) || nodeName === 'input') {
continue;
}

element = document.createElement(nodeName);
testDomAttribute(element, URI.domAttributes[nodeName]);
}

element = document.createElement('input');
element.type = 'image';
testDomAttribute(element, 'src');

element = document.createElement('input');
testUnsupportedDomAttribute(element, 'src');

element = document.createElement('div');
testUnsupportedDomAttribute(element, 'src');

function testDomAttribute(element, attribute) {
test("new URI(Element " + element.nodeName, function() {
element[attribute] = "http://example.org/foobar.html";

var u = new URI(element);
equal(u.scheme(), "http", "scheme");
equal(u.host(), "example.org", "host");
equal(u.path(), "/foobar.html", "path");

element[attribute] = "file:///C:/foo/bar.html";
u = new URI(element);
equal(u.href(), element[attribute], "file");
});
}

function testUnsupportedDomAttribute(element, attribute) {
test("new URI(unsupported Element " + element.nodeName, function() {
element[attribute] = "http://example.org/foobar.html";

var u = new URI(element);
equal(u.scheme(), "", "scheme");
equal(u.host(), "", "host");
equal(u.path(), "/", "path");

element[attribute] = "file:///C:/foo/bar.html";
u = new URI(element);
equal(u.href(), "", "file");
});
}
})();
test("new URI(HTMLAnchorElement", function (){
var a = document.createElement("a");
a.href = "http://example.org/foobar.html";
Expand Down

0 comments on commit 67d297c

Please sign in to comment.