Expand my Community achievements bar.

SOLVED

Unescape CSS string escaped with Sightly context

Avatar

Level 4

Hi,

I need to unescape (and then escape again) a string result of using Sightly context "styleString":

a string example is background-image: url(\2f content\2f dam\2fwebsites\2fglobal\2fimages-2\2f 2018\2f 05\2fKoala.jpg);

I need to unescape the String in Java and then escape it again. StringEscapeUtils does not seem to work in this case. Do you know how can I escape/unescape strings result of Sightly's styleString encoding?

Thanks!

1 Accepted Solution

Avatar

Correct answer by
Level 4

I found the way to solve the problem. I am using XSSAPI https://sling.apache.org/apidocs/sling9/org/apache/sling/xss/XSSAPI.html encodeForCSSString method which is used by AEM internally for encoding sensitive parts of the HTML page through 'context' option in HTL. I am encoding both ends of my comparison so I don’t actually need to decode.

And if you are wondering how to unit test a Sling Model that has that kind of dependency, this is how you add the mocked class to your test context:

context.registerService(XSSAPI.class, Mockito.mock(XSSAPI.class));

View solution in original post

1 Reply

Avatar

Correct answer by
Level 4

I found the way to solve the problem. I am using XSSAPI https://sling.apache.org/apidocs/sling9/org/apache/sling/xss/XSSAPI.html encodeForCSSString method which is used by AEM internally for encoding sensitive parts of the HTML page through 'context' option in HTL. I am encoding both ends of my comparison so I don’t actually need to decode.

And if you are wondering how to unit test a Sling Model that has that kind of dependency, this is how you add the mocked class to your test context:

context.registerService(XSSAPI.class, Mockito.mock(XSSAPI.class));