Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow all global event handlers to be used as listeners #1244

Merged
merged 7 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/todomvc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl Model {
class="toggle"
checked=entry.completed
onclick=self.link.callback(move |_| Msg::Toggle(idx)) />
<label ondoubleclick=self.link.callback(move |_| Msg::ToggleEdit(idx))>{ &entry.description }</label>
<label ondblclick=self.link.callback(move |_| Msg::ToggleEdit(idx))>{ &entry.description }</label>
<button class="destroy" onclick=self.link.callback(move |_| Msg::Remove(idx)) />
</div>
{ self.view_entry_edit_input((idx, &entry)) }
Expand Down
2 changes: 2 additions & 0 deletions yew-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ proc-macro = true

[dependencies]
boolinator = "2.4.0"
cfg-if = "0.1"
lazy_static = "1.3.0"
proc-macro-hack = "0.5"
proc-macro2 = "1.0"
Expand All @@ -35,3 +36,4 @@ yew = { path = "../yew" }

[features]
doc_test = []
std_web = []
181 changes: 156 additions & 25 deletions yew-macro/src/html_tree/html_tag/tag_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,49 +54,159 @@ lazy_static! {
static ref LISTENER_SET: HashSet<&'static str> = {
HashSet::from_iter(
vec![
// Living Standard
// From: https://html.spec.whatwg.org/multipage/webappapis.html#globaleventhandlers
"onabort",
"onauxclick",
"onblur",
"oncancel",
"oncanplay",
"oncanplaythrough",
"onchange",
"onclick",
"ondoubleclick",
"onkeypress",
"onclose",
"oncontextmenu",
"oncuechange",
"ondblclick",
"ondrag",
"ondragend",
"ondragenter",
"ondragexit",
"ondragleave",
"ondragover",
"ondragstart",
"ondrop",
"ondurationchange",
"onemptied",
"onended",
"onerror",
"onfocus",
"onformdata",
"oninput",
"oninvalid",
"onkeydown",
"onkeypress",
"onkeyup",
"onload",
"onloadeddata",
"onloadedmetadata",
"onloadstart",
"onmousedown",
"onmousemove",
"onmouseout",
"onmouseenter",
"onmouseleave",
"onmousewheel",
"onmousemove",
"onmouseout",
"onmouseover",
"onmouseup",
"ontouchcancel",
"ontouchend",
"ontouchenter",
"ontouchmove",
"ontouchstart",
"onpause",
"onplay",
"onplaying",
"onprogress",
"onratechange",
"onreset",
"onresize",
"onscroll",
"onsecuritypolicyviolation",
"onseeked",
"onseeking",
"onselect",
"onslotchange",
"onstalled",
"onsubmit",
"onsuspend",
"ontimeupdate",
"ontoggle",
"onvolumechange",
"onwaiting",
"onwheel",

// Standard HTML Document and Element
// From: https://html.spec.whatwg.org/multipage/webappapis.html#documentandelementeventhandlers
"oncopy",
"oncut",
"onpaste",

// Others
// From: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers
"onanimationcancel",
"onanimationend",
"onanimationiteration",
"onanimationstart",
"ongotpointercapture",
"onloadend",
"onlostpointercapture",
"onpointercancel",
"onpointerdown",
"onpointerenter",
"onpointerleave",
"onpointerlockchange",
"onpointerlockerror",
"onpointermove",
"onpointerout",
"onpointerover",
"onpointerup",
"onscroll",
"onblur",
"onfocus",
"onsubmit",
"oninput",
"onchange",
"ondrag",
"ondragstart",
"ondragend",
"ondragenter",
"ondragleave",
"ondragover",
"ondragexit",
"ondrop",
"oncontextmenu",
"onselectionchange",
"onselectstart",
"onshow",
"ontouchcancel",
"ontouchend",
"ontouchmove",
"ontouchstart",
"ontransitioncancel",
"ontransitionend",
"ontransitionrun",
"ontransitionstart",
]
.into_iter(),
)
};
}

