Search in sources :

Example 1 with SimpleURL

use of com.helger.commons.url.SimpleURL in project ph-css by phax.

the class CSSURLHelperTest method testGetAsCSSURL.

@Test
public void testGetAsCSSURL() {
    for (final String sURL : new String[] { "a.gif", "\"a.gif\"", "b\\a.gif", "\\b\\a\\c.gif", "'a.gif'", "'a\".gif'", "\"a'.gif\"", "a().gif", "a\\(\\).gif" }) {
        final String sEscaped = CSSURLHelper.getAsCSSURL(sURL, false);
        assertEquals(sURL, CSSParseHelper.trimUrl(sEscaped));
    }
    assertEquals("url(a.gif)", CSSURLHelper.getAsCSSURL("a.gif", false));
    // By default: single quotes
    assertEquals("url('a.gif')", CSSURLHelper.getAsCSSURL("a.gif", true));
    // Force single quotes
    assertEquals("url('\"a.gif\"')", CSSURLHelper.getAsCSSURL("\"a.gif\"", true));
    // auto-quote
    assertEquals("url('\"a.gif\"')", CSSURLHelper.getAsCSSURL("\"a.gif\"", false));
    // auto-quote
    assertEquals("url('b\\\\a.gif')", CSSURLHelper.getAsCSSURL("b\\a.gif", false));
    // Force double quotes
    assertEquals("url(\"'a.gif'\")", CSSURLHelper.getAsCSSURL("'a.gif'", true));
    // auto-quote
    assertEquals("url(\"'a.gif'\")", CSSURLHelper.getAsCSSURL("'a.gif'", false));
    // Containing char to be escaped
    assertEquals("url('\\'a\".gif\\'')", CSSURLHelper.getAsCSSURL("'a\".gif'", true));
    // auto-quote
    assertEquals("url('\\'a\".gif\\'')", CSSURLHelper.getAsCSSURL("'a\".gif'", false));
    // Containing char to be escaped
    assertEquals("url('\"a\\'.gif\"')", CSSURLHelper.getAsCSSURL("\"a'.gif\"", true));
    // auto-quote
    assertEquals("url('\"a\\'.gif\"')", CSSURLHelper.getAsCSSURL("\"a'.gif\"", false));
    // Escaped brackets
    assertEquals("url('a().gif')", CSSURLHelper.getAsCSSURL("a().gif", false));
    final SimpleURL aURL = new SimpleURL("a.gif", new StringMap("x", "y"));
    assertEquals("url(a.gif?x=y)", CSSURLHelper.getAsCSSURL(aURL, false));
    assertEquals("url('a.gif?x=y')", CSSURLHelper.getAsCSSURL(aURL, true));
    assertEquals("url()", CSSURLHelper.getAsCSSURL(new SimpleURL(), false));
    assertEquals("url('')", CSSURLHelper.getAsCSSURL(new SimpleURL(), true));
    // SimpleURL -> CSS URL -> String -> SimpleURL
    assertEquals(aURL, new SimpleURL(CSSURLHelper.getURLValue(CSSURLHelper.getAsCSSURL(aURL, true))));
    // empty URL!
    assertEquals("url()", CSSURLHelper.getAsCSSURL("", false));
    assertEquals("url('')", CSSURLHelper.getAsCSSURL("", true));
}
Also used : StringMap(com.helger.commons.collection.attr.StringMap) SimpleURL(com.helger.commons.url.SimpleURL) Test(org.junit.Test)

Aggregations

StringMap (com.helger.commons.collection.attr.StringMap)1 SimpleURL (com.helger.commons.url.SimpleURL)1 Test (org.junit.Test)1