Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / DOMURLUtils.cpp
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/dom/DOMURLUtils.h"
29
30 #include "platform/weborigin/KnownPorts.h"
31
32 namespace WebCore {
33
34 void DOMURLUtils::setHref(DOMURLUtils& impl, const String& value)
35 {
36     impl.setInput(value);
37 }
38
39 void DOMURLUtils::setProtocol(DOMURLUtils& impl, const String& value)
40 {
41     KURL url = impl.url();
42     if (url.isNull())
43         return;
44     url.setProtocol(value);
45     impl.setURL(url);
46 }
47
48 void DOMURLUtils::setUsername(DOMURLUtils& impl, const String& value)
49 {
50     KURL url = impl.url();
51     if (url.isNull())
52         return;
53     url.setUser(value);
54     impl.setURL(url);
55 }
56
57 void DOMURLUtils::setPassword(DOMURLUtils& impl, const String& value)
58 {
59     KURL url = impl.url();
60     if (url.isNull())
61         return;
62     url.setPass(value);
63     impl.setURL(url);
64 }
65
66 void DOMURLUtils::setHost(DOMURLUtils& impl, const String& value)
67 {
68     if (value.isEmpty())
69         return;
70
71     KURL url = impl.url();
72     if (!url.canSetHostOrPort())
73         return;
74
75     url.setHostAndPort(value);
76     impl.setURL(url);
77 }
78
79 void DOMURLUtils::setHostname(DOMURLUtils& impl, const String& value)
80 {
81     KURL url = impl.url();
82     if (!url.canSetHostOrPort())
83         return;
84
85     // Before setting new value:
86     // Remove all leading U+002F SOLIDUS ("/") characters.
87     unsigned i = 0;
88     unsigned hostLength = value.length();
89     while (value[i] == '/')
90         i++;
91
92     if (i == hostLength)
93         return;
94
95     url.setHost(value.substring(i));
96
97     impl.setURL(url);
98 }
99
100 void DOMURLUtils::setPort(DOMURLUtils& impl, const String& value)
101 {
102     KURL url = impl.url();
103     if (!url.canSetHostOrPort())
104         return;
105
106     url.setPort(value);
107     impl.setURL(url);
108 }
109
110 void DOMURLUtils::setPathname(DOMURLUtils& impl, const String& value)
111 {
112     KURL url = impl.url();
113     if (!url.canSetPathname())
114         return;
115     url.setPath(value);
116     impl.setURL(url);
117 }
118
119 void DOMURLUtils::setSearch(DOMURLUtils& impl, const String& value)
120 {
121     KURL url = impl.url();
122     if (!url.isValid())
123         return;
124     url.setQuery(value);
125     impl.setURL(url);
126 }
127
128 void DOMURLUtils::setHash(DOMURLUtils& impl, const String& value)
129 {
130     KURL url = impl.url();
131     if (url.isNull())
132         return;
133
134     if (value[0] == '#')
135         url.setFragmentIdentifier(value.substring(1));
136     else
137         url.setFragmentIdentifier(value);
138
139     impl.setURL(url);
140 }
141
142 } // namespace WebCore