Files
palemoon27/dom/base/test/test_sendQueryContentAndSelectionSetEvent.html
T
roytam1 3b6bacfa0e import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1189396 part.1 Implement IMENotification::SelectionChangeData::Clear() to initialize its members r=smaug (8996dcd6d7)
- Bug 1189396 part.2 Implement IMENotification::SelectionChangeData::Assign() to copy its members r=smaug (b32b3a123b)
- Bug 1189396 part.3 Make IMENotification::SelectionChangeData useful even outside of IMENotification r=smaug (f55f2cf6b6)
- Bug 1189396 part.4 IMEContentObserver should cache the selection data at notifying IME of selection change r=smaug (dc9c7abb7a)
- Bug 1189396 part.5 IMEContentObserver should cache selection at gets focus and every selection change r=smaug (33c7d5ceac)
- Bug 1189396 part.6 IMEContentObserver shouldn't notify IME of selection change when the range isn't actually changed r=smaug (d1f0fc3735)
- Bug 1200980 part.1 Log the behavior of IMEContentObserver for debugging r=smaug (5d09bb52df)
- Bug 1169917 ContentEventHandler::OnQueryCaretRect() should try to find text frame as far as possible and GetStartFrameAndOffset() shouldn't assert even if there is no textframe r=smaug (868f313f3c)
- Bug 1171858 ContentEventHandler::OnQueryCaretRect() should honor font height and writing mode when it guesses caret rect r=smaug (8ac31e5c46)
- Bug 1179081 ContentEventHandler should use the primary frame of mRootContent when it retrieves focused widget but there is no caret frame r=smaug (f51ad819f5)
- Bug 1179082 ContentEventHandler::OnQueryCaretRect() should guess its result when the query offset is same as the offset of collappsed selection but there is no caret frame r=smaug (b4a0657a5d)
- Bug 1179093 Make ContentEventHandler::mSelection as nsRefPtr<Selection> r=smaug (a0906f81b1)
- Bug 1200980 part.2 QueryContentEvent should be handled via IMEContentObserver if there is an instance of it r=smaug (7bd6cda83b)
- Bug 1200980 part.3 IMEContentObserver should use its selection cache at handling NS_QUERY_SELECTED_TEXT r=smaug (d1a6649315)
- Bug 1200980 part.4 nsPlaintextEditor should notify editor observers of the end of edit action when NS_COMPOSITION_CHANGE isn't followed by NS_COMPOSITION_END r=smaug (7c067e2b1c)
- Bug 1200980 part.5 Fix window_composition_text_querycontent.xul for the new input event behavior r=smaug (500429c7a8)
- Bug 1179090 ContentEventHandler should assume that there is selection at beginning of the document when there is no selection range r=smaug (0d3501404c)
- Bug 1179086 ContentEventHandler should use /n on Mac rather than /r r=smaug+smichaud+josh (b151400415)
- Bug 1180240 ContentEventHandler::OnQuerySelectedText() should refer mFirstSelectedRange if mSelection doesn't have selection ranges actually r=smaug (416b6eb102)
2022-05-03 11:19:32 +08:00

