Skip to content

Commit

Permalink
Escape HTML as we translate to rsx (#3038)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealmloff authored Oct 8, 2024
1 parent 9ffd4b8 commit d64df87
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/rsx-rosetta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ proc-macro2 = { workspace = true }
quote = { workspace = true }
syn = { workspace = true, features = ["full"] }
convert_case = { workspace = true }
htmlentity = "1.3.2"

[dev-dependencies]
pretty_assertions = "1.2.1"
Expand Down
7 changes: 6 additions & 1 deletion packages/rsx-rosetta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use dioxus_rsx::{
HotLiteral, TemplateBody, TextNode,
};
pub use html_parser::{Dom, Node};
use htmlentity::entity::ICodedDataTrait;
use proc_macro2::{Ident, Span};
use syn::{punctuated::Punctuated, LitStr};

Expand All @@ -33,7 +34,11 @@ pub fn rsx_node_from_html(node: &Node) -> Option<BodyNode> {
use AttributeValue::*;

match node {
Node::Text(text) => Some(BodyNode::Text(TextNode::from_text(text))),
Node::Text(text) => Some(BodyNode::Text(TextNode::from_text(
&htmlentity::entity::decode(text.as_bytes())
.to_string()
.ok()?,
))),

Node::Element(el) => {
let el_name = if let Some(name) = map_html_element_to_rsx(&el.name) {
Expand Down
18 changes: 18 additions & 0 deletions packages/rsx-rosetta/tests/escape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use html_parser::Dom;

// Regression test for https://github.com/DioxusLabs/dioxus/issues/3037
// We need to escape html entities as we translate html because rsx doesn't support them
#[test]
fn escaped_text() {
let html = r#"<div>&lt;div&gt;&#x231b;&#x231b;&#x231b;&#x231b;</div>"#.trim();

let dom = Dom::parse(html).unwrap();

let body = dioxus_rsx_rosetta::rsx_from_html(&dom);

let out = dioxus_autofmt::write_block_out(&body).unwrap();

let expected = r#"
div { "<div>⌛⌛⌛⌛" }"#;
pretty_assertions::assert_eq!(&out, &expected);
}

0 comments on commit d64df87

Please sign in to comment.