mirror of
https://github.com/servo/servo.git
synced 2025-07-22 23:03:42 +01:00
Add macro for reflecting URLs
This commit is contained in:
parent
e9f654d60a
commit
8cba6c7580
3 changed files with 32 additions and 15 deletions
|
@ -7,14 +7,13 @@ use dom::bindings::codegen::Bindings::HTMLFormElementBinding::HTMLFormElementMet
|
||||||
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLFormElementDerived};
|
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLFormElementDerived};
|
||||||
use dom::bindings::js::{JSRef, Temporary};
|
use dom::bindings::js::{JSRef, Temporary};
|
||||||
use dom::bindings::utils::{Reflectable, Reflector};
|
use dom::bindings::utils::{Reflectable, Reflector};
|
||||||
use dom::document::{Document, DocumentHelpers};
|
use dom::document::Document;
|
||||||
use dom::element::{Element, AttributeHandlers, HTMLFormElementTypeId};
|
use dom::element::{Element, AttributeHandlers, HTMLFormElementTypeId};
|
||||||
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
|
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
|
||||||
use dom::htmlelement::HTMLElement;
|
use dom::htmlelement::HTMLElement;
|
||||||
use dom::node::{Node, ElementNodeTypeId, document_from_node};
|
use dom::node::{Node, ElementNodeTypeId};
|
||||||
use servo_util::str::DOMString;
|
use servo_util::str::DOMString;
|
||||||
use std::ascii::OwnedStrAsciiExt;
|
use std::ascii::OwnedStrAsciiExt;
|
||||||
use url::UrlParser;
|
|
||||||
|
|
||||||
|
|
||||||
#[jstraceable]
|
#[jstraceable]
|
||||||
|
@ -51,17 +50,7 @@ impl<'a> HTMLFormElementMethods for JSRef<'a, HTMLFormElement> {
|
||||||
make_setter!(SetAcceptCharset, "accept-charset")
|
make_setter!(SetAcceptCharset, "accept-charset")
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/forms.html#dom-fs-action
|
// https://html.spec.whatwg.org/multipage/forms.html#dom-fs-action
|
||||||
fn Action(self) -> DOMString {
|
make_url_getter!(Action)
|
||||||
let elem: JSRef<Element> = ElementCast::from_ref(self);
|
|
||||||
let action = elem.get_string_attribute("action");
|
|
||||||
let doc = document_from_node(self).root();
|
|
||||||
let base = doc.url();
|
|
||||||
// https://html.spec.whatwg.org/multipage/infrastructure.html#reflect
|
|
||||||
match UrlParser::new().base_url(base).parse(action.as_slice()) {
|
|
||||||
Ok(parsed) => parsed.serialize(),
|
|
||||||
Err(_) => base.serialize()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/forms.html#dom-fs-action
|
// https://html.spec.whatwg.org/multipage/forms.html#dom-fs-action
|
||||||
make_setter!(SetAction, "action")
|
make_setter!(SetAction, "action")
|
||||||
|
|
|
@ -100,7 +100,7 @@ impl<'a> HTMLImageElementMethods for JSRef<'a, HTMLImageElement> {
|
||||||
|
|
||||||
make_setter!(SetAlt, "alt")
|
make_setter!(SetAlt, "alt")
|
||||||
|
|
||||||
make_getter!(Src)
|
make_url_getter!(Src)
|
||||||
|
|
||||||
make_setter!(SetSrc, "src")
|
make_setter!(SetSrc, "src")
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,34 @@ macro_rules! make_uint_getter(
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! make_url_getter(
|
||||||
|
( $attr:ident, $htmlname:expr ) => (
|
||||||
|
fn $attr(self) -> DOMString {
|
||||||
|
use dom::element::{Element, AttributeHandlers};
|
||||||
|
use dom::bindings::codegen::InheritTypes::ElementCast;
|
||||||
|
use dom::document::DocumentHelpers;
|
||||||
|
use dom::node::document_from_node;
|
||||||
|
use url::UrlParser;
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
use std::ascii::StrAsciiExt;
|
||||||
|
let elem: JSRef<Element> = ElementCast::from_ref(self);
|
||||||
|
let action = elem.get_string_attribute($htmlname);
|
||||||
|
let doc = document_from_node(self).root();
|
||||||
|
let base = doc.url();
|
||||||
|
// https://html.spec.whatwg.org/multipage/infrastructure.html#reflect
|
||||||
|
// XXXManishearth this doesn't handle `javascript:` urls properly
|
||||||
|
match UrlParser::new().base_url(base).parse(action.as_slice()) {
|
||||||
|
Ok(parsed) => parsed.serialize(),
|
||||||
|
Err(_) => base.serialize()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
($attr:ident) => {
|
||||||
|
make_url_getter!($attr, stringify!($attr).to_ascii_lower().as_slice())
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// concat_idents! doesn't work for function name positions, so
|
// concat_idents! doesn't work for function name positions, so
|
||||||
// we have to specify both the content name and the HTML name here
|
// we have to specify both the content name and the HTML name here
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue