add some tracing and debug info to HTML elements

This commit is contained in:
Greg Johnston 2024-06-05 20:15:05 -04:00
parent caf797dba0
commit 0fc47e3a35
7 changed files with 75 additions and 11 deletions

View file

@ -6,6 +6,7 @@ use crate::{
use std::{borrow::Cow, fmt::Debug, marker::PhantomData, sync::Arc};
// FIXME custom element HTML rendering is broken because tag names aren't static
#[track_caller]
pub fn custom<E, Rndr>(tag: E) -> HtmlElement<Custom<E>, (), (), Rndr>
where
E: CustomElementKey,
@ -16,6 +17,8 @@ where
rndr: PhantomData,
attributes: (),
children: (),
#[cfg(debug_assertions)]
defined_at: std::panic::Location::caller(),
}
}

View file

@ -24,6 +24,7 @@ macro_rules! html_elements {
paste::paste! {
$(
#[$meta]
#[track_caller]
pub fn $tag<Rndr>() -> HtmlElement<[<$tag:camel>], (), (), Rndr>
where
Rndr: Renderer
@ -33,6 +34,8 @@ macro_rules! html_elements {
attributes: (),
children: (),
rndr: PhantomData,
#[cfg(debug_assertions)]
defined_at: std::panic::Location::caller()
}
}
@ -59,12 +62,17 @@ macro_rules! html_elements {
At: NextTuple,
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>: Attribute<Rndr>,
{
let HtmlElement { tag, rndr, children, attributes } = self;
let HtmlElement { tag, rndr, children, attributes,
#[cfg(debug_assertions)]
defined_at
} = self;
HtmlElement {
tag,
rndr,
children,
attributes: attributes.next_tuple($crate::html::attribute::$attr(value))
attributes: attributes.next_tuple($crate::html::attribute::$attr(value)),
#[cfg(debug_assertions)]
defined_at
}
}
)*
@ -85,6 +93,8 @@ macro_rules! html_elements {
impl ElementWithChildren for [<$tag:camel>] {}
impl CreateElement<Dom> for [<$tag:camel>] {
#[track_caller]
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace", fields(callsite = std::panic::Location::caller().to_string())))]
fn create_element(&self) -> <Dom as Renderer>::Element {
use wasm_bindgen::JsCast;
@ -126,7 +136,9 @@ macro_rules! html_self_closing_elements {
attributes: (),
children: (),
rndr: PhantomData,
tag: [<$tag:camel>]
tag: [<$tag:camel>],
#[cfg(debug_assertions)]
defined_at: std::panic::Location::caller()
}
}
@ -154,12 +166,17 @@ macro_rules! html_self_closing_elements {
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>: Attribute<Rndr>,
{
let HtmlElement { tag, rndr, children, attributes } = self;
let HtmlElement { tag, rndr, children, attributes,
#[cfg(debug_assertions)]
defined_at
} = self;
HtmlElement {
tag,
rndr,
children,
attributes: attributes.next_tuple($crate::html::attribute::$attr(value))
attributes: attributes.next_tuple($crate::html::attribute::$attr(value)),
#[cfg(debug_assertions)]
defined_at
}
}
)*
@ -433,6 +450,8 @@ where
rndr: PhantomData,
attributes: (),
children: (),
#[cfg(debug_assertions)]
defined_at: std::panic::Location::caller(),
}
}

View file

@ -28,6 +28,8 @@ pub struct HtmlElement<E, At, Ch, Rndr> {
pub(crate) rndr: PhantomData<Rndr>,
pub(crate) attributes: At,
pub(crate) children: Ch,
#[cfg(debug_assertions)]
pub(crate) defined_at: &'static std::panic::Location<'static>,
}
/*impl<E, At, Ch, Rndr> ElementType for HtmlElement<E, At, Ch, Rndr>
@ -80,12 +82,16 @@ where
rndr,
attributes,
children,
#[cfg(debug_assertions)]
defined_at,
} = self;
HtmlElement {
tag,
rndr,
attributes,
children: children.next_tuple(child),
#[cfg(debug_assertions)]
defined_at,
}
}
}
@ -113,12 +119,16 @@ where
attributes,
children,
rndr,
#[cfg(debug_assertions)]
defined_at,
} = self;
HtmlElement {
tag,
attributes: attributes.add_any_attr(attr),
children,
rndr,
#[cfg(debug_assertions)]
defined_at,
}
}
}
@ -179,6 +189,7 @@ where
fn build(self) -> Self::State {
let el = Rndr::create_element(self.tag);
let attrs = self.attributes.build(&el);
let mut children = self.children.build();
children.mount(&el, None);
@ -223,6 +234,8 @@ where
rndr: PhantomData,
attributes: self.attributes,
children: self.children.resolve().await,
#[cfg(debug_assertions)]
defined_at: self.defined_at,
}
}

View file

@ -25,12 +25,17 @@ macro_rules! mathml_global {
At: NextTuple,
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>: Attribute<Rndr>,
{
let HtmlElement { tag, rndr, children, attributes } = self;
let HtmlElement { tag, rndr, children, attributes,
#[cfg(debug_assertions)]
defined_at
} = self;
HtmlElement {
tag,
rndr,
children,
attributes: attributes.next_tuple($crate::html::attribute::$attr(value))
attributes: attributes.next_tuple($crate::html::attribute::$attr(value)),
#[cfg(debug_assertions)]
defined_at
}
}
}
@ -42,6 +47,7 @@ macro_rules! mathml_elements {
paste::paste! {
$(
// `tag()` function
#[track_caller]
pub fn $tag<Rndr>() -> HtmlElement<[<$tag:camel>], (), (), Rndr>
where
Rndr: Renderer
@ -51,6 +57,8 @@ macro_rules! mathml_elements {
attributes: (),
children: (),
rndr: PhantomData,
#[cfg(debug_assertions)]
defined_at: std::panic::Location::caller()
}
}
@ -83,12 +91,17 @@ macro_rules! mathml_elements {
At: NextTuple,
<At as NextTuple>::Output<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>: Attribute<Rndr>,
{
let HtmlElement { tag, rndr, children, attributes } = self;
let HtmlElement { tag, rndr, children, attributes,
#[cfg(debug_assertions)]
defined_at
} = self;
HtmlElement {
tag,
rndr,
children,
attributes: attributes.next_tuple($crate::html::attribute::$attr(value))
attributes: attributes.next_tuple($crate::html::attribute::$attr(value)),
#[cfg(debug_assertions)]
defined_at
}
}
)*

View file

@ -31,6 +31,7 @@ impl Renderer for Dom {
intern(text)
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn create_text_node(text: &str) -> Self::Text {
document().create_text_node(text)
}
@ -39,18 +40,22 @@ impl Renderer for Dom {
document().create_comment("")
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn set_text(node: &Self::Text, text: &str) {
node.set_node_value(Some(text));
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn set_attribute(node: &Self::Element, name: &str, value: &str) {
or_debug!(node.set_attribute(name, value), node, "setAttribute");
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn remove_attribute(node: &Self::Element, name: &str) {
or_debug!(node.remove_attribute(name), node, "removeAttribute");
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn insert_node(
parent: &Self::Element,
new_child: &Self::Node,
@ -63,6 +68,7 @@ impl Renderer for Dom {
);
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn remove_node(
parent: &Self::Element,
child: &Self::Node,
@ -70,6 +76,7 @@ impl Renderer for Dom {
ok_or_debug!(parent.remove_child(child), parent, "removeNode")
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn remove(node: &Self::Node) {
node.unchecked_ref::<Element>().remove();
}
@ -90,6 +97,7 @@ impl Renderer for Dom {
web_sys::console::log_1(node);
}
#[cfg_attr(feature = "tracing", tracing::instrument(level = "trace"))]
fn clear_children(parent: &Self::Element) {
parent.set_text_content(Some(""));
}

View file

@ -41,6 +41,7 @@ pub trait Renderer: Send + Sized + Debug + 'static {
fn intern(text: &str) -> &str;
/// Creates a new element node.
#[track_caller]
fn create_element<E: CreateElement<Self>>(tag: E) -> Self::Element {
tag.create_element()
}

View file

@ -26,6 +26,8 @@ macro_rules! svg_elements {
attributes: (),
children: (),
rndr: PhantomData,
#[cfg(debug_assertions)]
defined_at: std::panic::Location::caller()
}
}
@ -49,12 +51,17 @@ macro_rules! svg_elements {
At: NextTuple<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>,
<At as NextTuple<Attr<$crate::html::attribute::[<$attr:camel>], V, Rndr>>>::Output: Attribute<Rndr>,
{
let HtmlElement { tag, rndr, children, attributes } = self;
let HtmlElement { tag, rndr, children, attributes,
#[cfg(debug_assertions)]
defined_at
} = self;
HtmlElement {
tag,
rndr,
children,
attributes: attributes.next_tuple($crate::html::attribute::$attr(value))
attributes: attributes.next_tuple($crate::html::attribute::$attr(value)),
#[cfg(debug_assertions)]
defined_at
}
}
)*