Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public boolean equals(Object obj) {

if (obj instanceof AttributeNode other) {
return name.equals(other.name) && //
getValue().equals(other.getValue()) && //
Objects.equals(getValue(), other.getValue()) && //
Objects.equals(prefix, other.prefix) && //
Objects.equals(namespace, other.namespace);
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/ch/digitalfondue/jfiveparse/DOMTokenList.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ public boolean toggle(String name) {
}

public boolean toggle(String name, boolean condition) {
return condition && toggle(name);
if (condition) {
add(name);
return true;
} else {
remove(name);
return false;
}
}

public String item(int index) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/digitalfondue/jfiveparse/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Node cloneNode(boolean deep) {
Document cloned = new Document();
if (doctype != null) {
cloned.doctype = (DocumentType) doctype.cloneNode(true);
cloned.doctype.parentNode = this;
cloned.doctype.parentNode = cloned;
}
if (!deep) {
return cloned;
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/ch/digitalfondue/jfiveparse/AttributeNodeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright © 2015 digitalfondue (info@digitalfondue.ch)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.digitalfondue.jfiveparse;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
* Tests for {@link AttributeNode}.
*/
class AttributeNodeTest {

@Test
void equalsWithBothValuesNull() {
AttributeNode a = new AttributeNode("src", null);
AttributeNode b = new AttributeNode("src", null);
assertEquals(a, b);
assertEquals(b, a);
}

@Test
void equalsWithOneValueNull() {
AttributeNode a = new AttributeNode("src", null);
AttributeNode b = new AttributeNode("src", "image.png");
assertNotEquals(a, b);
assertNotEquals(b, a);
}
}
62 changes: 62 additions & 0 deletions src/test/java/ch/digitalfondue/jfiveparse/DOMTokenListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright © 2015 digitalfondue (info@digitalfondue.ch)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.digitalfondue.jfiveparse;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;


class DOMTokenListTest {

private DOMTokenList classListFor(String classAttr) {
Element el = new Element("div");
if (classAttr != null) {
el.setAttribute("class", classAttr);
}
return new DOMTokenList(el, "class");
}

@Test
void toggleWithForceTrueAddsAbsentToken() {
DOMTokenList list = classListFor(null);
assertTrue(list.toggle("foo", true));
assertTrue(list.contains("foo"));
}

@Test
void toggleWithForceTrueKeepsPresentToken() {
DOMTokenList list = classListFor("foo");
assertTrue(list.toggle("foo", true));
assertTrue(list.contains("foo"));
}

@Test
void toggleWithForceFalseRemovesPresentToken() {
DOMTokenList list = classListFor("foo bar");
assertFalse(list.toggle("foo", false));
assertFalse(list.contains("foo"));
assertTrue(list.contains("bar"));
}

@Test
void toggleWithForceFalseIsNoOpOnAbsentToken() {
DOMTokenList list = classListFor("bar");
assertFalse(list.toggle("foo", false));
assertFalse(list.contains("foo"));
assertTrue(list.contains("bar"));
}
}
36 changes: 35 additions & 1 deletion src/test/java/ch/digitalfondue/jfiveparse/DocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.io.StringReader;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;


class DocumentTest {
Expand All @@ -44,4 +44,38 @@ void checkDocumentRelatedMethods() {

assertEquals("<body id=\"newBody\"></body>", d1.getBody().getOuterHTML());
}

@Test
void cloneNodeShallowDoctypeParentIsClone() {
Document original = new Parser().parse("<!DOCTYPE html><div>Hello</div>");
Document cloned = (Document) original.cloneNode(false);

assertNotNull(cloned.getDoctype());
assertSame(cloned, cloned.getDoctype().getParentNode());
assertNotSame(original, cloned.getDoctype().getParentNode());
}

@Test
void cloneNodeDeepDoctypeParentIsClone() {
Document original = new Parser().parse("<!DOCTYPE html><div>Hello</div>");
Document cloned = (Document) original.cloneNode(true);

assertNotNull(cloned.getDoctype());
assertSame(cloned, cloned.getDoctype().getParentNode());
assertNotSame(original, cloned.getDoctype().getParentNode());
}

@Test
void cloneNodeDeepPreservesContent() {
Document original = new Parser().parse("<!DOCTYPE html><div>Hello</div>");
Document cloned = (Document) original.cloneNode(true);
assertEquals(HtmlSerializer.serialize(original), HtmlSerializer.serialize(cloned));
}

@Test
void cloneNodeShallowHasNoChildren() {
Document original = new Parser().parse("<!DOCTYPE html><div>Hello</div>");
Document cloned = (Document) original.cloneNode(false);
assertEquals(0, cloned.getChildNodes().size());
}
}
11 changes: 8 additions & 3 deletions src/test/java/ch/digitalfondue/jfiveparse/NodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,14 @@ void testClass() {
assertEquals(Arrays.asList("hurr", "plop"), e.getClassList());

assertFalse(e.getClassList().toggle("plop", false));
assertEquals(Arrays.asList("hurr", "plop"), e.getClassList());
assertFalse(e.getClassList().toggle("plop", true));
assertEquals(List.of("hurr"), e.getClassList());
assertEquals(Arrays.asList("hurr"), e.getClassList());
assertFalse(e.getClassList().toggle("plop", false));
assertEquals(Arrays.asList("hurr"), e.getClassList());

assertTrue(e.getClassList().toggle("plop", true));
assertEquals(List.of("hurr", "plop"), e.getClassList());
assertTrue(e.getClassList().toggle("plop", true));
assertEquals(List.of("hurr", "plop"), e.getClassList());

Document doc2 = parser.parse("<div id=myid class=' my class \n abc '></div>");
Element e2 = doc2.getElementById("myid");
Expand Down
Loading