#[cfg(feature = "std_web")]
lazy_static! {
static ref UNSUPPORTED_LISTENER_SET: HashSet<&'static str> = {
HashSet::from_iter(
vec![
"oncancel",
"oncanplay",
"oncanplaythrough",
"onclose",
"oncuechange",
"ondurationchange",
"onemptied",
"onended",
"onformdata",
"oninvalid",
"onloadeddata",
"onloadedmetadata",
"onpause",
"onplay",
"onplaying",
"onratechange",
"onreset",
"onsecuritypolicyviolation",
"onseeked",
"onseeking",
"onselect",
"onstalled",
"onsuspend",
"ontimeupdate",
"ontoggle",
"onvolumechange",
"onwaiting",
"oncopy",
"oncut",
"onpaste",
"onanimationcancel",
"onanimationend",
"onanimationiteration",
"onanimationstart",
"onselectstart",
"onshow",
"ontransitioncancel",
"ontransitionend",
"ontransitionrun",
"ontransitionstart",
]
.into_iter(),
)
Expand All @@ -118,6 +228,17 @@ impl TagAttributes {
drained
}

#[allow(unused_variables)]
fn is_listener_supported(attr: &TagAttribute) -> bool {
cfg_if::cfg_if! {
if #[cfg(feature = "std_web")] {
!UNSUPPORTED_LISTENER_SET.contains(&attr.label.to_string().as_str())
} else {
true
}
}
}

fn drain_boolean(attrs: &mut Vec<TagAttribute>) -> Vec<TagAttribute> {
let mut i = 0;
let mut drained = Vec::new();
Expand Down Expand Up @@ -161,6 +282,16 @@ impl Parse for TagAttributes {

let mut listeners = Vec::new();
for listener in TagAttributes::drain_listeners(&mut attributes) {
if !TagAttributes::is_listener_supported(&listener) {
siku2 marked this conversation as resolved.
Show resolved Hide resolved
return Err(syn::Error::new_spanned(
&listener.label,
format!(
"the listener `{}` is only available when using web-sys",
&listener.label
),
));
}

listeners.push(listener);
}

Expand Down
2 changes: 1 addition & 1 deletion yew-stdweb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ thiserror = "1"
toml = { version = "0.5", optional = true }
wasm-bindgen = { version = "0.2.60", optional = true }
wasm-bindgen-futures = { version = "0.4", optional = true }
yew-macro = { version = "0.16.1", path = "../yew-macro" }
yew-macro = { version = "0.16.1", path = "../yew-macro", features = ["std_web"] }

# Changes here must be reflected in `build.rs`
[target.'cfg(all(target_arch = "wasm32", not(target_os="wasi"), not(cargo_web)))'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion yew-stdweb/examples/todomvc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl Model {
class="toggle"
checked=entry.completed
onclick=self.link.callback(move |_| Msg::Toggle(idx)) />
<label ondoubleclick=self.link.callback(move |_| Msg::ToggleEdit(idx))>{ &entry.description }</label>
<label ondblclick=self.link.callback(move |_| Msg::ToggleEdit(idx))>{ &entry.description }</label>
<button class="destroy" onclick=self.link.callback(move |_| Msg::Remove(idx)) />
</div>
{ self.view_entry_edit_input((idx, &entry)) }
Expand Down
7 changes: 6 additions & 1 deletion yew/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ optional = true
features = [
"AbortController",
"AbortSignal",
"AnimationEvent",
"BinaryType",
"Blob",
"BlobPropertyBag",
Expand All @@ -61,6 +62,7 @@ features = [
"DomTokenList",
"DragEvent",
"Element",
"ErrorEvent",
"Event",
"EventTarget",
"File",
Expand All @@ -72,13 +74,15 @@ features = [
"HtmlInputElement",
"HtmlSelectElement",
"HtmlTextAreaElement",
"InputEvent",
"KeyboardEvent",
"Location",
"MessageEvent",
"MouseEvent",
"Node",
"ObserverCallback",
"PointerEvent",
"ProgressEvent",
"ReferrerPolicy",
"Request",
"RequestCache",
Expand All @@ -90,6 +94,7 @@ features = [
"Storage",
"Text",
"TouchEvent",
"TransitionEvent",
"UiEvent",
"Url",
"WebSocket",
Expand Down Expand Up @@ -122,7 +127,7 @@ bincode = "~1.2.1"

[features]
default = ["services", "agent", "web_sys"]
std_web = ["stdweb"]
std_web = ["stdweb", "yew-macro/std_web"]
web_sys = ["console_error_panic_hook", "futures", "gloo", "js-sys", "web-sys", "wasm-bindgen", "wasm-bindgen-futures"]
doc_test = []
wasm_test = []
Expand Down
Loading