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
Родитель 83c51d6502
Коммит c8c3ab24b8
4 изменённых файлов: 120 добавлений и 6 удалений

Просмотреть файл

@ -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;

Просмотреть файл

@ -0,0 +1,17 @@
<!DOCTYPE HTML>
<html>
<body>
<div id="text" contenteditable>Jan Jongboom</div>
<script type="application/javascript;version=1.7">
var t = document.querySelector('#text');
t.focus();
var range = document.createRange();
range.selectNodeContents(t);
range.collapse(false);
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
</script>
</body>
</html>

Просмотреть файл

@ -6,6 +6,7 @@ support-files =
file_inputmethod.html
file_inputmethod_1043828.html
file_test_app.html
file_test_contenteditable.html
file_test_sendkey_cancel.html
file_test_sms_app.html
file_test_sms_app_1066515.html
@ -18,6 +19,7 @@ support-files =
[test_bug978918.html]
[test_bug1026997.html]
[test_bug1043828.html]
[test_bug1059163.html]
[test_bug1066515.html]
[test_delete_focused_element.html]
[test_sendkey_cancel.html]

Просмотреть файл

@ -0,0 +1,77 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1059163
-->
<head>
<title>Basic test for repeat sendKey events</title>
<script type="application/javascript;version=1.7" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript;version=1.7" src="inputmethod_common.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1059163">Mozilla Bug 1059163</a>
<p id="display"></p>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">
inputmethod_setup(function() {
runTest();
});
// The frame script running in the file
function appFrameScript() {
addMessageListener('test:InputMethod:clear', function() {
var t = content.document.getElementById('text');
t.innerHTML = '';
});
}
function runTest() {
let im = navigator.mozInputMethod;
// Set current page as an input method.
SpecialPowers.wrap(im).setActive(true);
// Create an app frame to recieve keyboard inputs.
let app = document.createElement('iframe');
app.src = 'file_test_contenteditable.html';
app.setAttribute('mozbrowser', true);
document.body.appendChild(app);
app.addEventListener('mozbrowserloadend', function() {
let mm = SpecialPowers.getBrowserFrameMessageManager(app);
function register() {
im.inputcontext.onselectionchange = function() {
im.inputcontext.onselectionchange = null;
is(im.inputcontext.textBeforeCursor, '', 'textBeforeCursor');
is(im.inputcontext.textBeforeCursor, '', 'textAfterCursor');
is(im.inputcontext.selectionStart, 0, 'selectionStart');
is(im.inputcontext.selectionEnd, 0, 'selectionEnd');
inputmethod_cleanup();
};
mm.sendAsyncMessage('test:InputMethod:clear');
}
if (im.inputcontext) {
register();
}
else {
im.oninputcontextchange = function() {
if (im.inputcontext) {
im.oninputcontextchange = null;
register();
}
};
}
mm.loadFrameScript('data:,(' + appFrameScript.toString() + ')();', false);
});
}
</script>
</pre>
</body>
</html>