Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two DOMs or DOM nodes in general?

I am using var win1 = open.window(...); var win2 = open.window(...); to open 2 tabs/windows in Firefox - now I want to compare the two DOMs (document object models) for similarity. So I have got two DOMs containing a very similar page. The pages have the same HTML but executed different JavaScript files.

In general I check if HTML and CSS is the same:

    var html1 = win1.document.body.innerHTML;
var html2 = win2.document.body.innerHTML;
if (html1 == html2) { ... }
var css1 = win1.document.body.style.cssText
var css2 = win2.document.body.style.cssText
if (css1 == css2) { ... }

But comparing all DOM nodes seems to give bad results:

var bodyNodes1 = win1.document.body.getElementsByTagName('*');
var bodyNodes2 = win2.document.body.getElementsByTagName('*');

bodyNodes1[123].innerHTML isn't neccessary similar bodyNodes2[123].innerHTML

Which methods can be used to compare DOM nodes? Do any Framework/Libraries/Scripts exist for testing pages for similarity?

I am very thankful for any hints. :-)

like image 263
Claudio Avatar asked Dec 14 '10 15:12

Claudio


People also ask

How do you compare two nodes?

isEqualNode() The isEqualNode() method of the Node interface tests whether two nodes are equal. Two nodes are equal when they have the same type, defining characteristics (for elements, this would be their ID, number of children, and so forth), its attributes match, and so on.

Which algorithm is used to compare the DOM structure?

React uses an efficient diff algorithm to compare the versions of virtual DOM. It then makes sure that batched updates are sent to the real DOM for repainting or re-rendering of the UI.

What's the difference between DOM node and element?

So, in a nutshell, a node is any DOM object. An element is one specific type of node as there are many other types of nodes (text nodes, comment nodes, document nodes, etc...). The DOM consists of a hierarchy of nodes where each node can have a parent, a list of child nodes and a nextSibling and previousSibling.

What does each node in the DOM tree represent?

The Document Object Model (DOM) is a cross-platform and language-independent interface that treats an XML or HTML document as a tree structure wherein each node is an object representing a part of the document.


1 Answers

I think what you are looking for is either:

isEqualNode : http://help.dottoro.com/ljlpvjmd.php
isSameNode : http://help.dottoro.com/ljqqqfft.php

Hope this helps.

like image 146
AgentRev Avatar answered Sep 30 '22 00:09

AgentRev