示例#1
0
  @Test
  public void testClone() {
    Document doc = Jsoup.parse("<div><p>One<p><span>Two</div>");

    Element p = doc.select("p").get(1);
    Element clone = p.clone();

    assertNull(clone.parent()); // should be orphaned
    assertEquals(0, clone.siblingIndex);
    assertEquals(1, p.siblingIndex);
    assertNotNull(p.parent());

    clone.append("<span>Three");
    assertEquals(
        "<p><span>Two</span><span>Three</span></p>", TextUtil.stripNewlines(clone.outerHtml()));
    assertEquals(
        "<div><p>One</p><p><span>Two</span></p></div>",
        TextUtil.stripNewlines(doc.body().html())); // not modified

    doc.body().appendChild(clone); // adopt
    assertNotNull(clone.parent());
    assertEquals(
        "<div><p>One</p><p><span>Two</span></p></div><p><span>Two</span><span>Three</span></p>",
        TextUtil.stripNewlines(doc.body().html()));
  }
示例#2
0
  @Test
  public void setHtml() {
    Document doc = Jsoup.parse("<p>One</p><p>Two</p><p>Three</p>");
    Elements ps = doc.select("p");

    ps.prepend("<b>Bold</b>").append("<i>Ital</i>");
    assertEquals("<p><b>Bold</b>Two<i>Ital</i></p>", TextUtil.stripNewlines(ps.get(1).outerHtml()));

    ps.html("<span>Gone</span>");
    assertEquals("<p><span>Gone</span></p>", TextUtil.stripNewlines(ps.get(1).outerHtml()));
  }
示例#3
0
  @Test
  public void after() {
    Document doc = Jsoup.parse("<div><p>Hello</p><p>There</p></div>");
    Element p1 = doc.select("p").first();
    p1.after("<div>one</div><div>two</div>");
    assertEquals(
        "<div><p>Hello</p><div>one</div><div>two</div><p>There</p></div>",
        TextUtil.stripNewlines(doc.body().html()));

    doc.select("p").last().after("<p>Three</p><!-- four -->");
    assertEquals(
        "<div><p>Hello</p><div>one</div><div>two</div><p>There</p><p>Three</p><!-- four --></div>",
        TextUtil.stripNewlines(doc.body().html()));
  }
示例#4
0
  @Test
  public void testWrap() {
    Document doc = Jsoup.parse("<div><p>Hello</p><p>There</p></div>");
    Element p = doc.select("p").first();
    p.wrap("<div class='head'></div>");
    assertEquals(
        "<div><div class=\"head\"><p>Hello</p></div><p>There</p></div>",
        TextUtil.stripNewlines(doc.body().html()));

    Element ret = p.wrap("<div><div class=foo></div><p>What?</p></div>");
    assertEquals(
        "<div><div class=\"head\"><div><div class=\"foo\"><p>Hello</p></div><p>What?</p></div></div><p>There</p></div>",
        TextUtil.stripNewlines(doc.body().html()));

    assertEquals(ret, p);
  }
示例#5
0
 @Test
 public void testSetHtml() {
   Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
   Element div = doc.getElementById("1");
   div.html("<p>there</p><p>now</p>");
   assertEquals("<p>there</p><p>now</p>", TextUtil.stripNewlines(div.html()));
 }
示例#6
0
 @Test
 public void unwrap() {
   String h = "<div><font>One</font> <font><a href=\"/\">Two</a></font></div";
   Document doc = Jsoup.parse(h);
   doc.select("font").unwrap();
   assertEquals("<div>One <a href=\"/\">Two</a></div>", TextUtil.stripNewlines(doc.body().html()));
 }
示例#7
0
 @Test
 public void testAddNewText() {
   Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
   Element div = doc.getElementById("1");
   div.appendText(" there & now >");
   assertEquals("<p>Hello</p> there &amp; now &gt;", TextUtil.stripNewlines(div.html()));
 }
示例#8
0
  @Test
  public void simpleBehaviourTest() {
    String h = "<div><p class=foo><a href='http://evil.com'>Hello <b id=bar>there</b>!</a></div>";
    String cleanHtml = Jsoup.clean(h, Whitelist.simpleText());

    assertEquals("Hello <b>there</b>!", TextUtil.stripNewlines(cleanHtml));
  }
示例#9
0
  @Test
  public void simpleBehaviourTest2() {
    String h = "Hello <b>there</b>!";
    String cleanHtml = Jsoup.clean(h, Whitelist.simpleText());

    assertEquals("Hello <b>there</b>!", TextUtil.stripNewlines(cleanHtml));
  }
示例#10
0
 @Test
 public void outerHtml() {
   Document doc = Jsoup.parse("<div><p>Hello</p></div><div><p>There</p></div>");
   Elements divs = doc.select("div");
   assertEquals(
       "<div><p>Hello</p></div><div><p>There</p></div>", TextUtil.stripNewlines(divs.outerHtml()));
 }
示例#11
0
 @Test
 public void testRelaxed() {
   String h = "<h1>Head</h1><td>One<td>Two</td>";
   String cleanHtml = Jsoup.clean(h, Whitelist.relaxed());
   assertEquals(
       "<h1>Head</h1><table><tbody><tr><td>One</td><td>Two</td></tr></tbody></table>",
       TextUtil.stripNewlines(cleanHtml));
 }
