webidl.rs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2016 Simon Sapin.
  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. use {Url, ParseError};
  9. /// https://url.spec.whatwg.org/#api
  10. pub struct WebIdl;
  11. impl WebIdl {
  12. /// **Not implemented yet** https://url.spec.whatwg.org/#dom-url-domaintoascii
  13. pub fn domain_to_ascii(_domain: &str) -> String {
  14. unimplemented!() // FIXME
  15. }
  16. /// **Not implemented yet** https://url.spec.whatwg.org/#dom-url-domaintounicode
  17. pub fn domain_to_unicode(_domain: &str) -> String {
  18. unimplemented!() // FIXME
  19. }
  20. pub fn href(url: &Url) -> &str {
  21. &url.serialization
  22. }
  23. pub fn set_href(url: &mut Url, value: &str) -> Result<(), ParseError> {
  24. *url = try!(Url::parse(value));
  25. Ok(())
  26. }
  27. /// Getter for https://url.spec.whatwg.org/#dom-url-origin
  28. pub fn origin(url: &Url) -> String {
  29. url.origin().unicode_serialization()
  30. }
  31. /// Getter for https://url.spec.whatwg.org/#dom-url-protocol
  32. #[inline]
  33. pub fn protocol(url: &Url) -> &str {
  34. debug_assert!(url.byte_at(url.scheme_end) == b':');
  35. url.slice(..url.scheme_end + 1)
  36. }
  37. /// Setter for https://url.spec.whatwg.org/#dom-url-protocol
  38. pub fn set_protocol(url: &mut Url, new_protocol: &str) {
  39. let _ = url.set_scheme_internal(new_protocol, true);
  40. }
  41. /// Getter for https://url.spec.whatwg.org/#dom-url-username
  42. #[inline]
  43. pub fn username(url: &Url) -> &str {
  44. url.username()
  45. }
  46. /// **Not implemented yet** Setter for https://url.spec.whatwg.org/#dom-url-username
  47. pub fn set_username(_url: &mut Url, _new_username: &str) {
  48. unimplemented!() // FIXME
  49. }
  50. /// Getter for https://url.spec.whatwg.org/#dom-url-password
  51. #[inline]
  52. pub fn password(url: &Url) -> &str {
  53. url.password().unwrap_or("")
  54. }
  55. /// **Not implemented yet** Setter for https://url.spec.whatwg.org/#dom-url-password
  56. pub fn set_password(_url: &mut Url, _new_password: &str) {
  57. unimplemented!() // FIXME
  58. }
  59. /// Getter for https://url.spec.whatwg.org/#dom-url-host
  60. #[inline]
  61. pub fn host(url: &Url) -> &str {
  62. let host = url.slice(url.host_start..url.path_start);
  63. host
  64. }
  65. /// **Not implemented yet** Setter for https://url.spec.whatwg.org/#dom-url-host
  66. pub fn set_host(_url: &mut Url, _new_host: &str) {
  67. unimplemented!() // FIXME
  68. }
  69. /// Getter for https://url.spec.whatwg.org/#dom-url-hostname
  70. #[inline]
  71. pub fn hostname(url: &Url) -> &str {
  72. url.host_str().unwrap_or("")
  73. }
  74. /// **Not implemented yet** Setter for https://url.spec.whatwg.org/#dom-url-hostname
  75. pub fn set_hostname(_url: &mut Url, _new_hostname: &str) {
  76. unimplemented!() // FIXME
  77. }
  78. /// Getter for https://url.spec.whatwg.org/#dom-url-port
  79. #[inline]
  80. pub fn port(url: &Url) -> &str {
  81. if url.port.is_some() {
  82. debug_assert!(url.byte_at(url.host_end) == b':');
  83. url.slice(url.host_end + 1..url.path_start)
  84. } else {
  85. ""
  86. }
  87. }
  88. /// **Not implemented yet** Setter for https://url.spec.whatwg.org/#dom-url-port
  89. pub fn set_port(_url: &mut Url, _new_port: &str) {
  90. unimplemented!() // FIXME
  91. }
  92. /// Getter for https://url.spec.whatwg.org/#dom-url-pathname
  93. #[inline]
  94. pub fn pathname(url: &Url) -> &str {
  95. url.path()
  96. }
  97. /// **Not implemented yet** Setter for https://url.spec.whatwg.org/#dom-url-pathname
  98. pub fn set_pathname(_url: &mut Url, _new_pathname: &str) {
  99. unimplemented!() // FIXME
  100. }
  101. /// Getter for https://url.spec.whatwg.org/#dom-url-search
  102. pub fn search(url: &Url) -> &str {
  103. match (url.query_start, url.fragment_start) {
  104. (Some(query_start), None) if {
  105. debug_assert!(url.byte_at(query_start) == b'?');
  106. // If the query (after ?) is not empty
  107. (query_start as usize) < url.serialization.len() - 1
  108. } => url.slice(query_start..),
  109. (Some(query_start), Some(fragment_start)) if {
  110. debug_assert!(url.byte_at(query_start) == b'?');
  111. // If the fragment (after ?) is not empty
  112. query_start < fragment_start
  113. } => url.slice(query_start..fragment_start),
  114. _ => "",
  115. }
  116. }
  117. /// Setter for https://url.spec.whatwg.org/#dom-url-search
  118. pub fn set_search(url: &mut Url, new_search: &str) {
  119. url.set_query(match new_search {
  120. "" => None,
  121. _ if new_search.starts_with('?') => Some(&new_search[1..]),
  122. _ => Some(new_search),
  123. })
  124. }
  125. /// **Not implemented yet** Getter for https://url.spec.whatwg.org/#dom-url-searchparams
  126. pub fn search_params(_url: &Url) -> Vec<(String, String)> {
  127. unimplemented!(); // FIXME
  128. }
  129. /// Getter for https://url.spec.whatwg.org/#dom-url-hash
  130. pub fn hash(url: &Url) -> &str {
  131. match url.fragment_start {
  132. Some(start) if {
  133. debug_assert!(url.byte_at(start) == b'#');
  134. // If the fragment (after #) is not empty
  135. (start as usize) < url.serialization.len() - 1
  136. } => url.slice(start..),
  137. _ => "",
  138. }
  139. }
  140. /// Setter for https://url.spec.whatwg.org/#dom-url-hash
  141. pub fn set_hash(url: &mut Url, new_hash: &str) {
  142. if url.scheme() != "javascript" {
  143. url.set_fragment(match new_hash {
  144. "" => None,
  145. _ if new_hash.starts_with('#') => Some(&new_hash[1..]),
  146. _ => Some(new_hash),
  147. })
  148. }
  149. }
  150. }