Bug 1059163 - Add a mutation observer to contenteditable elements to detect selection changes that nsISelectionPrivate misses. r=yxl

This commit is contained in:
Jan Jongboom
2014-10-09 06:06:00 -04:00
parent 201732f117
commit 9c7aee6f4f
4 changed files with 120 additions and 6 deletions

View File

@@ -224,7 +224,8 @@ let FormAssistant = {
scrollIntoViewTimeout: null,
_focusedElement: null,
_focusCounter: 0, // up one for every time we focus a new element
_observer: null,
_focusDeleteObserver: null,
_focusContentObserver: null,
_documentEncoder: null,
_editor: null,
_editing: false,
@@ -250,9 +251,13 @@ let FormAssistant = {
if (this.focusedElement) {
this.focusedElement.removeEventListener('compositionend', this);
if (this._observer) {
this._observer.disconnect();
this._observer = null;
if (this._focusDeleteObserver) {
this._focusDeleteObserver.disconnect();
this._focusDeleteObserver = null;
}
if (this._focusContentObserver) {
this._focusContentObserver.disconnect();
this._focusContentObserver = null;
}
if (this._selectionPrivate) {
this._selectionPrivate.removeSelectionListener(this);
@@ -292,7 +297,7 @@ let FormAssistant = {
// If our focusedElement is removed from DOM we want to handle it properly
let MutationObserver = element.ownerDocument.defaultView.MutationObserver;
this._observer = new MutationObserver(function(mutations) {
this._focusDeleteObserver = new MutationObserver(function(mutations) {
var del = [].some.call(mutations, function(m) {
return [].some.call(m.removedNodes, function(n) {
return n.contains(element);
@@ -305,10 +310,23 @@ let FormAssistant = {
}
});
this._observer.observe(element.ownerDocument.body, {
this._focusDeleteObserver.observe(element.ownerDocument.body, {
childList: true,
subtree: true
});
// If contenteditable, also add a mutation observer on its content and
// call selectionChanged when a change occurs
if (isContentEditable(element)) {
this._focusContentObserver = new MutationObserver(function() {
this.updateSelection();
}.bind(this));
this._focusContentObserver.observe(element, {
childList: true,
subtree: true
});
}
}
this.focusedElement = element;