@@ -1665,4 +1665,90 @@ describe("Equal.equals", () => {
16651665 expect ( Equal . equals ( obj1 , obj2 ) ) . toBe ( false )
16661666 } )
16671667 } )
1668+
1669+ describe ( "DOM nodes and ranges" , ( ) => {
1670+ it ( "compares DOM nodes and ranges by reference without reading throwing getters" , async ( ) => {
1671+ const { Window } = await import ( "happy-dom" )
1672+ const win = new Window ( )
1673+ const doc = win . document
1674+ const a = doc . createElement ( "div" )
1675+ const b = doc . createElement ( "div" )
1676+ const svgA = doc . createElementNS ( "http://www.w3.org/2000/svg" , "svg" )
1677+ const svgB = doc . createElementNS ( "http://www.w3.org/2000/svg" , "svg" )
1678+ let getterCalls = 0
1679+ const installThrowingGetter = ( node : object ) =>
1680+ Object . defineProperty ( node , "brokenSvgMetric" , {
1681+ enumerable : true ,
1682+ get ( ) {
1683+ getterCalls ++
1684+ throw new TypeError ( "Illegal invocation" )
1685+ }
1686+ } )
1687+ installThrowingGetter ( svgA )
1688+ installThrowingGetter ( svgB )
1689+
1690+ assert . isTrue ( Equal . equals ( a , a ) )
1691+ assert . isFalse ( Equal . equals ( a , b ) )
1692+ assert . isFalse ( Equal . equals ( a , { } ) )
1693+ assert . isFalse ( Equal . equals ( { } , a ) )
1694+ const otherWin = new Window ( )
1695+ assert . isTrue ( Equal . equals ( doc , doc ) )
1696+ assert . isFalse ( Equal . equals ( doc , otherWin . document ) )
1697+
1698+ // Nested wrappers compare the same DOM node as equal and distinct nodes as unequal
1699+ // without invoking DOM getters (including during Hash.hash on shared references).
1700+ assert . isTrue ( Equal . equals ( { node : svgA } , { node : svgA } ) )
1701+ assert . isFalse ( Equal . equals ( { node : svgA } , { node : svgB } ) )
1702+ assert . strictEqual ( Hash . hash ( { node : svgA } ) , Hash . hash ( { node : svgA } ) )
1703+ assert . strictEqual ( getterCalls , 0 )
1704+
1705+ // Detached documents (`defaultView === null`) and their elements.
1706+ const detached = doc . implementation . createHTMLDocument ( )
1707+ const detachedA = detached . createElement ( "span" )
1708+ const detachedB = detached . createElement ( "span" )
1709+ installThrowingGetter ( detachedA )
1710+ installThrowingGetter ( detachedB )
1711+ assert . isTrue ( Equal . equals ( detached , detached ) )
1712+ assert . isTrue ( Equal . equals ( detachedA , detachedA ) )
1713+ assert . isFalse ( Equal . equals ( detachedA , detachedB ) )
1714+ assert . strictEqual ( getterCalls , 0 )
1715+
1716+ // DOM ranges compare by reference without walking startContainer/endContainer.
1717+ doc . body . append ( a , b )
1718+ const rangeA = doc . createRange ( )
1719+ rangeA . selectNode ( a )
1720+ const rangeB = doc . createRange ( )
1721+ rangeB . selectNode ( a )
1722+ assert . isTrue ( Equal . equals ( rangeA , rangeA ) )
1723+ assert . isFalse ( Equal . equals ( rangeA , rangeB ) )
1724+ assert . isTrue ( Equal . equals ( { range : rangeA } , { range : rangeA } ) )
1725+ assert . isFalse ( Equal . equals ( { range : rangeA } , { range : rangeB } ) )
1726+ assert . strictEqual ( Hash . hash ( rangeA ) , Hash . hash ( rangeA ) )
1727+
1728+ // Custom Equal/Hash implementations on a DOM node still take precedence.
1729+ const customA = doc . createElement ( "span" )
1730+ const customB = doc . createElement ( "span" )
1731+ Object . assign ( customA , {
1732+ [ Equal . symbol ] ( that : unknown ) {
1733+ return that === customB || that === customA
1734+ } ,
1735+ [ Hash . symbol ] ( ) {
1736+ return 42
1737+ }
1738+ } )
1739+ Object . assign ( customB , {
1740+ [ Equal . symbol ] ( that : unknown ) {
1741+ return that === customA || that === customB
1742+ } ,
1743+ [ Hash . symbol ] ( ) {
1744+ return 42
1745+ }
1746+ } )
1747+ assert . isTrue ( Equal . equals ( customA , customB ) )
1748+ assert . strictEqual ( Hash . hash ( customA ) , 42 )
1749+
1750+ await win . happyDOM . close ( )
1751+ await otherWin . happyDOM . close ( )
1752+ } )
1753+ } )
16681754} )
0 commit comments