quirks.rs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 = 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().ascii_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), SchemeType::from(scheme));
  91. match result {
  92. Ok((h, remaining)) => {
  93. host = h;
  94. opt_port = if let Some(remaining) = remaining.split_prefix(':') {
  95. Parser::parse_port(remaining, || default_port(scheme), Context::Setter)
  96. .ok().map(|(port, _remaining)| port)
  97. } else {
  98. None
  99. };
  100. }
  101. Err(_) => return Err(())
  102. }
  103. }
  104. url.set_host_internal(host, opt_port);
  105. Ok(())
  106. }
  107. /// Getter for https://url.spec.whatwg.org/#dom-url-hostname
  108. #[inline]
  109. pub fn hostname(url: &Url) -> &str {
  110. url.host_str().unwrap_or("")
  111. }
  112. /// Setter for https://url.spec.whatwg.org/#dom-url-hostname
  113. pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
  114. if url.cannot_be_a_base() {
  115. return Err(())
  116. }
  117. let result = Parser::parse_host(Input::new(new_hostname), SchemeType::from(url.scheme()));
  118. if let Ok((host, _remaining)) = result {
  119. url.set_host_internal(host, None);
  120. Ok(())
  121. } else {
  122. Err(())
  123. }
  124. }
  125. /// Getter for https://url.spec.whatwg.org/#dom-url-port
  126. #[inline]
  127. pub fn port(url: &Url) -> &str {
  128. &url[Position::BeforePort..Position::AfterPort]
  129. }
  130. /// Setter for https://url.spec.whatwg.org/#dom-url-port
  131. pub fn set_port(url: &mut Url, new_port: &str) -> Result<(), ()> {
  132. let result;
  133. {
  134. // has_host implies !cannot_be_a_base
  135. let scheme = url.scheme();
  136. if !url.has_host() || url.host() == Some(Host::Domain("")) || scheme == "file" {
  137. return Err(())
  138. }
  139. result = Parser::parse_port(Input::new(new_port), || default_port(scheme), Context::Setter)
  140. }
  141. if let Ok((new_port, _remaining)) = result {
  142. url.set_port_internal(new_port);
  143. Ok(())
  144. } else {
  145. Err(())
  146. }
  147. }
  148. /// Getter for https://url.spec.whatwg.org/#dom-url-pathname
  149. #[inline]
  150. pub fn pathname(url: &Url) -> &str {
  151. url.path()
  152. }
  153. /// Setter for https://url.spec.whatwg.org/#dom-url-pathname
  154. pub fn set_pathname(url: &mut Url, new_pathname: &str) {
  155. if !url.cannot_be_a_base() {
  156. url.set_path(new_pathname)
  157. }
  158. }
  159. /// Getter for https://url.spec.whatwg.org/#dom-url-search
  160. pub fn search(url: &Url) -> &str {
  161. trim(&url[Position::AfterPath..Position::AfterQuery])
  162. }
  163. /// Setter for https://url.spec.whatwg.org/#dom-url-search
  164. pub fn set_search(url: &mut Url, new_search: &str) {
  165. url.set_query(match new_search {
  166. "" => None,
  167. _ if new_search.starts_with('?') => Some(&new_search[1..]),
  168. _ => Some(new_search),
  169. })
  170. }
  171. /// Getter for https://url.spec.whatwg.org/#dom-url-hash
  172. pub fn hash(url: &Url) -> &str {
  173. trim(&url[Position::AfterQuery..])
  174. }
  175. /// Setter for https://url.spec.whatwg.org/#dom-url-hash
  176. pub fn set_hash(url: &mut Url, new_hash: &str) {
  177. if url.scheme() != "javascript" {
  178. url.set_fragment(match new_hash {
  179. "" => None,
  180. _ if new_hash.starts_with('#') => Some(&new_hash[1..]),
  181. _ => Some(new_hash),
  182. })
  183. }
  184. }
  185. fn trim(s: &str) -> &str {
  186. if s.len() == 1 {
  187. ""
  188. } else {
  189. s
  190. }
  191. }