Bug 1381710 - Selection.addRange will be failure with dynamic table insertion. r=tnikkel

When range is selected table element, Selection.addRange uses nsFrameSelection.  If frame isn't constructed yet, addRange throws NS_ERROR_FAILURE even if table element isn't editable element.

When getting nsITableCellLayout, we should flush frame to construct cell frame.

MozReview-Commit-ID: 9qWwW46RYNL

--HG--
extra : rebase_source : 708e78af457a28bc273b83015f78950a5bee232e
This commit is contained in:
Makoto Kato 2017-07-18 16:54:31 +09:00
Родитель ac234bd18b
Коммит 4418d7d986
3 изменённых файлов: 57 добавлений и 3 удалений

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

@ -605,14 +605,24 @@ Selection::GetTableCellLocationFromRange(nsRange* aRange,
if (!content)
return NS_ERROR_FAILURE;
nsIContent *child = content->GetChildAt(aRange->StartOffset());
nsCOMPtr<nsIContent> child = content->GetChildAt(aRange->StartOffset());
if (!child)
return NS_ERROR_FAILURE;
// GetCellLayout depends on current frame, we need flush frame to get
// nsITableCellLayout
nsCOMPtr<nsIPresShell> presShell = mFrameSelection->GetShell();
if (presShell) {
presShell->FlushPendingNotifications(FlushType::Frames);
// Since calling FlushPendingNotifications, so check whether disconnected.
if (!mFrameSelection || !mFrameSelection->GetShell()) {
return NS_ERROR_FAILURE;
}
}
//Note: This is a non-ref-counted pointer to the frame
nsITableCellLayout *cellLayout = mFrameSelection->GetCellLayout(child);
if (NS_FAILED(result))
return result;
if (!cellLayout)
return NS_ERROR_FAILURE;
@ -2276,6 +2286,9 @@ Selection::AddRangeInternal(nsRange& aRange, nsIDocument* aDocument,
return;
}
// AddTableCellRange might flush frame.
RefPtr<Selection> kungFuDeathGrip(this);
// This inserts a table cell range in proper document order
// and returns NS_OK if range doesn't contain just one table cell
bool didAddRange;

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

@ -621,6 +621,7 @@ skip-if = toolkit == 'android'
[test_bug1314032.html]
[test_bug1318303.html]
[test_bug1375050.html]
[test_bug1381710.html]
[test_bug1384658.html]
skip-if = toolkit == 'android'
[test_caretPositionFromPoint.html]

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

@ -0,0 +1,40 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1381710
-->
<head>
<title>Test for Mozilla Bug 1381710</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.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=1381710">Mozilla Bug 1381710</a>
<div id="content">
<div id="nonedit"></div>
<div id="edit" contenteditable=true></div>
</div>
<pre id="test">
<script type="application/javascript">
function test(element)
{
let selection = window.getSelection();
selection.removeAllRanges();
let range = document.createRange();
element.innerHTML = "<table><tr id=tr><td id=td>A</td><td>B</td><tr></table>";
let td = document.getElementById("td");
range.selectNode(td);
// Don't throw exception
selection.addRange(range);
is(selection.anchorNode, document.getElementById("tr"),
"anchor node should be <TR> element");
element.innerHTML = "";
}
test(document.getElementById("nonedit"));
test(document.getElementById("edit"));
</script>
</pre>
</body>
</html>