Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: Source/core/html/DOMURLUtils.cpp

Issue 24066010: Implement URLUtils and URL interface (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: updates based on code review comments Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/html/DOMURLUtils.h ('k') | Source/core/html/HTMLAnchorElement.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2012 Motorola Mobility Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include "config.h"
28 #include "core/html/DOMURLUtils.h"
29
30 #include "weborigin/KURL.h"
31 #include "weborigin/KnownPorts.h"
32 #include "weborigin/SecurityOrigin.h"
33
34 namespace WebCore {
35
36 String DOMURLUtils::href(DOMURLUtils* impl)
37 {
38 const KURL& url = impl->url();
39 if (url.isNull())
40 return impl->input();
41 return url.string();
42 }
43
44 void DOMURLUtils::setHref(DOMURLUtils* impl, const String& value)
45 {
46 impl->setInput(value);
47 }
48
49 String DOMURLUtils::toString(DOMURLUtils* impl)
50 {
51 return href(impl);
52 }
53
54 String DOMURLUtils::origin(DOMURLUtils* impl)
55 {
56 const KURL& url = impl->url();
57 if (url.isNull())
58 return "";
59
60 RefPtr<SecurityOrigin> origin = SecurityOrigin::create(url);
61 return origin->toString();
62 }
63
64 String DOMURLUtils::protocol(DOMURLUtils* impl)
65 {
66 return impl->url().protocol() + ":";
67 }
68
69 void DOMURLUtils::setProtocol(DOMURLUtils* impl, const String& value)
70 {
71 KURL url = impl->url();
72 if (url.isNull())
73 return;
74 url.setProtocol(value);
75 impl->setURL(url);
76 }
77
78 String DOMURLUtils::username(DOMURLUtils* impl)
79 {
80 return impl->url().user();
81 }
82
83 void DOMURLUtils::setUsername(DOMURLUtils* impl, const String& value)
84 {
85 KURL url = impl->url();
86 if (url.isNull())
87 return;
88 url.setUser(value);
89 impl->setURL(url);
90 }
91
92 String DOMURLUtils::password(DOMURLUtils* impl)
93 {
94 return impl->url().pass();
95 }
96
97 void DOMURLUtils::setPassword(DOMURLUtils* impl, const String& value)
98 {
99 KURL url = impl->url();
100 if (url.isNull())
101 return;
102 url.setPass(value);
103 impl->setURL(url);
104 }
105
106 String DOMURLUtils::host(DOMURLUtils* impl)
107 {
108 const KURL& url = impl->url();
109 if (url.hostEnd() == url.pathStart())
110 return url.host();
111 if (isDefaultPortForProtocol(url.port(), url.protocol()))
112 return url.host();
113 return url.host() + ":" + String::number(url.port());
114 }
115
116 // This function does not allow leading spaces before the port number.
117 static unsigned parsePortFromStringPosition(const String& value, unsigned portSt art, unsigned& portEnd)
118 {
119 portEnd = portStart;
120 while (isASCIIDigit(value[portEnd]))
121 ++portEnd;
122 return value.substring(portStart, portEnd - portStart).toUInt();
123 }
124
125 void DOMURLUtils::setHost(DOMURLUtils* impl, const String& value)
126 {
127 // Update once spec bug is resolved: https://www.w3.org/Bugs/Public/show_bug .cgi?id=23474
128 if (value.isEmpty())
129 return;
130
131 KURL url = impl->url();
132 if (!url.canSetHostOrPort())
133 return;
134
135 size_t separator = value.find(':');
136 if (!separator)
137 return;
138
139 if (separator == kNotFound) {
140 url.setHost(value);
141 } else {
142 unsigned portEnd;
143 unsigned port = parsePortFromStringPosition(value, separator + 1, portEn d);
144 if (!port) {
145 // Update once spec bug is resolved: https://www.w3.org/Bugs/Public/ show_bug.cgi?id=23463
146
147 // http://dev.w3.org/html5/spec/infrastructure.html#url-decompositio n-idl-attributes
148 // specifically goes against RFC 3986 (p3.2) and
149 // requires setting the port to "0" if it is set to empty string.
150 url.setHostAndPort(value.substring(0, separator + 1) + "0");
151 } else {
152 if (isDefaultPortForProtocol(port, url.protocol()))
153 url.setHostAndPort(value.substring(0, separator));
154 else
155 url.setHostAndPort(value.substring(0, portEnd));
156 }
157 }
158
159 impl->setURL(url);
160 }
161
162 String DOMURLUtils::hostname(DOMURLUtils* impl)
163 {
164 return impl->url().host();
165 }
166
167 void DOMURLUtils::setHostname(DOMURLUtils* impl, const String& value)
168 {
169 KURL url = impl->url();
170 if (!url.canSetHostOrPort())
171 return;
172
173 // Before setting new value:
174 // Remove all leading U+002F SOLIDUS ("/") characters.
175 unsigned i = 0;
176 unsigned hostLength = value.length();
177 while (value[i] == '/')
178 i++;
179
180 if (i == hostLength)
181 return;
182
183 url.setHost(value.substring(i));
184
185 impl->setURL(url);
186 }
187
188 String DOMURLUtils::port(DOMURLUtils* impl)
189 {
190 const KURL& url = impl->url();
191 if (url.hasPort())
192 return String::number(url.port());
193
194 return emptyString();
195 }
196
197 void DOMURLUtils::setPort(DOMURLUtils* impl, const String& value)
198 {
199 KURL url = impl->url();
200 if (!url.canSetHostOrPort())
201 return;
202
203 // http://dev.w3.org/html5/spec/infrastructure.html#url-decomposition-idl-at tributes
204 // specifically goes against RFC 3986 (p3.2) and
205 // requires setting the port to "0" if it is set to empty string.
206 unsigned port = value.toUInt();
207 if (isDefaultPortForProtocol(port, url.protocol()))
208 url.removePort();
209 else
210 url.setPort(port);
211
212 impl->setURL(url);
213 }
214
215 String DOMURLUtils::pathname(DOMURLUtils* impl)
216 {
217 return impl->url().path();
218 }
219
220 void DOMURLUtils::setPathname(DOMURLUtils* impl, const String& value)
221 {
222 KURL url = impl->url();
223 if (!url.canSetPathname())
224 return;
225
226 if (value[0] == '/')
227 url.setPath(value);
228 else
229 url.setPath("/" + value);
230
231 impl->setURL(url);
232 }
233
234 String DOMURLUtils::search(DOMURLUtils* impl)
235 {
236 String query = impl->url().query();
237 return query.isEmpty() ? emptyString() : "?" + query;
238 }
239
240 void DOMURLUtils::setSearch(DOMURLUtils* impl, const String& value)
241 {
242 KURL url = impl->url();
243 if (!url.isValid())
244 return;
245
246 String newSearch = value[0] == '?' ? value.substring(1) : value;
247 // Make sure that '#' in the query does not leak to the hash.
248 url.setQuery(newSearch.replaceWithLiteral('#', "%23"));
249
250 impl->setURL(url);
251 }
252
253 String DOMURLUtils::hash(DOMURLUtils* impl)
254 {
255 String fragmentIdentifier = impl->url().fragmentIdentifier();
256 if (fragmentIdentifier.isEmpty())
257 return emptyString();
258 return AtomicString(String("#" + fragmentIdentifier));
259 }
260
261 void DOMURLUtils::setHash(DOMURLUtils* impl, const String& value)
262 {
263 KURL url = impl->url();
264 if (url.isNull())
265 return;
266
267 if (value[0] == '#')
268 url.setFragmentIdentifier(value.substring(1));
269 else
270 url.setFragmentIdentifier(value);
271
272 impl->setURL(url);
273 }
274
275 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/html/DOMURLUtils.h ('k') | Source/core/html/HTMLAnchorElement.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698