1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-21 09:08:10 +01:00

Fix buffer.(getCurrentWord|followDocumentRelationship). Thanks anekos and dhahler.

This commit is contained in:
Kris Maglione
2010-05-07 02:27:42 +09:00
parent 620e6a73a5
commit 0c6a7b178f
2 changed files with 41 additions and 16 deletions

View File

@@ -320,6 +320,19 @@ const Buffer = Module("buffer", {
); );
}, },
/**
* @property {Array[Window]} All frames in the current buffer.
*/
get allFrames() {
let frames = [];
(function (frame) {
if (frame.document.body instanceof HTMLBodyElement)
frames.push(frame);
Array.forEach(frame.frames, arguments.callee);
})(window.content);
return frames;
},
/** /**
* @property {Object} A map of page info sections to their * @property {Object} A map of page info sections to their
* content generating functions. * content generating functions.
@@ -446,18 +459,23 @@ const Buffer = Module("buffer", {
// FIXME: getSelection() doesn't always preserve line endings, see: // FIXME: getSelection() doesn't always preserve line endings, see:
// https://www.mozdev.org/bugs/show_bug.cgi?id=19303 // https://www.mozdev.org/bugs/show_bug.cgi?id=19303
getCurrentWord: function () { getCurrentWord: function () {
let selection = window.content.getSelection(); let win = tabs.localStore.focusedFrame || content;
let selection = win.getSelection();
if (selection.rangeCount == 0)
return "";
let range = selection.getRangeAt(0); let range = selection.getRangeAt(0);
if (selection.isCollapsed) { if (selection.isCollapsed) {
let selController = this.selectionController; let controller = util.selectionController(win);
let caretmode = selController.getCaretEnabled(); let caretmode = controller.getCaretEnabled();
selController.setCaretEnabled(true); controller.setCaretEnabled(true);
// Only move backwards if the previous character is not a space. // Only move backwards if the previous character is not a space.
if (range.startOffset > 0 && !/\s/.test(range.startContainer.textContent[range.startOffset - 1])) if (range.startOffset > 0 && !/\s/.test(range.startContainer.textContent[range.startOffset - 1]))
selController.wordMove(false, false); controller.wordMove(false, false);
selController.wordMove(true, true); controller.wordMove(true, true);
selController.setCaretEnabled(caretmode); controller.setCaretEnabled(caretmode);
return String.match(selection, /\w*/)[0]; return String.match(selection, /\w*/)[0];
} }
if (util.computedStyle(range.startContainer).whiteSpace == "pre" if (util.computedStyle(range.startContainer).whiteSpace == "pre"
@@ -563,7 +581,7 @@ const Buffer = Module("buffer", {
let ret = followFrame(window.content); let ret = followFrame(window.content);
if (!ret) if (!ret)
// only loop through frames if the main content didn't match // only loop through frames if the main content didn't match
ret = Array.some(window.content.frames, followFrame); ret = Array.some(buffer.allFrames.frames, followFrame);
if (!ret) if (!ret)
dactyl.beep(); dactyl.beep();
@@ -787,14 +805,7 @@ const Buffer = Module("buffer", {
return; return;
count = Math.max(count, 1); count = Math.max(count, 1);
let frames = []; let frames = buffer.allFrames;
// find all frames - depth-first search
(function (frame) {
if (frame.document.body instanceof HTMLBodyElement)
frames.push(frame);
Array.forEach(frame.frames, arguments.callee);
})(window.content);
if (frames.length == 0) // currently top is always included if (frames.length == 0) // currently top is always included
return; return;

View File

@@ -233,6 +233,20 @@ const Util = Module("util", {
}); });
}, },
/**
* Returns the selection controller for the given window.
*
* @param {Window} window
* @returns {nsISelectionController}
*/
selectionController: function (win)
win.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShell)
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsISelectionDisplay)
.QueryInterface(Ci.nsISelectionController),
/** /**
* Split a string on literal occurrences of a marker. * Split a string on literal occurrences of a marker.
* *