Skip to content

Commit

Permalink
Fix equals overrides to check underlying and remove deprecated normal…
Browse files Browse the repository at this point in the history
…ize function
  • Loading branch information
JonMcPherson committed Apr 19, 2024
1 parent c44a608 commit 0ce2d09
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
12 changes: 8 additions & 4 deletions core/src/main/scala/com/iterable/scalasoup/Attribute.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
9 changes: 4 additions & 5 deletions core/src/main/scala/com/iterable/scalasoup/Node.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
7 changes: 7 additions & 0 deletions core/src/test/scala/com/iterable/scalasoup/ElementSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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("""<html lang="en"><head></head><body title="Hello World">Hello, World!</body></html>""")
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

Expand Down
2 changes: 0 additions & 2 deletions dsl/src/main/scala/com/iterable/scalasoup/impl/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 0ce2d09

Please sign in to comment.