tipsy / j2html

Java to HTML generator. Enjoy typesafe HTML generation.

Home Page:https://j2html.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Move escaping from Attribute constructor to rendering

lambdaupb opened this issue · comments

Attribute escapes its value in the constructor, that leads to the behaviour that Attribute.getValue() returns escaped text.

If that text is then used to create another value or text element, it will be double escaped < -> <

    @Test
    public void testAttributeValueRoundtrip() throws Exception {
        Attribute attributeWithValue = new Attribute("href", "<");
        Attribute attributeWithValueReconstructed = new Attribute("href", attributeWithValue.getValue());

        assertThat(attributeWithValueReconstructed.getValue(), is("<"));
    }
java.lang.AssertionError: 
Expected: is "<"
     but: was "&amp;lt;"
Expected :<
Actual   :&amp;lt;

Additionally the behaviour is inconsistent between the constructor and the setValue method. setValue() does not escape the text, while the constructor does.

Changing this will technically alter behaviour of existing code, but one could argue that uses like this were broken.

commented

Changing this will technically alter behaviour of existing code, but one could argue that uses like this were broken.

Agreed. Please go ahead.