예제 #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 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()));
  }
예제 #3
0
 void reset() {
   doc.body().removeChildNodes();
   reader = new CharacterReader(input, this);
   this.errors = ParseErrorList.tracking(100);
   tokeniser = new Tokeniser(reader, errors);
   while (stack.size() > 2) stack.pollLast();
 }
예제 #4
0
 @Test
 public void testKeepsPreTextInCode() {
   String h = "<pre><code>code\n\ncode</code></pre>";
   Document doc = Jsoup.parse(h);
   assertEquals("code\n\ncode", doc.text());
   assertEquals("<pre><code>code\n\ncode</code></pre>", doc.body().html());
 }
예제 #5
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);
  }
예제 #6
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()));
 }
예제 #7
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()));
  }
예제 #8
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()));
  }
예제 #9
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);
    }
  }
예제 #10
0
  public void index(final String title, String wikitext, Document doc) {

    try {
      final String hash = new StringHash(wikitext).hash();

      // parse input with JSOUP
      class Walker {
        private int secIndex;

        Walker(int secIndex) {
          this.secIndex = secIndex;
        }

        public int walk(Element el) {
          Elements children = el.children();
          String tagName = el.tagName().toLowerCase();

          if (tagName.matches("h[1-6]")) {
            secIndex++;
            String secName = el.text();
            String key =
                "SectionTitle" + "/" + title + "/" + hash + "/" + new Integer(secIndex).toString();
            String value = secNameFilter(secName);
            dataStore.put(key, value);
          }

          for (Element child : children) {
            secIndex = (new Walker(secIndex)).walk(child);
          }

          return secIndex;
        }
      }

      (new Walker(0)).walk(doc.body());
    } catch (Exception e) {
      System.err.println(
          "SectionTitleIndexer.index failed due to exception: '" + e.toString() + "'");
    }
  }
예제 #11
0
  @Test
  public void moveByAppend() {
    // test for https://github.com/jhy/jsoup/issues/239
    // can empty an element and append its children to another element
    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);

    assertEquals(4, div1.childNodeSize());
    List<Node> children = div1.childNodes();
    assertEquals(4, children.size());

    div2.insertChildren(0, children);

    assertEquals(
        0, children.size()); // children is backed by div1.childNodes, moved, so should be 0 now
    assertEquals(0, div1.childNodeSize());
    assertEquals(4, div2.childNodeSize());
    assertEquals(
        "<div id=\"1\"></div>\n<div id=\"2\">\n Text \n <p>One</p> Text \n <p>Two</p>\n</div>",
        doc.body().html());
  }