package us.codecraft.webmagic;

import org.junit.Test;
import us.codecraft.webmagic.selector.Html;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * @author code4crafter@gmail.com <br>
 * Date: 13-4-21
 * Time: 上午8:42
 */
public class HtmlTest {

    @Test
    public void testRegexSelector() {
        Html selectable = new Html("aaaaaaab");
		assertThat(selectable.regex("(a+b)").replace("aa(a)", "$1bb").toString()).isEqualTo("abbabbab");
    }

	@Test
	public void testDisableJsoupHtmlEntityEscape() throws Exception {
		Html.DISABLE_HTML_ENTITY_ESCAPE = true;
		Html html = new Html("aaaaaaa&b");
		assertThat(html.regex("(aaaaaaa&b)").toString()).isEqualTo("aaaaaaa&b");
	}

	@Test
	public void testEnableJsoupHtmlEntityEscape() throws Exception {
		Html.DISABLE_HTML_ENTITY_ESCAPE = false;
		Html html = new Html("aaaaaaa&b");
		assertThat(html.regex("(aaaaaaa&amp;b)").toString()).isEqualTo("aaaaaaa&amp;b");
	}
}

@@ -91,16 +91,66 @@
 or.Html;
+%0Aimport us.codecraft.webmagic.selector.Selectable;
 %0A%0Aimport
@@ -979,11 +979,497 @@
 ;b%22);%0A%09%7D
+%0A%0A%09@Test%0A%09public void testAHrefExtract()%7B%0A%09%09Html html = new Html(%22%3Ca data-tip=%5C%22p$t$xxx%5C%22 href=%5C%22/xx/xx%5C%22%3Exx%3C/a%3E%22);%0A%09%09assertThat(html.links().all()).contains(%22/xx/xx%22);%0A%09%7D%0A%0A%09@Test%0A%09public void testNthNodesGet()%7B%0A%09%09Html html = new Html(%22%3Ca data-tip=%5C%22p$t$xxx%5C%22 href=%5C%22/xx/xx%5C%22%3Exx%3C/a%3E%22);%0A%09%09assertThat(html.xpath(%22//a%5B1%5D/@href%22).get()).isEqualTo(%22/xx/xx%22);%0A%09%09Selectable selectable = html.xpath(%22//a%5B1%5D%22).nodes().get(0);%0A%09%09assertThat(selectable.xpath(%22@href%22).get()).isEqualTo(%22/xx/xx%22);%0A%09%7D
 %0A%7D%0A

test for issue #200