From 0ce2d09d0cb8df9b7d6de6255d6d45f3ba3a7734 Mon Sep 17 00:00:00 2001 From: "jon.mcpherson" Date: Fri, 19 Apr 2024 15:36:18 -0700 Subject: [PATCH] Fix equals overrides to check underlying and remove deprecated normalize function --- .../scala/com/iterable/scalasoup/Attribute.scala | 12 ++++++++---- .../src/main/scala/com/iterable/scalasoup/Node.scala | 9 ++++----- .../com/iterable/scalasoup/mutable/package.scala | 2 -- .../scala/com/iterable/scalasoup/ElementSpec.scala | 7 +++++++ .../scala/com/iterable/scalasoup/impl/package.scala | 2 -- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/core/src/main/scala/com/iterable/scalasoup/Attribute.scala b/core/src/main/scala/com/iterable/scalasoup/Attribute.scala index 7af92f1..4bb02c8 100644 --- a/core/src/main/scala/com/iterable/scalasoup/Attribute.scala +++ b/core/src/main/scala/com/iterable/scalasoup/Attribute.scala @@ -21,8 +21,10 @@ final class Attribute( override def toString: String = underlying.toString - @SuppressWarnings(Array("org.wartremover.warts.Equals")) - override def equals(other: Any): Boolean = underlying.equals(other) + override def equals(other: Any): Boolean = other match { + case otherAttr: Attribute => underlying.equals(otherAttr.underlying) + case _ => false + } override def hashCode: Int = underlying.hashCode @@ -73,8 +75,10 @@ final class Attributes( override def toString: String = underlying.toString - @SuppressWarnings(Array("org.wartremover.warts.Equals")) - override def equals(other: Any): Boolean = underlying.equals(other) + override def equals(other: Any): Boolean = other match { + case otherAttrs: Attributes => underlying.equals(otherAttrs.underlying) + case _ => false + } override def hashCode: Int = underlying.hashCode diff --git a/core/src/main/scala/com/iterable/scalasoup/Node.scala b/core/src/main/scala/com/iterable/scalasoup/Node.scala index b2159b0..674d76d 100644 --- a/core/src/main/scala/com/iterable/scalasoup/Node.scala +++ b/core/src/main/scala/com/iterable/scalasoup/Node.scala @@ -76,8 +76,10 @@ sealed abstract class Node[A <: ParentState] private[scalasoup](private[scalasou override def toString: String = underlying.toString - @SuppressWarnings(Array("org.wartremover.warts.Equals")) - override def equals(other: Any): Boolean = underlying.equals(other) + override def equals(other: Any): Boolean = other match { + case otherNode: Node[_] => underlying.equals(otherNode.underlying) + case _ => false + } def hasSameValue(other: Any): Boolean = underlying.hasSameValue(other) @@ -525,9 +527,6 @@ final class Document[A <: ParentState] private[scalasoup] (private[scalasoup] ov def createElement(tagName: String): Element[ParentState.NoParent] = Element.fromUnderlying(underlying.createElement(tagName)) - @deprecated("normalization occurs during the HTML parse, this method is no longer useful and will be retired in the next release.") - def withNormalise(): Document [ParentState.NoParent]= withClone(_.normalise()) - def charset: Charset = underlying.charset def withCharset(charset: Charset): Document[ParentState.NoParent] = withClone(_.charset(charset)) diff --git a/core/src/main/scala/com/iterable/scalasoup/mutable/package.scala b/core/src/main/scala/com/iterable/scalasoup/mutable/package.scala index 2290a65..ec5703d 100644 --- a/core/src/main/scala/com/iterable/scalasoup/mutable/package.scala +++ b/core/src/main/scala/com/iterable/scalasoup/mutable/package.scala @@ -103,8 +103,6 @@ package object mutable { def setTitle(title: String): Unit = document.underlying.title(title) - def normalise(): Unit = document.underlying.normalise() - def head(): Element[ParentState.HasParent] = new Element(document.underlying.head()) def body(): Element[ParentState.HasParent] = new Element(document.underlying.body()) diff --git a/core/src/test/scala/com/iterable/scalasoup/ElementSpec.scala b/core/src/test/scala/com/iterable/scalasoup/ElementSpec.scala index b63a899..725b850 100644 --- a/core/src/test/scala/com/iterable/scalasoup/ElementSpec.scala +++ b/core/src/test/scala/com/iterable/scalasoup/ElementSpec.scala @@ -31,6 +31,13 @@ class ElementSpec extends AnyFlatSpec with Matchers { assertTypeError("""document.select("a[")""") } + "The equals method" should "check equality with the underlying JSoup Node and Attributes" in { + val doc = ScalaSoup.parse("""Hello, World!""") + doc.body.get.childNodes.head.equals(doc.body.get.childNodes.head) shouldBe true + doc.body.get.attributes.head.equals(doc.body.get.attributes.head) shouldBe true + doc.body.get.attributes.equals(doc.body.get.attributes) shouldBe true + } + "The Wikipedia readme example" should "compile" in { import org.http4s.blaze.client.BlazeClientBuilder diff --git a/dsl/src/main/scala/com/iterable/scalasoup/impl/package.scala b/dsl/src/main/scala/com/iterable/scalasoup/impl/package.scala index 2a25422..f5b1eb3 100644 --- a/dsl/src/main/scala/com/iterable/scalasoup/impl/package.scala +++ b/dsl/src/main/scala/com/iterable/scalasoup/impl/package.scala @@ -105,8 +105,6 @@ package object impl { def setTitle(title: String): Modification[Unit] = modification(_.setTitle(title)) - def normalise: Modification[Unit] = modification(_.normalise()) - def getOrCreateHead: Modification[Element[ParentState.HasParent]] = modification(_.head()) def getOrCreateBody: Modification[Element[ParentState.HasParent]] = modification(_.body())