1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-20 18:17:58 +01:00

Rename followDocumentRelationship.

This commit is contained in:
Kris Maglione
2010-10-12 04:22:39 -04:00
parent a3ce7ae169
commit 3c77559614
2 changed files with 82 additions and 55 deletions

View File

@@ -427,15 +427,18 @@ const Buffer = Module("buffer", {
}, },
/** /**
* Returns a list of all frames in the given current buffer. * Returns a list of all frames in the given window or current buffer.
*/ */
allFrames: function (win) { allFrames: function (win, focusedFirst) {
let frames = []; let frames = [];
(function rec(frame) { (function rec(frame) {
if (frame.document.body instanceof HTMLBodyElement) if (frame.document.body instanceof HTMLBodyElement)
frames.push(frame); frames.push(frame);
Array.forEach(frame.frames, rec); Array.forEach(frame.frames, rec);
})(win || window.content); })(win || window.content);
if (focusedFirst)
return frames.filter(function (f) f === buffer.focusedFrame).concat(
frames.filter(function (f) f !== buffer.focusedFrame))
return frames; return frames;
}, },
@@ -532,59 +535,66 @@ const Buffer = Module("buffer", {
}, },
/** /**
* Tries to guess links the like of "next" and "prev". Though it has a * Find the counth last link on a page matching one of the given
* singularly horrendous name, it turns out to be quite useful. * regular expressions, or with a @rel or @rev attribute matching
* the given relation. Each frame is searched beginning with the
* last link and progressing to the first, once checking for
* matching @rel or @rev properties, and then once for each given
* regular expression. The first match is returned. All frames of
* the page are searched, beginning with the currently focused.
* *
* @param {string} rel The relationship to look for. Looks for * If follow is true, the link is followed.
* links with matching @rel or @rev attributes, and, *
* failing that, looks for an option named rel + * @param {string} rel The relationship to look for.
* "pattern", and finds the last link matching that * @param {[RegExp]} regexps The regular expressions to search for.
* RegExp. * @param {number} count The nth matching link to follow.
* @param {bool} follow Whether to follow the matching link.
* @param {string} path The XPath to use for the search. @optional
*/ */
followDocumentRelationship: function (rel) { followDocumentRelationship: deprecated("Please use buffer.findLink instead",
let regexes = options[rel + "pattern"].map(function (re) RegExp(re, "i")); function followDocumentRelationship(rel) {
this.findLink(rel, options[rel + "pattern"], 0, true);
}),
findLink: function (rel, regexps, count, follow, path) {
path = path || options.get("hinttags").defaultValue;
function followFrame(frame) { function followFrame(frame) {
function iter(elems) { function iter(elems) {
for (let i = 0; i < elems.length; i++) for (let i = 0; i < elems.length; i++)
if (elems[i].rel.toLowerCase() == rel || elems[i].rev.toLowerCase() == rel) if (elems[i].rel.toLowerCase() === rel || elems[i].rev.toLowerCase() === rel)
yield elems[i]; yield elems[i];
} }
// <link>s have higher priority than normal <a> hrefs // <link>s have higher priority than normal <a> hrefs
let elems = frame.document.getElementsByTagName("link"); let elems = frame.document.getElementsByTagName("link");
for (let elem in iter(elems)) { for (let elem in iter(elems))
dactyl.open(elem.href); yield elem;
return true;
}
// no links? ok, look for hrefs // no links? ok, look for hrefs
elems = frame.document.getElementsByTagName("a"); elems = frame.document.getElementsByTagName("a");
for (let elem in iter(elems)) { for (let elem in iter(elems))
buffer.followLink(elem, dactyl.CURRENT_TAB); yield elem;
return true;
}
let res = util.evaluateXPath(options.get("hinttags").defaultValue, frame.document); let res = util.evaluateXPath(path, frame.document);
for (let [, regex] in Iterator(regexes)) { for (let regex in values(regexps)) {
for (let i in util.range(res.snapshotLength, 0, -1)) { for (let i in util.range(res.snapshotLength, 0, -1)) {
let elem = res.snapshotItem(i); let elem = res.snapshotItem(i);
if (regex.test(elem.textContent) || regex.test(elem.title) || if (regex.test(elem.textContent) === regex.result || regex.test(elem.title) === regex.result ||
Array.some(elem.childNodes, function (child) regex.test(child.alt))) { Array.some(elem.childNodes, function (child) regex.test(child.alt) === regex.result))
buffer.followLink(elem, dactyl.CURRENT_TAB); yield elem;
return true;
}
} }
} }
return false;
} }
let ret = followFrame(window.content); for (let frame in values(buffer.allFrames(null, true)))
if (!ret) for (let elem in followFrame(frame))
// only loop through frames if the main content didn't match if (count-- === 0) {
ret = Array.some(buffer.allFrames().frames, followFrame); if (follow)
buffer.followLink(elem, dactyl.CURRENT_TAB);
return elem;
}
if (!ret) if (follow)
dactyl.beep(); dactyl.beep();
}, },
@@ -601,11 +611,12 @@ const Buffer = Module("buffer", {
let offsetX = 1; let offsetX = 1;
let offsetY = 1; let offsetY = 1;
if (isinstance(elem, [HTMLFrameElement, HTMLIFrameElement])) { if (isinstance(elem, [HTMLFrameElement, HTMLIFrameElement]))
buffer.focusElement(elem); return buffer.focusElement(elem);
return; if (isinstance(elem, HTMLLinkElement))
} return dactyl.open(elem.href, where);
else if (elem instanceof HTMLAreaElement) { // for imagemap
if (elem instanceof HTMLAreaElement) { // for imagemap
let coords = elem.getAttribute("coords").split(","); let coords = elem.getAttribute("coords").split(",");
offsetX = Number(coords[0]) + 1; offsetX = Number(coords[0]) + 1;
offsetY = Number(coords[1]) + 1; offsetY = Number(coords[1]) + 1;
@@ -1619,12 +1630,16 @@ const Buffer = Module("buffer", {
mappings.add(myModes, ["]]"], mappings.add(myModes, ["]]"],
"Follow the link labeled 'next' or '>' if it exists", "Follow the link labeled 'next' or '>' if it exists",
function (count) { buffer.followDocumentRelationship("next"); }, function (count) {
buffer.findLink("next", options["nextpattern"], (count || 1) - 1, true);
},
{ count: true }); { count: true });
mappings.add(myModes, ["[["], mappings.add(myModes, ["[["],
"Follow the link labeled 'prev', 'previous' or '<' if it exists", "Follow the link labeled 'prev', 'previous' or '<' if it exists",
function (count) { buffer.followDocumentRelationship("previous"); }, function (count) {
buffer.findLink("previous", options["previouspattern"], (count || 1) - 1, true);
},
{ count: true }); { count: true });
mappings.add(myModes, ["gf"], mappings.add(myModes, ["gf"],
@@ -1643,11 +1658,10 @@ const Buffer = Module("buffer", {
if (count >= 1 || !elem || !Events.isContentNode(elem)) { if (count >= 1 || !elem || !Events.isContentNode(elem)) {
let xpath = ["input", "textarea[not(@disabled) and not(@readonly)]"]; let xpath = ["input", "textarea[not(@disabled) and not(@readonly)]"];
let frames = array([buffer.focusedFrame].concat( let frames = buffer.allFrames(null, true);
buffer.allFrames().filter(function (f) f != buffer.focusedFrame)));
let elements = frames.map(function (win) [m for (m in util.evaluateXPath(xpath, win.document))]) let elements = array.flatten(frames.map(function (win) [m for (m in util.evaluateXPath(xpath, win.document))]))
.flatten().filter(function (elem) { .filter(function (elem) {
if (elem.readOnly || elem instanceof HTMLInputElement && !set.has(util.editableInputs, elem.type)) if (elem.readOnly || elem instanceof HTMLInputElement && !set.has(util.editableInputs, elem.type))
return false; return false;
@@ -1771,11 +1785,13 @@ const Buffer = Module("buffer", {
options: function () { options: function () {
options.add(["nextpattern"], options.add(["nextpattern"],
"Patterns to use when guessing the 'next' page in a document sequence", "Patterns to use when guessing the 'next' page in a document sequence",
"stringlist", UTF8("'\\bnext\\b',^>$,^(>>|»)$,^(>|»),(>|»)$,'\\bmore\\b'")); "regexlist", UTF8("'\\bnext\\b',^>$,^(>>|»)$,^(>|»),(>|»)$,'\\bmore\\b'"),
{ regexFlags: "i" });
options.add(["previouspattern"], options.add(["previouspattern"],
"Patterns to use when guessing the 'previous' page in a document sequence", "Patterns to use when guessing the 'previous' page in a document sequence",
"stringlist", UTF8("'\\bprev|previous\\b',^<$,^(<<|«)$,^(<|«),(<|«)$")); "regexlist", UTF8("'\\bprev|previous\\b',^<$,^(<<|«)$,^(<|«),(<|«)$"),
{ regexFlags: "i" });
options.add(["pageinfo", "pa"], options.add(["pageinfo", "pa"],
"Desired info in the :pageinfo output", "Desired info in the :pageinfo output",

View File

@@ -53,15 +53,15 @@ const Option = Class("Option", {
this._op = Option.ops[this.type]; this._op = Option.ops[this.type];
if (extraInfo)
update(this, extraInfo);
if (arguments.length > 3) { if (arguments.length > 3) {
if (this.type == "string") if (this.type == "string")
defaultValue = Commands.quote(defaultValue); defaultValue = Commands.quote(defaultValue);
this.defaultValue = this.parse(defaultValue) this.defaultValue = this.parse(defaultValue)
} }
if (extraInfo)
update(this, extraInfo);
// add no{option} variant of boolean {option} to this.names // add no{option} variant of boolean {option} to this.names
if (this.type == "boolean") if (this.type == "boolean")
this.names = array([name, "no" + name] for (name in values(names))).flatten().array; this.names = array([name, "no" + name] for (name in values(names))).flatten().array;
@@ -372,11 +372,11 @@ const Option = Class("Option", {
*/ */
SCOPE_BOTH: 3, SCOPE_BOTH: 3,
parseRegex: function (value, result) { parseRegex: function (value, result, flags) {
let [, bang, val] = /^(!?)(.*)/.exec(value); let [, bang, val] = /^(!?)(.*)/.exec(value);
let re = RegExp(Option.dequote(val)); let re = RegExp(Option.dequote(val), flags);
re.bang = bang; re.bang = bang;
re.result = arguments.length == 2 ? result : !bang; re.result = result !== undefined ? result : !bang;
re.toString = function () Option.unparseRegex(this); re.toString = function () Option.unparseRegex(this);
return re; return re;
}, },
@@ -398,31 +398,42 @@ const Option = Class("Option", {
stringify: { stringify: {
charlist: function (vals) Commands.quote(vals.join("")), charlist: function (vals) Commands.quote(vals.join("")),
stringlist: function (vals) vals.map(Option.quote).join(","), stringlist: function (vals) vals.map(Option.quote).join(","),
stringmap: function (vals) [Option.quote(k, /:/) + ":" + Option.quote(v) for ([k, v] in Iterator(vals))].join(","), stringmap: function (vals) [Option.quote(k, /:/) + ":" + Option.quote(v) for ([k, v] in Iterator(vals))].join(","),
regexlist: function (vals) vals.join(","), regexlist: function (vals) vals.join(","),
get regexmap() this.regexlist get regexmap() this.regexlist
}, },
parse: { parse: {
number: function (value) Number(Option.dequote(value)), number: function (value) Number(Option.dequote(value)),
boolean: function (value) Option.dequote(value) == "true" || value == true ? true : false, boolean: function (value) Option.dequote(value) == "true" || value == true ? true : false,
charlist: function (value) Array.slice(Option.dequote(value)), charlist: function (value) Array.slice(Option.dequote(value)),
stringlist: function (value) (value === "") ? [] : Option.splitList(value), stringlist: function (value) (value === "") ? [] : Option.splitList(value),
regexlist: function (value) (value === "") ? [] : Option.splitList(value, true).map(Option.parseRegex),
regexlist: function (value) (value === "") ? [] :
Option.splitList(value, true)
.map(function (re) Option.parseRegex(re, undefined, this.regexFlags), this),
stringmap: function (value) array.toObject( stringmap: function (value) array.toObject(
Option.splitList(value, true).map(function (v) { Option.splitList(value, true).map(function (v) {
let [count, key, quote] = Commands.parseArg(v, /:/); let [count, key, quote] = Commands.parseArg(v, /:/);
return [key, Option.dequote(v.substr(count + 1))] return [key, Option.dequote(v.substr(count + 1))]
})), })),
regexmap: function (value) regexmap: function (value)
Option.splitList(value, true).map(function (v) { Option.splitList(value, true).map(function (v) {
let [count, re, quote] = Commands.parseArg(v, /:/, true); let [count, re, quote] = Commands.parseArg(v, /:/, true);
v = Option.dequote(v.substr(count + 1)); v = Option.dequote(v.substr(count + 1));
if (count === v.length) if (count === v.length)
[v, re] = [re, ".?"]; [v, re] = [re, ".?"];
return Option.parseRegex(re, v); return Option.parseRegex(re, v, this.regexFlags);
}) }, this)
}, },
testValues: { testValues: {