예제 #1
0
  @Test
  public void testClonesClassnames() {
    Document doc = Jsoup.parse("<div class='one two'></div>");
    Element div = doc.select("div").first();
    Set<String> classes = div.classNames();
    assertEquals(2, classes.size());
    assertTrue(classes.contains("one"));
    assertTrue(classes.contains("two"));

    Element copy = div.clone();
    Set<String> copyClasses = copy.classNames();
    assertEquals(2, copyClasses.size());
    assertTrue(copyClasses.contains("one"));
    assertTrue(copyClasses.contains("two"));
    copyClasses.add("three");
    copyClasses.remove("one");

    assertTrue(classes.contains("one"));
    assertFalse(classes.contains("three"));
    assertFalse(copyClasses.contains("one"));
    assertTrue(copyClasses.contains("three"));

    assertEquals("", div.html());
    assertEquals("", copy.html());
  }
예제 #2
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()));
 }
예제 #3
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()));
 }
예제 #4
0
  @Test
  public void testNotPretty() {
    Document doc = Jsoup.parse("<div>   \n<p>Hello\n there\n</p></div>");
    doc.outputSettings().prettyPrint(false);
    assertEquals(
        "<html><head></head><body><div>   \n<p>Hello\n there\n</p></div></body></html>",
        doc.html());

    Element div = doc.select("div").first();
    assertEquals("   \n<p>Hello\n there\n</p>", div.html());
  }
예제 #5
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);
    }
  }
예제 #6
0
  @Test
  public void testSetHtmlTitle() {
    Document doc = Jsoup.parse("<html><head id=2><title id=1></title></head></html>");

    Element title = doc.getElementById("1");
    title.html("good");
    assertEquals("good", title.html());
    title.html("<i>bad</i>");
    assertEquals("&lt;i&gt;bad&lt;/i&gt;", title.html());

    Element head = doc.getElementById("2");
    head.html("<title><i>bad</i></title>");
    assertEquals("<title>&lt;i&gt;bad&lt;/i&gt;</title>", head.html());
  }