225 lines
11 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<title>Test for nsIDOMWindowUtils.sendQueryContentEvent() and .sendSelectionSetEvent()</title>
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<div id="editor" contenteditable>abc<br>def</div>
<pre id="test">
<script type="application/javascript">
var editor = document.getElementById("editor");
SimpleTest.waitForExplicitFinish();
const kIsWin = navigator.platform.indexOf("Win") == 0;
const kIsMac = navigator.platform.indexOf("Mac") == 0;
const kLineBreak = kIsWin ? "\r\n" : "\n";
var gUtils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindowUtils);
function escape(aStr)
{
var result = aStr.replace("\n", "\\n");
return result.replace("\r", "\\r");
}
function runTests()
{
editor.focus();
// NOTE: For compatibility, calling without flags should work as with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK.
// QueryTextContent
var expectedStr = escape(("abc" + kLineBreak + "def").substr(2, 4));
var result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_CONTENT, 2, 4, 0, 0,
gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
ok(result.succeeded,
"sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
is(escape(result.text), expectedStr,
"sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) got unexpected string");
result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_CONTENT, 2, 4, 0, 0);
ok(result.succeeded,
"sendQueryContentEvent(QUERY_TEXT_CONTENT) should succeed");
is(escape(result.text), expectedStr,
"sendQueryContentEvent(QUERY_TEXT_CONTENT) should return same string as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
expectedStr = escape(("abc\ndef").substr(2, 4));
result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_CONTENT, 2, 4, 0, 0,
gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
ok(result.succeeded,
"sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed");
is(escape(result.text), expectedStr,
"sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) got unexpected string");
// QueryCaretRect
window.getSelection().collapse(editor.firstChild, 0);
var caretNative = gUtils.sendQueryContentEvent(gUtils.QUERY_CARET_RECT, 6, 0, 0, 0,
gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
ok(caretNative.succeeded,
"sendQueryContentEvent(QUERY_CARET_RECT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
var caretXP = gUtils.sendQueryContentEvent(gUtils.QUERY_CARET_RECT, 6 - kLineBreak.length + 1, 0, 0, 0,
gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
ok(caretXP.succeeded,
"sendQueryContentEvent(QUERY_CARET_RECT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed");
is(caretXP.top, caretNative.top,
"The caret top should be same");
is(caretXP.left, caretNative.left,
"The caret left should be same");
result = gUtils.sendQueryContentEvent(gUtils.QUERY_CARET_RECT, 6, 0, 0, 0);
ok(result.succeeded,
"sendQueryContentEvent(QUERY_CARET_RECT) should succeed");
is(result.top, caretNative.top,
"sendQueryContentEvent(QUERY_CARET_RECT) should return same top as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
is(result.left, caretNative.left,
"sendQueryContentEvent(QUERY_CARET_RECT) should return same left as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
// QueryTextRect
var textRectNative = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_RECT, 6, 1, 0, 0,
gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
ok(textRectNative.succeeded,
"sendQueryContentEvent(QUERY_TEXT_RECT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
var textRectXP = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_RECT, 6 - kLineBreak.length + 1, 1, 0, 0,
gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
ok(textRectXP.succeeded,
"sendQueryContentEvent(QUERY_TEXT_RECT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed");
is(textRectXP.top, textRectNative.top,
"The text top should be same");
is(textRectXP.left, textRectNative.left,
"The text left should be same");
is(textRectXP.height, textRectNative.height,
"The text height should be same");
is(textRectXP.width, textRectNative.width,
"The text width should be same");
result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_RECT, 6, 1, 0, 0);
ok(result.succeeded,
"sendQueryContentEvent(QUERY_TEXT_RECT) should succeed");
is(result.top, textRectNative.top,
"sendQueryContentEvent(QUERY_TEXT_RECT) should return same top as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
is(result.left, textRectNative.left,
"sendQueryContentEvent(QUERY_TEXT_RECT) should return same left as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
is(result.height, textRectNative.height,
"sendQueryContentEvent(QUERY_TEXT_RECT) should return same height as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
is(result.width, textRectNative.width,
"sendQueryContentEvent(QUERY_TEXT_RECT) should return same width as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
// QueryCharacterAtOffset
result = gUtils.sendQueryContentEvent(gUtils.QUERY_CHARACTER_AT_POINT, 0, 0, textRectNative.left + 1, textRectNative.top + 1,
gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
ok(result.succeeded,
"sendQueryContentEvent(QUERY_CHARACTER_AT_POINT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
is(result.top, textRectNative.top,
"The character top is wrong");
is(result.left, textRectNative.left,
"The character left is wrong");
is(result.height, textRectNative.height,
"The character height is wrong");
is(result.width, textRectNative.width,
"The character width is wrong");
is(result.offset, 6,
"The character offset is wrong");
result = gUtils.sendQueryContentEvent(gUtils.QUERY_CHARACTER_AT_POINT, 0, 0, textRectNative.left + 1, textRectNative.top + 1);
ok(result.succeeded,
"sendQueryContentEvent(QUERY_CHARACTER_AT_POINT) should succeed");
is(result.top, textRectNative.top,
"The character top should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
is(result.left, textRectNative.left,
"The character left should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
is(result.height, textRectNative.height,
"The character height should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
is(result.width, textRectNative.width,
"The character width should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
is(result.offset, 6,
"The character offset should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK");
result = gUtils.sendQueryContentEvent(gUtils.QUERY_CHARACTER_AT_POINT, 0, 0, textRectXP.left + 1, textRectXP.top + 1,
gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
ok(result.succeeded,
"sendQueryContentEvent(QUERY_CHARACTER_AT_POINT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed");
is(result.top, textRectXP.top,
"The character top is wrong");
is(result.left, textRectXP.left,
"The character left is wrong");
is(result.height, textRectXP.height,
"The character height is wrong");
is(result.width, textRectXP.width,
"The character width is wrong");
is(result.offset, 6 - kLineBreak.length + 1,
"The character offset is wrong");
// SelectionSet and QuerySelectedText
var selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK);
ok(selectionSet,
"sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
expectedStr = escape(("abc" + kLineBreak + "def").substr(0, 6));
result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0,
gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
ok(result.succeeded,
"sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
ok(!result.reversed,
"sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK) should set non-reversed selection");
is(escape(result.text), expectedStr,
"sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) got unexpected string");
selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_XP_LINE_BREAK);
ok(selectionSet,
"sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK) should succeed");
expectedStr = escape(("abc\ndef").substr(0, 6));
result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0,
gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
ok(result.succeeded,
"sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed");
ok(!result.reversed,
"sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK) should set non-reversed selection");
is(escape(result.text), expectedStr,
"sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) got unexpected string");
var selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK | gUtils.SELECTION_SET_FLAG_REVERSE);
ok(selectionSet,
"sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0,
gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK);
ok(result.succeeded,
"sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed");
ok(result.reversed,
"sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK | SELECTION_SET_FLAG_REVERSE) should set reversed selection");
selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_XP_LINE_BREAK | gUtils.SELECTION_SET_FLAG_REVERSE);
ok(selectionSet,
"sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK | SELECTION_SET_FLAG_REVERSE) should succeed");
result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0,
gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
ok(result.succeeded,
"sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed");
ok(result.reversed,
"sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK | SELECTION_SET_FLAG_REVERSE) should set reversed selection");
SimpleTest.finish();
}
SimpleTest.waitForFocus(runTests);
</script>
</pre>
</body>
</html>