diff --git a/src/main/java/ch/digitalfondue/jfiveparse/AttributeNode.java b/src/main/java/ch/digitalfondue/jfiveparse/AttributeNode.java index 5139cfd8..c3d2476a 100644 --- a/src/main/java/ch/digitalfondue/jfiveparse/AttributeNode.java +++ b/src/main/java/ch/digitalfondue/jfiveparse/AttributeNode.java @@ -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); } diff --git a/src/main/java/ch/digitalfondue/jfiveparse/DOMTokenList.java b/src/main/java/ch/digitalfondue/jfiveparse/DOMTokenList.java index a847b457..59f09549 100644 --- a/src/main/java/ch/digitalfondue/jfiveparse/DOMTokenList.java +++ b/src/main/java/ch/digitalfondue/jfiveparse/DOMTokenList.java @@ -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) { diff --git a/src/main/java/ch/digitalfondue/jfiveparse/Document.java b/src/main/java/ch/digitalfondue/jfiveparse/Document.java index ca39483b..a8087a51 100644 --- a/src/main/java/ch/digitalfondue/jfiveparse/Document.java +++ b/src/main/java/ch/digitalfondue/jfiveparse/Document.java @@ -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; diff --git a/src/test/java/ch/digitalfondue/jfiveparse/AttributeNodeTest.java b/src/test/java/ch/digitalfondue/jfiveparse/AttributeNodeTest.java new file mode 100644 index 00000000..6f9d73c4 --- /dev/null +++ b/src/test/java/ch/digitalfondue/jfiveparse/AttributeNodeTest.java @@ -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); + } +} diff --git a/src/test/java/ch/digitalfondue/jfiveparse/DOMTokenListTest.java b/src/test/java/ch/digitalfondue/jfiveparse/DOMTokenListTest.java new file mode 100644 index 00000000..1a58281e --- /dev/null +++ b/src/test/java/ch/digitalfondue/jfiveparse/DOMTokenListTest.java @@ -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")); + } +} diff --git a/src/test/java/ch/digitalfondue/jfiveparse/DocumentTest.java b/src/test/java/ch/digitalfondue/jfiveparse/DocumentTest.java index 02380549..e30d0e8f 100644 --- a/src/test/java/ch/digitalfondue/jfiveparse/DocumentTest.java +++ b/src/test/java/ch/digitalfondue/jfiveparse/DocumentTest.java @@ -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 { @@ -44,4 +44,38 @@ void checkDocumentRelatedMethods() { assertEquals("", d1.getBody().getOuterHTML()); } + + @Test + void cloneNodeShallowDoctypeParentIsClone() { + Document original = new Parser().parse("
Hello
"); + 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("
Hello
"); + 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("
Hello
"); + Document cloned = (Document) original.cloneNode(true); + assertEquals(HtmlSerializer.serialize(original), HtmlSerializer.serialize(cloned)); + } + + @Test + void cloneNodeShallowHasNoChildren() { + Document original = new Parser().parse("
Hello
"); + Document cloned = (Document) original.cloneNode(false); + assertEquals(0, cloned.getChildNodes().size()); + } } diff --git a/src/test/java/ch/digitalfondue/jfiveparse/NodeTest.java b/src/test/java/ch/digitalfondue/jfiveparse/NodeTest.java index b13c3c73..f1300943 100644 --- a/src/test/java/ch/digitalfondue/jfiveparse/NodeTest.java +++ b/src/test/java/ch/digitalfondue/jfiveparse/NodeTest.java @@ -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("
"); Element e2 = doc2.getElementById("myid");