Skip to content

Commit

Permalink
feat: Improve before_mount docs and do runtime debug check
Browse files Browse the repository at this point in the history
See commit code for explanation.
  • Loading branch information
TatriX authored and MartinKavik committed Jan 15, 2020
1 parent 4d3b3eb commit 5d94d96
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ impl<Ms, Mdl, ElC: View<Ms> + 'static, GMs: 'static> App<Ms, Mdl, ElC, GMs> {
// not entirely correct).
// TODO: 1) Please refer to [issue #277](https://github.com/seed-rs/seed/issues/277)
let mut dom_nodes: El<Ms> = (&self.cfg.mount_point).into();
if cfg!(debug_assertions) {
dom_nodes.warn_about_script_tags();
}
dom_nodes.strip_ws_nodes_from_self_and_children();

// Replace the root dom with a placeholder tag and move the children from the root element
Expand Down
10 changes: 8 additions & 2 deletions src/app/builder/before_mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,14 @@ impl BeforeMount {
/// mount_point("another_id")
///
/// // argument is `HTMLElement`
/// // NOTE: Be careful with mounting into body,
/// // it can cause hard-to-debug bugs when there are other scripts in the body.
///
/// // NOTE: Be careful with mounting into body!
/// // If you render directly into document.body, you risk collisions
/// // with scripts that do something with it (e.g. Google Font Loader or
/// // third party browser extensions) which produce very weird and hard
/// // to debug errors in production.
/// // (from https://github.com/facebook/create-react-app/issues/1568)
///
/// mount_point(seed::body())
///
/// // argument is `Element`
Expand Down
7 changes: 7 additions & 0 deletions src/virtual_dom/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ impl<Ms> Node<Ms> {
Node::Empty => (),
}
}

#[cfg(debug_assertions)]
pub fn warn_about_script_tags(&self) {
if let Node::Element(e) = self {
e.warn_about_script_tags();
}
}
}

impl<Ms: 'static, OtherMs: 'static> MessageMapper<Ms, OtherMs> for Node<Ms> {
Expand Down
18 changes: 18 additions & 0 deletions src/virtual_dom/node/el.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,24 @@ impl<Ms> El<Ms> {
.collect()
}

#[cfg(debug_assertions)]
/// Warn user about potential bugs when having scripts and `Takeover` mount type.
pub fn warn_about_script_tags(&self) {
let script_found = match &self.tag {
Tag::Script => true,
Tag::Custom(tag) if tag == "script" => true,
_ => false,
};
if script_found {
error!("Script tag found inside mount point! \
Please check https://docs.rs/seed/latest/seed/app/builder/struct.Builder.html#examples");
}

for child in &self.children {
child.warn_about_script_tags();
}
}

/// Remove websys nodes.
pub fn strip_ws_nodes_from_self_and_children(&mut self) {
self.node_ws.take();
Expand Down

0 comments on commit 5d94d96

Please # to comment.