Skip to content

Commit e9e302f

Browse files
author
Chris K
committed
Added short_url in the URI class.
1 parent c63816b commit e9e302f

File tree

2 files changed

+96
-11
lines changed

2 files changed

+96
-11
lines changed

src/scripts/uri.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var URIPrototype = function(uri_prop_name)
5151
if (pos > -1)
5252
this._filename = this.pathname.slice(pos + 1);
5353
else
54-
this._filename = this.pathname;
54+
this._filename = "";
5555
}
5656
else
5757
this._filename = "";
@@ -106,11 +106,42 @@ var URIPrototype = function(uri_prop_name)
106106

107107
this.__defineGetter__("basename", function()
108108
{
109-
return this.filename || this.dir_pathname;
109+
110+
if (this._basename === undefined && (this._is_parsed || this[uri_prop_name]))
111+
{
112+
if (!this._is_data_uri)
113+
{
114+
if (this.filename)
115+
this._basename = this.filename;
116+
else if (this.path_parts.length)
117+
this._basename = this.path_parts[this.path_parts.length - 1] + "/";
118+
else
119+
this._basename = "";
120+
}
121+
else
122+
this._basename = "";
123+
}
124+
125+
return this._basename;
110126
});
111127

112128
this.__defineSetter__("basename", function() {});
113129

130+
this.__defineGetter__("short_url", function()
131+
{
132+
if (this._short_url === undefined && (this._is_parsed || this[uri_prop_name]))
133+
{
134+
if (this._is_data_uri)
135+
this._short_url = (this._protocol + this._pathname).slice(0, 20) + "…";
136+
else
137+
this._short_url = this.filename || this.basename || this.host;
138+
}
139+
140+
return this._short_url;
141+
});
142+
143+
this.__defineSetter__("short_url", function() {});
144+
114145
this.__defineGetter__("abs_dir", function()
115146
{
116147
if (this._abs_dir === undefined && (this._is_parsed || this[uri_prop_name]))
@@ -244,6 +275,7 @@ var URIPrototype = function(uri_prop_name)
244275
else if (this._protocol && !this._is_data_uri)
245276
{
246277
this._host = val;
278+
val = "";
247279
}
248280
else
249281
this._host = "";

tests/uri.qunit.html

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
<!DOCTYPE html>
1+
<!DOCTYPE html>
22
<html>
33
<head>
44
<title>Dragonfly - URI class</title>
5+
<metea charset="utf-8">
56
<link rel="stylesheet" href="qunit/qunit.css" media="screen">
67
<script src="qunit/qunit.js"></script>
78
<script src="../src/scripts/uri.js"></script>
@@ -11,7 +12,7 @@
1112

1213

1314
test("uri", function()
14-
{
15+
{
1516
var uri = new URI("http://edition.cnn.com/.element/ssi/intl/breaking_news/3.0/banner.html?csiID=csi1#hello");
1617

1718
equal(uri.protocol, "http:");
@@ -27,17 +28,17 @@
2728
deepEqual(uri.path_parts, [".element", "ssi", "intl", "breaking_news", "3.0", "banner.html"]);
2829
equal(uri.short_distinguisher, "banner.html?csiID=csi1#hello");
2930

30-
var uri = new URI("foo.txt");
31+
var uri = new URI("http://a.b.c/foo.txt");
3132

32-
equal(uri.protocol, "");
33-
equal(uri.host, "");
34-
equal(uri.pathname, "foo.txt");
33+
equal(uri.protocol, "http:");
34+
equal(uri.host, "a.b.c");
35+
equal(uri.pathname, "/foo.txt");
3536
equal(uri.search, "");
3637
equal(uri.hash, "");
3738
equal(uri.filename, "foo.txt");
3839
equal(uri.extension, "txt");
39-
equal(uri.dir_pathname, "");
40-
equal(uri.abs_dir, "");
40+
equal(uri.dir_pathname, "/");
41+
equal(uri.abs_dir, "http://a.b.c/");
4142
deepEqual(uri.params, []);
4243
deepEqual(uri.path_parts, ["foo.txt"]);
4344
equal(uri.short_distinguisher, "foo.txt");
@@ -94,7 +95,8 @@
9495

9596
var uri = new URI("http://edition.cnn.com/");
9697
equal(uri.short_distinguisher, "edition.cnn.com");
97-
98+
equal(uri.pathname, "/");
99+
98100
var uri = new URI("data:text/html,<p>?a=b#a");
99101
equal(uri.protocol, "data:");
100102
equal(uri.host, "");
@@ -108,6 +110,57 @@
108110
deepEqual(uri.params, []);
109111
deepEqual(uri.path_parts, []);
110112
equal(uri.short_distinguisher, "data:text/html,<p>?a=b#a");
113+
114+
var uri = new URI("http://a.b.c");
115+
equal(uri.protocol, "http:");
116+
equal(uri.host, "a.b.c");
117+
equal(uri.pathname, "");
118+
equal(uri.dir_pathname, "");
119+
equal(uri.abs_dir, "http://a.b.c");
120+
equal(uri.basename, "");
121+
equal(uri.filename, "");
122+
equal(uri.short_url, "a.b.c");
123+
124+
var uri = new URI("http://a.b.c/a/b/c/");
125+
equal(uri.protocol, "http:");
126+
equal(uri.host, "a.b.c");
127+
equal(uri.pathname, "/a/b/c/");
128+
equal(uri.dir_pathname, "/a/b/c/");
129+
equal(uri.abs_dir, "http://a.b.c/a/b/c/");
130+
equal(uri.basename, "c/");
131+
equal(uri.filename, "");
132+
equal(uri.short_url, "c/");
133+
134+
var uri = new URI("http://a.b.c/a/b/c/d");
135+
equal(uri.protocol, "http:");
136+
equal(uri.host, "a.b.c");
137+
equal(uri.pathname, "/a/b/c/d");
138+
equal(uri.dir_pathname, "/a/b/c/");
139+
equal(uri.abs_dir, "http://a.b.c/a/b/c/");
140+
equal(uri.basename, "d");
141+
equal(uri.filename, "d");
142+
equal(uri.short_url, "d");
143+
144+
var uri = new URI("data:text/plain,a");
145+
equal(uri.protocol, "data:");
146+
equal(uri.host, "");
147+
equal(uri.pathname, "text/plain,a");
148+
equal(uri.dir_pathname, "");
149+
equal(uri.abs_dir, "");
150+
equal(uri.basename, "");
151+
equal(uri.filename, "");
152+
equal(uri.short_url, "data:text/plain,a…");
153+
154+
var uri = new URI("data:text/plain,abcdefghijklmnopqrstuvwxyz");
155+
equal(uri.protocol, "data:");
156+
equal(uri.host, "");
157+
equal(uri.pathname, "text/plain,abcdefghijklmnopqrstuvwxyz");
158+
equal(uri.dir_pathname, "");
159+
equal(uri.abs_dir, "");
160+
equal(uri.basename, "");
161+
equal(uri.filename, "");
162+
equal(uri.short_url, "data:text/plain,abcd…");
163+
111164
});
112165

113166

0 commit comments

Comments
 (0)