Skip to content

Improve before_mount docs and do runtime debug check #334

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

Merged
merged 1 commit into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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