From 1d43a25457d7c7846956d28f596434d8a1670a96 Mon Sep 17 00:00:00 2001 From: "Scott.Embler" Date: Wed, 2 Jun 2021 13:32:37 -0400 Subject: [PATCH] Treating text as unescaped when using TagCreator methods for style and script elements. - Replicating PR #152, but avoiding conflicts and using more current conventions. --- library/src/main/java/j2html/TagCreator.java | 8 +++---- .../test/java/j2html/tags/TagCreatorTest.java | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/library/src/main/java/j2html/TagCreator.java b/library/src/main/java/j2html/TagCreator.java index 5856ccd..10289cb 100644 --- a/library/src/main/java/j2html/TagCreator.java +++ b/library/src/main/java/j2html/TagCreator.java @@ -820,10 +820,10 @@ public class TagCreator { public static SampTag samp (Attr.ShortForm shortAttr, DomContent... dc) { return Attr.addTo( new SampTag().with(dc), shortAttr); } public static ScriptTag script () { return new ScriptTag(); } - public static ScriptTag script (String text) { return new ScriptTag().withText(text); } + public static ScriptTag script (String text) { return new ScriptTag().with(new UnescapedText(text)); } public static ScriptTag script (DomContent... dc) { return new ScriptTag().with(dc); } public static ScriptTag script (Attr.ShortForm shortAttr) { return Attr.addTo( new ScriptTag(), shortAttr); } - public static ScriptTag script (Attr.ShortForm shortAttr, String text) { return Attr.addTo( new ScriptTag().withText(text), shortAttr); } + public static ScriptTag script (Attr.ShortForm shortAttr, String text) { return Attr.addTo( new ScriptTag().with(new UnescapedText(text)), shortAttr); } public static ScriptTag script (Attr.ShortForm shortAttr, DomContent... dc) { return Attr.addTo( new ScriptTag().with(dc), shortAttr); } public static SectionTag section () { return new SectionTag(); } @@ -862,10 +862,10 @@ public class TagCreator { public static StrongTag strong (Attr.ShortForm shortAttr, DomContent... dc) { return Attr.addTo( new StrongTag().with(dc), shortAttr); } public static StyleTag style () { return new StyleTag(); } - public static StyleTag style (String text) { return new StyleTag().withText(text); } + public static StyleTag style (String text) { return new StyleTag().with(new UnescapedText(text)); } public static StyleTag style (DomContent... dc) { return new StyleTag().with(dc); } public static StyleTag style (Attr.ShortForm shortAttr) { return Attr.addTo( new StyleTag(), shortAttr); } - public static StyleTag style (Attr.ShortForm shortAttr, String text) { return Attr.addTo( new StyleTag().withText(text), shortAttr); } + public static StyleTag style (Attr.ShortForm shortAttr, String text) { return Attr.addTo( new StyleTag().with(new UnescapedText(text)), shortAttr); } public static StyleTag style (Attr.ShortForm shortAttr, DomContent... dc) { return Attr.addTo( new StyleTag().with(dc), shortAttr); } public static SubTag sub () { return new SubTag(); } diff --git a/library/src/test/java/j2html/tags/TagCreatorTest.java b/library/src/test/java/j2html/tags/TagCreatorTest.java index c3f2a16..aab6096 100644 --- a/library/src/test/java/j2html/tags/TagCreatorTest.java +++ b/library/src/test/java/j2html/tags/TagCreatorTest.java @@ -319,4 +319,28 @@ public class TagCreatorTest { assertThat(video().render(), is("")); } + @Test + public void script_factories_do_not_escape_text_parameters() { + assertEquals( + "", + script("var test = 'Hello, world!';").render() + ); + assertEquals( + "", + script(attrs("#x"), "var test = 'Hello, world!';").render() + ); + } + + @Test + public void style_factories_do_not_escape_text_parameters() { + assertEquals( + "", + style(".test>a {}").render() + ); + assertEquals( + "", + style(attrs("#x"),".test>a {}").render() + ); + } + }