示例#12
0
 @Test
 public void testOuterHtml() {
   Document doc =
       Jsoup.parse("<div title='Tags &amp;c.'><img src=foo.png><p><!-- comment -->Hello<p>there");
   assertEquals(
       "<html><head></head><body><div title=\"Tags &amp;c.\"><img src=\"foo.png\"><p><!-- comment -->Hello</p><p>there</p></div></body></html>",
       TextUtil.stripNewlines(doc.outerHtml()));
 }
示例#13
0
 @Test
 public void after() {
   Document doc = Jsoup.parse("<p>This <a>is</a> <a>jsoup</a>.</p>");
   doc.select("a").after("<span>foo</span>");
   assertEquals(
       "<p>This <a>is</a><span>foo</span> <a>jsoup</a><span>foo</span>.</p>",
       TextUtil.stripNewlines(doc.body().html()));
 }
示例#14
0
 @Test
 public void basicWithImagesTest() {
   String h =
       "<div><p><img src='http://example.com/' alt=Image></p><p><img src='ftp://ftp.example.com'></p></div>";
   String cleanHtml = Jsoup.clean(h, Whitelist.basicWithImages());
   assertEquals(
       "<p><img src=\"http://example.com/\" alt=\"Image\" /></p><p><img /></p>",
       TextUtil.stripNewlines(cleanHtml));
 }
示例#15
0
 @Test
 public void testWrapWithRemainder() {
   Document doc = Jsoup.parse("<div><p>Hello</p></div>");
   Element p = doc.select("p").first();
   p.wrap("<div class='head'></div><p>There!</p>");
   assertEquals(
       "<div><div class=\"head\"><p>Hello</p><p>There!</p></div></div>",
       TextUtil.stripNewlines(doc.body().html()));
 }
示例#16
0
  @Test
  public void basicBehaviourTest() {
    String h =
        "<div><p><a href='javascript:sendAllMoney()'>Dodgy</a> <A HREF='HTTP://nice.com'>Nice</p><blockquote>Hello</blockquote>";
    String cleanHtml = Jsoup.clean(h, Whitelist.basic());

    assertEquals(
        "<p><a rel=\"nofollow\">Dodgy</a> <a href=\"http://nice.com\" rel=\"nofollow\">Nice</a></p><blockquote>Hello</blockquote>",
        TextUtil.stripNewlines(cleanHtml));
  }
示例#17
0
  @Test
  public void testAppendRowToTable() {
    Document doc = Jsoup.parse("<table><tr><td>1</td></tr></table>");
    Element table = doc.select("tbody").first();
    table.append("<tr><td>2</td></tr>");

    assertEquals(
        "<table><tbody><tr><td>1</td></tr><tr><td>2</td></tr></tbody></table>",
        TextUtil.stripNewlines(doc.body().html()));
  }
示例#18
0
  @Test
  public void testPrependNewHtml() {
    Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
    Element div = doc.getElementById("1");
    div.prepend("<p>there</p><p>now</p>");
    assertEquals("<p>there</p><p>now</p><p>Hello</p>", TextUtil.stripNewlines(div.html()));

    // check sibling index (reindexChildren):
    Elements ps = doc.select("p");
    for (int i = 0; i < ps.size(); i++) {
      assertEquals(i, ps.get(i).siblingIndex);
    }
  }
示例#19
0
  @Test
  public void insertChildrenAsCopy() {
    Document doc = Jsoup.parse("<div id=1>Text <p>One</p> Text <p>Two</p></div><div id=2></div>");
    Element div1 = doc.select("div").get(0);
    Element div2 = doc.select("div").get(1);
    Elements ps = doc.select("p").clone();
    ps.first().text("One cloned");
    div2.insertChildren(-1, ps);

    assertEquals(4, div1.childNodeSize()); // not moved -- cloned
    assertEquals(2, div2.childNodeSize());
    assertEquals(
        "<div id=\"1\">Text <p>One</p> Text <p>Two</p></div><div id=\"2\"><p>One cloned</p><p>Two</p></div>",
        TextUtil.stripNewlines(doc.body().html()));
  }
示例#20
0
  @Test
  public void testPrependRowToTable() {
    Document doc = Jsoup.parse("<table><tr><td>1</td></tr></table>");
    Element table = doc.select("tbody").first();
    table.prepend("<tr><td>2</td></tr>");

    assertEquals(
        "<table><tbody><tr><td>2</td></tr><tr><td>1</td></tr></tbody></table>",
        TextUtil.stripNewlines(doc.body().html()));

    // check sibling index (reindexChildren):
    Elements ps = doc.select("tr");
    for (int i = 0; i < ps.size(); i++) {
      assertEquals(i, ps.get(i).siblingIndex);
    }
  }
示例#21
0
  @Test
  public void testAddNewElement() {
    Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
    Element div = doc.getElementById("1");
    div.appendElement("p").text("there");
    div.appendElement("P").attr("CLASS", "second").text("now");
    // manually specifying tag and attributes should now preserve case, regardless of parse mode
    assertEquals(
        "<html><head></head><body><div id=\"1\"><p>Hello</p><p>there</p><P CLASS=\"second\">now</P></div></body></html>",
        TextUtil.stripNewlines(doc.html()));

    // check sibling index (with short circuit on reindexChildren):
    Elements ps = doc.select("p");
    for (int i = 0; i < ps.size(); i++) {
      assertEquals(i, ps.get(i).siblingIndex);
    }
  }