示例#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 testClassNames() {
    Document doc = Jsoup.parse("<div class=\"c1 c2\">C</div>");
    Element div = doc.select("div").get(0);

    assertEquals("c1 c2", div.className());

    final Set<String> set1 = div.classNames();
    final Object[] arr1 = set1.toArray();
    assertTrue(arr1.length == 2);
    assertEquals("c1", arr1[0]);
    assertEquals("c2", arr1[1]);

    // Changes to the set should not be reflected in the Elements getters
    set1.add("c3");
    assertTrue(2 == div.classNames().size());
    assertEquals("c1 c2", div.className());

    // Update the class names to a fresh set
    final Set<String> newSet = new LinkedHashSet<String>(3);
    newSet.addAll(set1);
    newSet.add("c3");

    div.classNames(newSet);

    assertEquals("c1 c2 c3", div.className());

    final Set<String> set2 = div.classNames();
    final Object[] arr2 = set2.toArray();
    assertTrue(arr2.length == 3);
    assertEquals("c1", arr2[0]);
    assertEquals("c2", arr2[1]);
    assertEquals("c3", arr2[2]);
  }
示例#3
0
  @Test
  public void testClassDomMethods() {
    Document doc =
        Jsoup.parse("<div><span class=' mellow yellow '>Hello <b>Yellow</b></span></div>");
    List<Element> els = doc.getElementsByAttribute("class");
    Element span = els.get(0);
    assertEquals("mellow yellow", span.className());
    assertTrue(span.hasClass("mellow"));
    assertTrue(span.hasClass("yellow"));
    Set<String> classes = span.classNames();
    assertEquals(2, classes.size());
    assertTrue(classes.contains("mellow"));
    assertTrue(classes.contains("yellow"));

    assertEquals("", doc.className());
    classes = doc.classNames();
    assertEquals(0, classes.size());
    assertFalse(doc.hasClass("mellow"));
  }
示例#4
0
 @Override
 public Element clone() {
   Element clone = (Element) super.clone();
   clone.classNames = null; // derived on first hit, otherwise gets a pointer to source classnames
   return clone;
 }