escape.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. use dioxus::prelude::*;
  2. #[test]
  3. fn escape_static_values() {
  4. fn app() -> Element {
  5. rsx! { input { disabled: "\"><div>" } }
  6. }
  7. let mut dom = VirtualDom::new(app);
  8. dom.rebuild(&mut dioxus_core::NoOpMutations);
  9. assert_eq!(
  10. dioxus_ssr::pre_render(&dom),
  11. "<input disabled=\"&#34;&#62;&#60;div&#62;\" data-node-hydration=\"0\"/>"
  12. );
  13. }
  14. #[test]
  15. fn escape_dynamic_values() {
  16. fn app() -> Element {
  17. let disabled = "\"><div>";
  18. rsx! { input { disabled } }
  19. }
  20. let mut dom = VirtualDom::new(app);
  21. dom.rebuild(&mut dioxus_core::NoOpMutations);
  22. assert_eq!(
  23. dioxus_ssr::pre_render(&dom),
  24. "<input disabled=\"&#34;&#62;&#60;div&#62;\" data-node-hydration=\"0\"/>"
  25. );
  26. }
  27. #[test]
  28. fn escape_static_style() {
  29. fn app() -> Element {
  30. rsx! { div { width: "\"><div>" } }
  31. }
  32. let mut dom = VirtualDom::new(app);
  33. dom.rebuild(&mut dioxus_core::NoOpMutations);
  34. assert_eq!(
  35. dioxus_ssr::pre_render(&dom),
  36. "<div style=\"width:&#34;&#62;&#60;div&#62;;\" data-node-hydration=\"0\"></div>"
  37. );
  38. }
  39. #[test]
  40. fn escape_dynamic_style() {
  41. fn app() -> Element {
  42. let width = "\"><div>";
  43. rsx! { div { width } }
  44. }
  45. let mut dom = VirtualDom::new(app);
  46. dom.rebuild(&mut dioxus_core::NoOpMutations);
  47. assert_eq!(
  48. dioxus_ssr::pre_render(&dom),
  49. "<div style=\"width:&#34;&#62;&#60;div&#62;;\" data-node-hydration=\"0\"></div>"
  50. );
  51. }
  52. #[test]
  53. fn escape_static_text() {
  54. fn app() -> Element {
  55. rsx! {
  56. div {
  57. "\"><div>"
  58. }
  59. }
  60. }
  61. let mut dom = VirtualDom::new(app);
  62. dom.rebuild(&mut dioxus_core::NoOpMutations);
  63. assert_eq!(
  64. dioxus_ssr::pre_render(&dom),
  65. "<div data-node-hydration=\"0\">&#34;&#62;&#60;div&#62;</div>"
  66. );
  67. }
  68. #[test]
  69. fn escape_dynamic_text() {
  70. fn app() -> Element {
  71. let text = "\"><div>";
  72. rsx! {
  73. div {
  74. {text}
  75. }
  76. }
  77. }
  78. let mut dom = VirtualDom::new(app);
  79. dom.rebuild(&mut dioxus_core::NoOpMutations);
  80. assert_eq!(
  81. dioxus_ssr::pre_render(&dom),
  82. "<div data-node-hydration=\"0\"><!--node-id1-->&#34;&#62;&#60;div&#62;<!--#--></div>"
  83. );
  84. }
  85. #[test]
  86. fn don_t_escape_static_scripts() {
  87. fn app() -> Element {
  88. rsx! {
  89. script {
  90. "console.log('hello world');"
  91. }
  92. }
  93. }
  94. let mut dom = VirtualDom::new(app);
  95. dom.rebuild(&mut dioxus_core::NoOpMutations);
  96. assert_eq!(
  97. dioxus_ssr::pre_render(&dom),
  98. "<script data-node-hydration=\"0\">console.log('hello world');</script>"
  99. );
  100. }
  101. #[test]
  102. fn don_t_escape_dynamic_scripts() {
  103. fn app() -> Element {
  104. let script = "console.log('hello world');";
  105. rsx! {
  106. script {
  107. {script}
  108. }
  109. }
  110. }
  111. let mut dom = VirtualDom::new(app);
  112. dom.rebuild(&mut dioxus_core::NoOpMutations);
  113. assert_eq!(
  114. dioxus_ssr::pre_render(&dom),
  115. "<script data-node-hydration=\"0\"><!--node-id1-->console.log('hello world');<!--#--></script>"
  116. );
  117. }
  118. #[test]
  119. fn don_t_escape_static_styles() {
  120. fn app() -> Element {
  121. rsx! {
  122. style {
  123. "body {{ background-color: red; }}"
  124. }
  125. }
  126. }
  127. let mut dom = VirtualDom::new(app);
  128. dom.rebuild(&mut dioxus_core::NoOpMutations);
  129. assert_eq!(
  130. dioxus_ssr::pre_render(&dom),
  131. "<style data-node-hydration=\"0\">body { background-color: red; }</style>"
  132. );
  133. }
  134. #[test]
  135. fn don_t_escape_dynamic_styles() {
  136. fn app() -> Element {
  137. let style = "body { font-family: \"sans-serif\"; }";
  138. rsx! {
  139. style {
  140. {style}
  141. }
  142. }
  143. }
  144. let mut dom = VirtualDom::new(app);
  145. dom.rebuild(&mut dioxus_core::NoOpMutations);
  146. assert_eq!(
  147. dioxus_ssr::pre_render(&dom),
  148. "<style data-node-hydration=\"0\"><!--node-id1-->body { font-family: \"sans-serif\"; }<!--#--></style>"
  149. );
  150. }
  151. #[test]
  152. fn don_t_escape_static_fragment_styles() {
  153. fn app() -> Element {
  154. let style_element = rsx! { "body {{ font-family: \"sans-serif\"; }}" };
  155. rsx! {
  156. style {
  157. {style_element}
  158. }
  159. }
  160. }
  161. let mut dom = VirtualDom::new(app);
  162. dom.rebuild(&mut dioxus_core::NoOpMutations);
  163. assert_eq!(
  164. dioxus_ssr::pre_render(&dom),
  165. "<style data-node-hydration=\"0\"><!--node-id1-->body { font-family: \"sans-serif\"; }<!--#--></style>"
  166. );
  167. }
  168. #[test]
  169. fn escape_static_component_fragment_div() {
  170. #[component]
  171. fn StyleContents() -> Element {
  172. rsx! { "body {{ font-family: \"sans-serif\"; }}" }
  173. }
  174. fn app() -> Element {
  175. rsx! {
  176. div {
  177. StyleContents {}
  178. }
  179. }
  180. }
  181. let mut dom = VirtualDom::new(app);
  182. dom.rebuild(&mut dioxus_core::NoOpMutations);
  183. assert_eq!(
  184. dioxus_ssr::pre_render(&dom),
  185. "<div data-node-hydration=\"0\"><!--node-id1-->body { font-family: &#34;sans-serif&#34;; }<!--#--></div>"
  186. );
  187. }
  188. #[test]
  189. fn escape_dynamic_component_fragment_div() {
  190. #[component]
  191. fn StyleContents() -> Element {
  192. let dynamic = "body { font-family: \"sans-serif\"; }";
  193. rsx! { "{dynamic}" }
  194. }
  195. fn app() -> Element {
  196. rsx! {
  197. div {
  198. StyleContents {}
  199. }
  200. }
  201. }
  202. let mut dom = VirtualDom::new(app);
  203. dom.rebuild(&mut dioxus_core::NoOpMutations);
  204. assert_eq!(
  205. dioxus_ssr::pre_render(&dom),
  206. "<div data-node-hydration=\"0\"><!--node-id1-->body { font-family: &#34;sans-serif&#34;; }<!--#--></div>"
  207. );
  208. }