2 * (C) Copyright 2009 Shawn Betts
3 * (C) Copyright 2009,2011 John J. Foerch <jjfoerch@earthlink.net>
5 * Use, modification, and distribution are subject to the terms specified in the
9 define_variable("headings_xpath",
10 "//h1 | //h2 | //h3 | //h4 | //h5 | //h6 | //xhtml:h1 | "+
11 "//xhtml:h2 | //xhtml:h3 | //xhtml:h4 | //xhtml:h5 | //xhtml:h6",
12 "The xpath expression used by next-heading and previous-heading to find "+
13 "headings. Users will rarely need to change the value of this, but it "+
14 "exists especially for page-modes to override with a site-specific "+
18 define_variable("scroll_to_heading_wrap", true,
19 "If true, will wrap to the topmost heading when the viewport is at the "+
20 "bottom of the document and the user tries to access the next heading. "+
21 "Does the equivalent thing for \"previous heading\" as well.");
24 define_browser_object_class("next-heading", null,
26 let xpr = I.buffer.document.evaluate(
27 I.local.headings_xpath, I.buffer.document, xpath_lookup_namespace,
28 Ci.nsIDOMXPathResult.ORDERED_NODE_ITERATOR_TYPE, null),
29 heading, found = null, foundtop = null,
30 first = null, firsttop = null;
31 while ((heading = xpr.iterateNext())) {
32 let rect = heading.getBoundingClientRect();
33 if (rect.bottom - rect.top < 2)
35 if (! first || rect.top < firsttop) {
39 if (rect.top > 2 && (! found || rect.top < foundtop)) {
44 // scrollY can exceed scrollMaxY
45 let eod = I.buffer.scrollY - I.buffer.scrollMaxY >= 0;
46 if ((!found || eod) && scroll_to_heading_wrap)
48 yield co_return(found);
52 define_browser_object_class("previous-heading", null,
54 let xpr = I.buffer.document.evaluate(
55 I.local.headings_xpath, I.buffer.document, xpath_lookup_namespace,
56 Ci.nsIDOMXPathResult.ORDERED_NODE_ITERATOR_TYPE, null),
57 heading, found = null, foundtop = null,
58 last = null, lasttop = null;
59 while ((heading = xpr.iterateNext())) {
60 let rect = heading.getBoundingClientRect();
61 if (rect.bottom - rect.top < 2)
63 if (rect.top < -1 && (! found || rect.top > foundtop)) {
67 if (! last || rect.top > lasttop) {
72 if (! found && scroll_to_heading_wrap)
74 yield co_return(found);
79 var o = yield read_browser_object(I);
80 // no scrolling and no error if we failed to get an object.
83 if (o instanceof load_spec)
84 o = load_spec_element(o);
85 if (o instanceof Ci.nsIDOMWindow) {
86 // scroll to #ref or top
87 var ref = o.document.documentURIObject.ref;
89 var xpr = I.buffer.document.evaluate(
90 "//*[@id='"+ref+"']|//*[@name='"+ref+"']",
91 o.document, xpath_lookup_namespace,
92 Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE, null);
93 var node = xpr.singleNodeValue;
95 var rect = node.getBoundingClientRect();
96 o.scrollTo(0, o.scrollY + rect.top);
101 } else if (o instanceof Ci.nsIDOMNode) {
103 I.window.minibuffer.message(o.textContent);
105 throw interactive_error("Cannot scroll to given item");
110 interactive("scroll",
111 "Generalized scroll command.\nThe amount of scrolling is determined by "+
112 "the object passed to the command as a browser-object. If the object "+
113 "is a DOM node, that node will be scrolled to the top of the viewport "+
116 $browser_object = null);