package jamaica.core.http;

import org.testng.annotations.*;
import static org.testng.AssertJUnit.*;

public class html {

    /**
     * Convert page-relative URLs (e.g. "foo/bar") in the given HTML content
     * to absolute URLs by preprending the given base URL. URLs relative to
     * the domain root are not modified (e.g. "/foo/bar").
     * 
     * Only src and href attributes are modified. A full list of possible
     * attributes with URL values can be found at
     * http://stackoverflow.com/questions/2725156
     * 
     * This function does not validate the baseURL.
     */
    @Test public void make_urls_absolute__processes_relative_urls() {
        assertEquals(make_urls_absolute(
                "<a href=\"foo/bar.jpg\"/>", "http://example.com"), 
                "<a href=\"http://example.com/foo/bar.jpg\"/>");
    }
    @Test public void make_urls_absolute__processes_src_attributes() {
        assertEquals(make_urls_absolute(
                "<img src=\"foo/bar.jpg\"/>", "http://example.com"), 
                "<img src=\"http://example.com/foo/bar.jpg\"/>");
    }
    @Test public void make_urls_absolute__processes_href_attributes() {
        assertEquals(make_urls_absolute(
                "<a href=\"foo/bar.jpg\"/>", "http://example.com"), 
                "<a href=\"http://example.com/foo/bar.jpg\"/>");
    }
    @Test public void make_urls_absolute__does_not_process_plain_text() {
        assertEquals(make_urls_absolute(
                "foo/bar.jpg", "http://example.com"), 
                "foo/bar.jpg");
    }
    @Test public void make_urls_absolute__processes_single_quoted_attributes() {
        assertEquals(make_urls_absolute(
                "<a href='foo/bar.jpg'/>", "http://example.com"), 
                "<a href='http://example.com/foo/bar.jpg'/>");
    }
    @Test public void make_urls_absolute__does_not_process_absolute_http_urls() {
        assertEquals(make_urls_absolute(
                "<a href=\"http://helloworld.com/foo/bar.jpg\"/>", "http://example.com"), 
                "<a href=\"http://helloworld.com/foo/bar.jpg\"/>");
    }
    @Test public void make_urls_absolute__does_not_process_absolute_https_urls() {
        assertEquals(make_urls_absolute(
                "<a href=\"https://helloworld.com/foo/bar.jpg\"/>", "http://example.com"), 
                "<a href=\"https://helloworld.com/foo/bar.jpg\"/>");
    }
    @Test public void make_urls_absolute__does_not_process_mailto_urls() {
        assertEquals(make_urls_absolute(
                "<a href=\"mailto:hello@world.com\"/>", "http://example.com"), 
                "<a href=\"mailto:hello@world.com\"/>");
    }
    @Test public void make_urls_absolute__does_not_process_ftp_urls() {
        assertEquals(make_urls_absolute(
                "<a href=\"ftp://helloworld.com/foo/bar.jpg\"/>", "http://example.com"), 
                "<a href=\"ftp://helloworld.com/foo/bar.jpg\"/>");
    }
    @Test public void make_urls_absolute__does_not_process_urls_starting_with_a_slash() {
        assertEquals(make_urls_absolute(
                "<a href=\"/foo/bar.jpg\"/>", "http://example.com"), 
                "<a href=\"/foo/bar.jpg\"/>");
    }
    @Test public void make_urls_absolute__does_not_process_urls_starting_with_a_double_slash() {
        assertEquals(make_urls_absolute(
                "<a href=\"//helloworld.com/foo/bar.jpg\"/>", "http://example.com"), 
                "<a href=\"//helloworld.com/foo/bar.jpg\"/>");
    }
    @Test public void make_urls_absolute__handles_base_urls_with_a_trailing_slash() {
        assertEquals(make_urls_absolute(
                "<a href=\"foo/bar.jpg\"/>", "http://example.com/"), 
                "<a href=\"http://example.com/foo/bar.jpg\"/>");
    }
    public static String make_urls_absolute(String html, String baseURL) {
        if (baseURL.endsWith("/")) {
            baseURL = baseURL.substring(0, baseURL.length() - 1);
        }
        return html.replaceAll("<(.*?)(src|href)=(\"|')([^/][^:]*)(\"|')(.*?)>",
                "<$1$2=$3" + baseURL + "/$4$5$6>");
    }
}

@@ -125,472 +125,28 @@
    /
-**%0A     * Convert page-relative URLs (e.g. %22foo/bar%22) in the given HTML content%0A     * to absolute URLs by preprending the given base URL. URLs relative to%0A     * the domain root are not modified (e.g. %22/foo/bar%22).%0A     * %0A     * Only src and href attributes are modified. A full list of possible%0A     * attributes with URL values can be found at%0A     * http://stackoverflow.com/questions/2725156%0A     * %0A     * This function does not validate the baseURL.%0A     */
+/ make_urls_absolute
 %0A   

remove dead comments