quirks.rs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2016 The rust-url developers.
  2. //
  3. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  4. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  5. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  6. // option. This file may not be copied, modified, or distributed
  7. // except according to those terms.
  8. //! Getters and setters for URL components implemented per https://url.spec.whatwg.org/#api
  9. //!
  10. //! Unless you need to be interoperable with web browsers,
  11. //! you probably want to use `Url` method instead.
  12. use {Url, Position, Host, ParseError, idna};
  13. use parser::{Parser, SchemeType, default_port, Context, Input};
  14. /// https://url.spec.whatwg.org/#dom-url-domaintoascii
  15. pub fn domain_to_ascii(domain: &str) -> String {
  16. match Host::parse(domain) {
  17. Ok(Host::Domain(domain)) => domain,
  18. _ => String::new(),
  19. }
  20. }
  21. /// https://url.spec.whatwg.org/#dom-url-domaintounicode
  22. pub fn domain_to_unicode(domain: &str) -> String {
  23. match Host::parse(domain) {
  24. Ok(Host::Domain(ref domain)) => {
  25. let (unicode, _errors) = idna::domain_to_unicode(domain);
  26. unicode
  27. }
  28. _ => String::new(),
  29. }
  30. }
  31. /// Getter for https://url.spec.whatwg.org/#dom-url-href
  32. pub fn href(url: &Url) -> &str {
  33. url.as_str()
  34. }
  35. /// Setter for https://url.spec.whatwg.org/#dom-url-href
  36. pub fn set_href(url: &mut Url, value: &str) -> Result<(), ParseError> {
  37. *url = try!(Url::parse(value));
  38. Ok(())
  39. }
  40. /// Getter for https://url.spec.whatwg.org/#dom-url-origin
  41. pub fn origin(url: &Url) -> String {
  42. url.origin().unicode_serialization()
  43. }
  44. /// Getter for https://url.spec.whatwg.org/#dom-url-protocol
  45. #[inline]
  46. pub fn protocol(url: &Url) -> &str {
  47. &url.as_str()[..url.scheme().len() + ":".len()]
  48. }
  49. /// Setter for https://url.spec.whatwg.org/#dom-url-protocol
  50. pub fn set_protocol(url: &mut Url, mut new_protocol: &str) -> Result<(), ()> {
  51. // The scheme state in the spec ignores everything after the first `:`,
  52. // but `set_scheme` errors if there is more.
  53. if let Some(position) = new_protocol.find(':') {
  54. new_protocol = &new_protocol[..position];
  55. }
  56. url.set_scheme(new_protocol)
  57. }
  58. /// Getter for https://url.spec.whatwg.org/#dom-url-username
  59. #[inline]
  60. pub fn username(url: &Url) -> &str {
  61. url.username()
  62. }
  63. /// Setter for https://url.spec.whatwg.org/#dom-url-username
  64. pub fn set_username(url: &mut Url, new_username: &str) -> Result<(), ()> {
  65. url.set_username(new_username)
  66. }
  67. /// Getter for https://url.spec.whatwg.org/#dom-url-password
  68. #[inline]
  69. pub fn password(url: &Url) -> &str {
  70. url.password().unwrap_or("")
  71. }
  72. /// Setter for https://url.spec.whatwg.org/#dom-url-password
  73. pub fn set_password(url: &mut Url, new_password: &str) -> Result<(), ()> {
  74. url.set_password(if new_password.is_empty() { None } else { Some(new_password) })
  75. }
  76. /// Getter for https://url.spec.whatwg.org/#dom-url-host
  77. #[inline]
  78. pub fn host(url: &Url) -> &str {
  79. &url[Position::BeforeHost..Position::AfterPort]
  80. }
  81. /// Setter for https://url.spec.whatwg.org/#dom-url-host
  82. pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> {
  83. if url.cannot_be_a_base() {
  84. return Err(())
  85. }
  86. let host;
  87. let opt_port;
  88. {
  89. let scheme = url.scheme();
  90. let result = Parser::parse_host(Input::new(new_host),
  91. SchemeType::from(scheme), |_| ());
  92. match result {
  93. Ok((h, remaining)) => {
  94. host = h;
  95. opt_port = if let Some(remaining) = remaining.split_prefix(':') {
  96. Parser::parse_port(remaining, |_| (), || default_port(scheme),
  97. Context::Setter)
  98. .ok().map(|(port, _remaining)| port)
  99. } else {
  100. None
  101. };
  102. }
  103. Err(_) => return Err(())
  104. }
  105. }
  106. url.set_host_internal(host, opt_port);
  107. Ok(())
  108. }
  109. /// Getter for https://url.spec.whatwg.org/#dom-url-hostname
  110. #[inline]
  111. pub fn hostname(url: &Url) -> &str {
  112. url.host_str().unwrap_or("")
  113. }
  114. /// Setter for https://url.spec.whatwg.org/#dom-url-hostname
  115. pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
  116. if url.cannot_be_a_base() {
  117. return Err(())
  118. }
  119. let result = Parser::parse_host(Input::new(new_hostname),
  120. SchemeType::from(url.scheme()), |_| ());
  121. if let Ok((host, _remaining)) = result {
  122. url.set_host_internal(host, None);
  123. Ok(())
  124. } else {
  125. Err(())
  126. }
  127. }
  128. /// Getter for https://url.spec.whatwg.org/#dom-url-port
  129. #[inline]
  130. pub fn port(url: &Url) -> &str {
  131. &url[Position::BeforePort..Position::AfterPort]
  132. }
  133. /// Setter for https://url.spec.whatwg.org/#dom-url-port
  134. pub fn set_port(url: &mut Url, new_port: &str) -> Result<(), ()> {
  135. let result;
  136. {
  137. // has_host implies !cannot_be_a_base
  138. let scheme = url.scheme();
  139. if !url.has_host() || scheme == "file" {
  140. return Err(())
  141. }
  142. result = Parser::parse_port(Input::new(new_port),
  143. |_| (), || default_port(scheme), Context::Setter)
  144. }
  145. if let Ok((new_port, _remaining)) = result {
  146. url.set_port_internal(new_port);
  147. Ok(())
  148. } else {
  149. Err(())
  150. }
  151. }
  152. /// Getter for https://url.spec.whatwg.org/#dom-url-pathname
  153. #[inline]
  154. pub fn pathname(url: &Url) -> &str {
  155. url.path()
  156. }
  157. /// Setter for https://url.spec.whatwg.org/#dom-url-pathname
  158. pub fn set_pathname(url: &mut Url, new_pathname: &str) {
  159. if !url.cannot_be_a_base() {
  160. url.set_path(new_pathname)
  161. }
  162. }
  163. /// Getter for https://url.spec.whatwg.org/#dom-url-search
  164. pub fn search(url: &Url) -> &str {
  165. trim(&url[Position::AfterPath..Position::AfterQuery])
  166. }
  167. /// Setter for https://url.spec.whatwg.org/#dom-url-search
  168. pub fn set_search(url: &mut Url, new_search: &str) {
  169. url.set_query(match new_search {
  170. "" => None,
  171. _ if new_search.starts_with('?') => Some(&new_search[1..]),
  172. _ => Some(new_search),
  173. })
  174. }
  175. /// Getter for https://url.spec.whatwg.org/#dom-url-hash
  176. pub fn hash(url: &Url) -> &str {
  177. trim(&url[Position::AfterQuery..])
  178. }
  179. /// Setter for https://url.spec.whatwg.org/#dom-url-hash
  180. pub fn set_hash(url: &mut Url, new_hash: &str) {
  181. if url.scheme() != "javascript" {
  182. url.set_fragment(match new_hash {
  183. "" => None,
  184. _ if new_hash.starts_with('#') => Some(&new_hash[1..]),
  185. _ => Some(new_hash),
  186. })
  187. }
  188. }
  189. fn trim(s: &str) -> &str {
  190. if s.len() == 1 {
  191. ""
  192. } else {
  193. s
  194. }
  195. }