Fix panic from update_href in HTMLAnchorElement

This commit is contained in:
Keith Yeung 2016-05-19 04:46:51 -04:00
parent dea610986d
commit deadfa65cc

View file

@ -80,8 +80,8 @@ impl HTMLAnchorElement {
} }
// https://html.spec.whatwg.org/multipage/#update-href // https://html.spec.whatwg.org/multipage/#update-href
fn update_href(&self, url: &Url) { fn update_href(&self, url: DOMString) {
self.upcast::<Element>().set_string_attribute(&atom!("href"), DOMString::from(url.as_str())); self.upcast::<Element>().set_string_attribute(&atom!("href"), url);
} }
} }
@ -160,15 +160,20 @@ impl HTMLAnchorElementMethods for HTMLAnchorElement {
// Step 1. // Step 1.
self.reinitialize_url(); self.reinitialize_url();
// Step 2.
let url = match self.url.borrow_mut().as_mut() {
// Step 3. // Step 3.
if let Some(url) = self.url.borrow_mut().as_mut() { Some(ref url) if url.scheme() == "javascript" => return,
if url.scheme() == "javascript" { return; } None => return,
// Steps 4-5. // Steps 4-5.
Some(url) => {
UrlHelper::SetHash(url, value); UrlHelper::SetHash(url, value);
DOMString::from(url.as_str())
}
};
// Step 6. // Step 6.
self.update_href(url); self.update_href(url);
} }
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-host // https://html.spec.whatwg.org/multipage/#dom-hyperlink-host
fn Host(&self) -> USVString { fn Host(&self) -> USVString {
@ -194,17 +199,20 @@ impl HTMLAnchorElementMethods for HTMLAnchorElement {
// Step 1. // Step 1.
self.reinitialize_url(); self.reinitialize_url();
// Step 2.
let url = match self.url.borrow_mut().as_mut() {
// Step 3. // Step 3.
if let Some(url) = self.url.borrow_mut().as_mut() { Some(ref url) if url.cannot_be_a_base() => return,
if url.cannot_be_a_base() { None => return,
return;
}
// Step 4. // Step 4.
Some(url) => {
UrlHelper::SetHost(url, value); UrlHelper::SetHost(url, value);
DOMString::from(url.as_str())
}
};
// Step 5. // Step 5.
self.update_href(url); self.update_href(url);
} }
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-hostname // https://html.spec.whatwg.org/multipage/#dom-hyperlink-hostname
fn Hostname(&self) -> USVString { fn Hostname(&self) -> USVString {
@ -226,17 +234,20 @@ impl HTMLAnchorElementMethods for HTMLAnchorElement {
// Step 1. // Step 1.
self.reinitialize_url(); self.reinitialize_url();
// Step 2.
let url = match self.url.borrow_mut().as_mut() {
// Step 3. // Step 3.
if let Some(url) = self.url.borrow_mut().as_mut() { Some(ref url) if url.cannot_be_a_base() => return,
if url.cannot_be_a_base() { None => return,
return;
}
// Step 4. // Step 4.
Some(url) => {
UrlHelper::SetHostname(url, value); UrlHelper::SetHostname(url, value);
DOMString::from(url.as_str())
}
};
// Step 5. // Step 5.
self.update_href(url); self.update_href(url);
} }
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-href // https://html.spec.whatwg.org/multipage/#dom-hyperlink-href
fn Href(&self) -> USVString { fn Href(&self) -> USVString {
@ -282,17 +293,20 @@ impl HTMLAnchorElementMethods for HTMLAnchorElement {
// Step 1. // Step 1.
self.reinitialize_url(); self.reinitialize_url();
// Step 2.
let url = match self.url.borrow_mut().as_mut() {
// Step 3. // Step 3.
if let Some(url) = self.url.borrow_mut().as_mut() { Some(ref url) if url.host().is_none() || url.cannot_be_a_base() => return,
if url.host().is_none() || url.cannot_be_a_base() { None => return,
return;
}
// Step 4. // Step 4.
Some(url) => {
UrlHelper::SetPassword(url, value); UrlHelper::SetPassword(url, value);
DOMString::from(url.as_str())
}
};
// Step 5. // Step 5.
self.update_href(url); self.update_href(url);
} }
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-pathname // https://html.spec.whatwg.org/multipage/#dom-hyperlink-pathname
fn Pathname(&self) -> USVString { fn Pathname(&self) -> USVString {
@ -312,15 +326,20 @@ impl HTMLAnchorElementMethods for HTMLAnchorElement {
// Step 1. // Step 1.
self.reinitialize_url(); self.reinitialize_url();
// Step 2.
let url = match self.url.borrow_mut().as_mut() {
// Step 3. // Step 3.
if let Some(url) = self.url.borrow_mut().as_mut() { Some(ref url) if url.cannot_be_a_base() => return,
if url.cannot_be_a_base() { return; } None => return,
// Step 5. // Step 5.
Some(url) => {
UrlHelper::SetPathname(url, value); UrlHelper::SetPathname(url, value);
DOMString::from(url.as_str())
}
};
// Step 6. // Step 6.
self.update_href(url); self.update_href(url);
} }
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-port // https://html.spec.whatwg.org/multipage/#dom-hyperlink-port
fn Port(&self) -> USVString { fn Port(&self) -> USVString {
@ -341,18 +360,20 @@ impl HTMLAnchorElementMethods for HTMLAnchorElement {
self.reinitialize_url(); self.reinitialize_url();
// Step 3. // Step 3.
if let Some(url) = self.url.borrow_mut().as_mut() { let url = match self.url.borrow_mut().as_mut() {
if url.host().is_none() || Some(ref url) if url.host().is_none() ||
url.cannot_be_a_base() || url.cannot_be_a_base() ||
url.scheme() == "file" { url.scheme() == "file" => return,
return; None => return,
}
// Step 4. // Step 4.
Some(url) => {
UrlHelper::SetPort(url, value); UrlHelper::SetPort(url, value);
DOMString::from(url.as_str())
}
};
// Step 5. // Step 5.
self.update_href(url); self.update_href(url);
} }
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-protocol // https://html.spec.whatwg.org/multipage/#dom-hyperlink-protocol
fn Protocol(&self) -> USVString { fn Protocol(&self) -> USVString {
@ -372,14 +393,18 @@ impl HTMLAnchorElementMethods for HTMLAnchorElement {
// Step 1. // Step 1.
self.reinitialize_url(); self.reinitialize_url();
let url = match self.url.borrow_mut().as_mut() {
// Step 2. // Step 2.
if let Some(url) = self.url.borrow_mut().as_mut() { None => return,
// Step 3. // Step 3.
Some(url) => {
UrlHelper::SetProtocol(url, value); UrlHelper::SetProtocol(url, value);
DOMString::from(url.as_str())
}
};
// Step 4. // Step 4.
self.update_href(url); self.update_href(url);
} }
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-search // https://html.spec.whatwg.org/multipage/#dom-hyperlink-search
fn Search(&self) -> USVString { fn Search(&self) -> USVString {
@ -399,16 +424,21 @@ impl HTMLAnchorElementMethods for HTMLAnchorElement {
// Step 1. // Step 1.
self.reinitialize_url(); self.reinitialize_url();
// Step 2.
let url = match self.url.borrow_mut().as_mut() {
// Step 3. // Step 3.
if let Some(url) = self.url.borrow_mut().as_mut() { None => return,
// Steps 4-5. // Steps 4-5.
// TODO add this element's node document character encoding as // TODO add this element's node document character encoding as
// encoding override (as described in the spec) // encoding override (as described in the spec)
Some(url) => {
UrlHelper::SetSearch(url, value); UrlHelper::SetSearch(url, value);
DOMString::from(url.as_str())
}
};
// Step 6. // Step 6.
self.update_href(url); self.update_href(url);
} }
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-username // https://html.spec.whatwg.org/multipage/#dom-hyperlink-username
fn Username(&self) -> USVString { fn Username(&self) -> USVString {
@ -428,18 +458,20 @@ impl HTMLAnchorElementMethods for HTMLAnchorElement {
// Step 1. // Step 1.
self.reinitialize_url(); self.reinitialize_url();
// Step 2.
let url = match self.url.borrow_mut().as_mut() {
// Step 3. // Step 3.
if let Some(url) = self.url.borrow_mut().as_mut() { Some(ref url) if url.host().is_none() || url.cannot_be_a_base() => return,
if url.host().is_none() || url.cannot_be_a_base() { None => return,
return;
}
// Step 4. // Step 4.
Some(url) => {
UrlHelper::SetUsername(url, value); UrlHelper::SetUsername(url, value);
DOMString::from(url.as_str())
}
};
// Step 5. // Step 5.
self.update_href(url); self.update_href(url);
} }
}
// https://html.spec.whatwg.org/multipage/#dom-hyperlink-href // https://html.spec.whatwg.org/multipage/#dom-hyperlink-href
fn Stringifier(&self) -> DOMString { fn Stringifier(&self) -> DOMString {