Adding in shadow_root! macro

This commit is contained in:
Pauan 2020-05-04 23:28:09 +02:00
parent e3fc0f152a
commit 07ae71cf35
4 changed files with 43 additions and 2 deletions

View File

@ -53,6 +53,9 @@ features = [
"Location",
"MouseEvent",
"Node",
"ShadowRoot",
"ShadowRootInit",
"ShadowRootMode",
"StyleSheet",
"SvgElement",
"Text",

View File

@ -11,7 +11,7 @@ use futures_util::FutureExt;
use futures_channel::oneshot;
use discard::{Discard, DiscardOnDrop};
use wasm_bindgen::{JsValue, UnwrapThrowExt, JsCast, intern};
use web_sys::{HtmlElement, Node, EventTarget, Element, CssStyleSheet, CssStyleDeclaration};
use web_sys::{HtmlElement, Node, EventTarget, Element, CssStyleSheet, CssStyleDeclaration, ShadowRoot, ShadowRootMode, ShadowRootInit};
use crate::bindings;
use crate::callbacks::Callbacks;
@ -393,6 +393,14 @@ impl<A> DomBuilder<A> where A: JsCast {
}
impl<A> DomBuilder<A> {
#[inline]
#[doc(hidden)]
pub fn __internal_transfer_callbacks<B>(mut self, mut shadow: DomBuilder<B>) -> Self {
self.callbacks.after_insert.append(&mut shadow.callbacks.after_insert);
self.callbacks.after_remove.append(&mut shadow.callbacks.after_remove);
self
}
#[inline]
pub fn new(value: A) -> Self {
Self {
@ -567,7 +575,7 @@ impl<A> DomBuilder<A> where A: AsRef<Node> {
// TODO figure out how to make this owned rather than &mut
#[inline]
pub fn children<'a, B: BorrowMut<Dom>, C: IntoIterator<Item = B>>(mut self, children: C) -> Self {
pub fn children<B: BorrowMut<Dom>, C: IntoIterator<Item = B>>(mut self, children: C) -> Self {
self.check_children();
operations::insert_children_iter(self.element.as_ref(), &mut self.callbacks, children);
self
@ -625,6 +633,13 @@ impl<A> DomBuilder<A> where A: AsRef<Node> {
}
impl<A> DomBuilder<A> where A: AsRef<Element> {
#[inline]
#[doc(hidden)]
pub fn __internal_shadow_root(&self, mode: ShadowRootMode) -> DomBuilder<ShadowRoot> {
let shadow = self.element.as_ref().attach_shadow(&ShadowRootInit::new(mode)).unwrap_throw();
DomBuilder::new(shadow)
}
#[inline]
pub fn attribute<B>(self, name: B, value: &str) -> Self where B: MultiStr {
let element = self.element.as_ref();
@ -1155,6 +1170,7 @@ pub mod __internal {
#[cfg(test)]
mod tests {
use super::{DomBuilder, text_signal, RefFn};
use crate::{html, shadow_root, ShadowRootMode};
use futures_signals::signal::{always, SignalExt};
use lazy_static::lazy_static;
use web_sys::HtmlElement;
@ -1289,4 +1305,15 @@ mod tests {
.style_signal(["-moz-foo", "-webkit-foo", "foo"], always("bar".to_owned()).map(|x| Some(RefFn::new(x, |x| x.as_str()))))
;
}
#[test]
fn shadow_root() {
let _a = html!("div", {
.shadow_root!(ShadowRootMode::Closed => {
.children(&mut [
html!("span")
])
})
});
}
}

View File

@ -10,6 +10,7 @@ mod callbacks;
mod operations;
mod dom;
pub use web_sys::ShadowRootMode;
pub use dom::*;
pub mod traits;
pub mod animation;

View File

@ -44,6 +44,16 @@ macro_rules! with_node {
}
#[macro_export]
macro_rules! shadow_root {
($this:ident, $mode:expr => { $($methods:tt)* }) => {{
let shadow = $crate::DomBuilder::__internal_shadow_root(&$this, $mode);
let shadow = $crate::apply_methods!(shadow, { $($methods)* });
$crate::DomBuilder::__internal_transfer_callbacks($this, shadow)
}};
}
#[macro_export]
macro_rules! html {
($($args:tt)+) => {