Skip to content

Commit 46512fa

Browse files
committed
Bugfix, (partial) support for handlers
1 parent 2b35134 commit 46512fa

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

spec/NoBacktrackWsmSpec.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ describe("NoBacktrackWsm behaviour", function() {
2424

2525
beforeEach(function() {
2626
pages = [];
27-
pages[1] = DomNode.parseFromString("<#document><html><h1>Page 1</h1><p><a>To page 2</a><a>To page 3</a></p></html></#document>");
28-
pages[2] = DomNode.parseFromString("<#document><html><h1>Page 2</h1><a>To page 3</a><a>To page 4</a></html></#document>");
29-
pages[3] = DomNode.parseFromString("<#document><html><h1>Page 3</h1><a>To page 3</a><a>To page 4</a></html></#document>");
30-
pages[4] = DomNode.parseFromString("<#document><html><h1>Page 4</h1><a>To page 3</a><a>To page 4</a></html></#document>");
27+
pages[1] = DomNode.parseFromString("<#document><html><body><h1>Page 1</h1><p><a>To page 2</a><a>To page 3</a></p></body></html></#document>");
28+
pages[2] = DomNode.parseFromString("<#document><html><body><h1>Page 2</h1><a>To page 3</a><a>To page 4</a></body></html></#document>");
29+
pages[3] = DomNode.parseFromString("<#document><html><body><h1>Page 3</h1><a>To page 3</a><a>To page 4</a></body></html></#document>");
30+
pages[4] = DomNode.parseFromString("<#document><html><body><h1>Page 4</h1><a>To page 3</a><a>To page 4</a></body></html></#document>");
3131
wsm = new NoBacktrackWsm();
3232
});
3333

@@ -39,17 +39,17 @@ describe("NoBacktrackWsm behaviour", function() {
3939

4040
p = new DomNode(pages[2]);
4141
p.setAllMarks(WsmNode.CLICKED);
42-
wsm.setCurrentDom(p, "html/p[0]/a[0]");
42+
wsm.setCurrentDom(p, "html/body[0]/p[0]/a[0]");
4343

4444
p = new DomNode(pages[3]);
4545
p.setAllMarks(WsmNode.CLICKED);
46-
el = p.getElementFromPathString("html/a[1]");
46+
el = p.getElementFromPathString("html/body[0]/a[1]");
4747
el.setMark(WsmNode.NOT_CLICKED); // Leave a node unclicked in this page
48-
wsm.setCurrentDom(p, "html/a[0]");
48+
wsm.setCurrentDom(p, "html/body[0]/a[0]");
4949

5050
p = new DomNode(pages[4]);
5151
p.setAllMarks(WsmNode.CLICKED);
52-
wsm.setCurrentDom(p, "html/p[0]/a[0]");
52+
wsm.setCurrentDom(p, "html/body[0]/p[0]/a[0]");
5353

5454
// Ask for next click: should be empty
5555
el_to_click = wsm.getNextClick();

src/Dom.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ function DomNode(contents) // {{{
214214

215215
this.m_attributes = [];
216216

217+
this.m_handlers = [];
218+
217219
this.m_children = [];
218220

219221
/**
@@ -577,6 +579,7 @@ function DomNode(contents) // {{{
577579
*/
578580
this.clone = function(other_node) // {{{
579581
{
582+
var i = 0;
580583
if (!(other_node instanceof DomNode))
581584
{
582585
console.error("Trying to clone DomNode from an object that is not a DomNode");
@@ -585,15 +588,22 @@ function DomNode(contents) // {{{
585588
this.m_name = other_node.m_name;
586589
this.m_isLeaf = other_node.m_isLeaf;
587590
this.m_attributes = [];
588-
for (var i = 0; i < other_node.m_attributes; i++)
591+
for (i = 0; i < other_node.m_attributes; i++)
589592
{
590593
var avp = other_node.m_attributes[i];
591594
this.m_attributes.push(new DomNodeAttribute(avp.m_name, avp.m_value));
592595
}
596+
this.m_handlers = [];
597+
{
598+
if (other_node.m_handlers["onclick"] === true)
599+
{
600+
this.m_handlers["onclick"] = true;
601+
}
602+
}
593603
this.m_children = [];
594-
for (var j = 0; j < other_node.m_children.length; j++)
604+
for (i = 0; i < other_node.m_children.length; i++)
595605
{
596-
var child = other_node.m_children[j];
606+
var child = other_node.m_children[i];
597607
this.m_children.push(new DomNode(child));
598608
}
599609
}; // }}}
@@ -639,6 +649,18 @@ DomNode.parseFromDom = function(e) // {{{
639649
out.m_attributes.push(dna);
640650
}
641651
}
652+
if (!DomNode.IGNORE_HANDLERS)
653+
{
654+
// For handlers, we only record whether an element has a handler
655+
// attached to it --not the contents of that handler
656+
// TODO: this doesn't work. When a page has handlers attached
657+
// with jQuery, onclick is always null regardless. Find a way to
658+
// detect handlers when attached with jQuery.
659+
if (e.onclick !== undefined && e.onclick !== null)
660+
{
661+
out.m_handlers["onclick"] = true;
662+
}
663+
}
642664
for (i = 0; i < e.childNodes.length; i++)
643665
{
644666
var child = e.childNodes[i];
@@ -735,6 +757,13 @@ DomNode.are_equal = function(n1, n2) // {{{
735757
* @type {boolean}
736758
*/
737759
DomNode.IGNORE_ATTRIBUTES = true;
760+
761+
/**
762+
* Whether to ignore event handlers when comparing nodes
763+
* @constant
764+
* @type {boolean}
765+
*/
766+
DomNode.IGNORE_HANDLERS = false;
738767
// }}}
739768

740769

src/NoBacktrackWsm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function NoBacktrackWsm() // extends VanillaWsm {{{
105105
var node_dest_id = pseg.getDestination();
106106
visited_ids[node_dest_id] = true; // Mark node as visited
107107
var node_dest = this.getNodeFromId(node_dest_id);
108-
if (!node_dest.isExhausted())
108+
if (!node_dest.isExhausted(this.isAcceptableClick))
109109
{
110110
// Yes, we are done: return sequence leading to that node
111111
return pseq;

src/Wsm.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function WsmNode(id) // {{{
8080
* @return {boolean} true or false, depending on whether the node still
8181
* has elements that have not been clicked
8282
*/
83-
this.isExhausted = function(is_acceptacle_click) // {{{
83+
this.isExhausted = function(is_acceptable_click) // {{{
8484
{
8585
if (this.m_exhausted === true)
8686
{
@@ -95,7 +95,7 @@ function WsmNode(id) // {{{
9595
return false;
9696
}
9797
// Otherwise, check if an element remains to be clicked
98-
this.computeNextElement(is_acceptacle_click);
98+
this.computeNextElement(is_acceptable_click);
9999
if (this.m_nextElementToClick !== "")
100100
{
101101
// This method produced a new next element to click,

0 commit comments

Comments
 (0)