Unescape CSS string escaped with Sightly context | Community
Skip to main content
Level 4
January 17, 2018
Solved

Unescape CSS string escaped with Sightly context

  • January 17, 2018
  • 1 reply
  • 2583 views

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!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by boser87

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));

1 reply

boser87AuthorAccepted solution
Level 4
January 17, 2018

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));