Bug 483651 - Trailing <br> node not removed when it should be; r=ehsan

This commit is contained in:
Graeme McCutcheon 2011-08-12 15:53:10 -04:00
Родитель f6f2db44cb
Коммит f14955fe44
4 изменённых файлов: 162 добавлений и 1 удалений

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

@ -221,11 +221,16 @@ nsTextEditRules::AfterEdit(PRInt32 action, nsIEditor::EDirection aDirection)
nsnull, 0, nsnull, 0);
NS_ENSURE_SUCCESS(res, res);
// if only trailing <br> remaining remove it
res = RemoveRedundantTrailingBR();
if (NS_FAILED(res))
return res;
// detect empty doc
res = CreateBogusNodeIfNeeded(selection);
NS_ENSURE_SUCCESS(res, res);
// insure trailing br node
// ensure trailing br node
res = CreateTrailingBRIfNeeded();
NS_ENSURE_SUCCESS(res, res);
@ -1045,6 +1050,66 @@ nsTextEditRules::DidOutputText(nsISelection *aSelection, nsresult aResult)
return NS_OK;
}
nsresult
nsTextEditRules::RemoveRedundantTrailingBR()
{
// If the bogus node exists, we have no work to do
if (mBogusNode)
return NS_OK;
// Likewise, nothing to be done if we could never have inserted a trailing br
if (IsSingleLineEditor())
return NS_OK;
nsIDOMNode* body = mEditor->GetRoot();
if (!body)
return NS_ERROR_NULL_POINTER;
PRBool hasChildren;
nsresult res = body->HasChildNodes(&hasChildren);
NS_ENSURE_SUCCESS(res, res);
if (hasChildren) {
nsCOMPtr<nsIDOMNodeList> childList;
res = body->GetChildNodes(getter_AddRefs(childList));
NS_ENSURE_SUCCESS(res, res);
if (!childList)
return NS_ERROR_NULL_POINTER;
PRUint32 childCount;
res = childList->GetLength(&childCount);
NS_ENSURE_SUCCESS(res, res);
// The trailing br is redundant if it is the only remaining child node
if (childCount != 1)
return NS_OK;
nsCOMPtr<nsIDOMNode> child;
res = body->GetFirstChild(getter_AddRefs(child));
NS_ENSURE_SUCCESS(res, res);
if (nsTextEditUtils::IsMozBR(child)) {
// Rather than deleting this node from the DOM tree we should instead
// morph this br into the bogus node
nsCOMPtr<nsIDOMElement> elem = do_QueryInterface(child);
if (elem) {
elem->RemoveAttribute(NS_LITERAL_STRING("type"));
NS_ENSURE_SUCCESS(res, res);
// set mBogusNode to be this <br>
mBogusNode = elem;
// give it the bogus node attribute
nsCOMPtr<nsIContent> content = do_QueryInterface(elem);
content->SetAttr(kNameSpaceID_None, kMOZEditorBogusNodeAttrAtom,
kMOZEditorBogusNodeValue, PR_FALSE);
}
}
}
return NS_OK;
}
nsresult
nsTextEditRules::CreateTrailingBRIfNeeded()
{

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

@ -205,6 +205,9 @@ protected:
// helper functions
/** check for and replace a redundant trailing break */
nsresult RemoveRedundantTrailingBR();
/** creates a trailing break in the text doc if there is not one already */
nsresult CreateTrailingBRIfNeeded();

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

@ -73,6 +73,7 @@ _TEST_FILES += \
endif
_CHROME_TEST_FILES = \
test_bug483651.html \
test_bug636465.xul \
$(NULL)

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

@ -0,0 +1,92 @@
<!DOCTYPE HTML>
<!-- ***** BEGIN LICENSE BLOCK *****
- Version: MPL 1.1/GPL 2.0/LGPL 2.1
-
- The contents of this file are subject to the Mozilla Public License Version
- 1.1 (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.mozilla.org/MPL/
-
- Software distributed under the License is distributed on an "AS IS" basis,
- WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- for the specific language governing rights and limitations under the
- License.
-
- The Original Code is Plaintext Editor Test code
-
- The Initial Developer of the Original Code is
- Graeme McCutcheon <graememcc_firefox@graeme-online.co.uk>.
- Portions created by the Initial Developer are Copyright (C) 2009
- the Initial Developer. All Rights Reserved.
-
- Contributor(s):
-
-
- Alternatively, the contents of this file may be used under the terms of
- either the GNU General Public License Version 2 or later (the "GPL"), or
- the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
- in which case the provisions of the GPL or the LGPL are applicable instead
- of those above. If you wish to allow use of your version of this file only
- under the terms of either the GPL or the LGPL, and not to allow others to
- use your version of this file under the terms of the MPL, indicate your
- decision by deleting the provisions above and replace them with the notice
- and other provisions required by the GPL or the LGPL. If you do not delete
- the provisions above, a recipient may use your version of this file under
- the terms of any one of the MPL, the GPL or the LGPL.
-
- ***** END LICENSE BLOCK ***** -->
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=483651
-->
<head>
<title>Test for Bug 483651</title>
<script type="application/javascript"
src="chrome://mochikit/content/MochiKit/packed.js"></script>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
<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 onload="doTest();">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=483651">Mozilla Bug 483651</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 483651 **/
SimpleTest.waitForExplicitFinish();
function doTest() {
var t1 = $("t1");
var editor = null;
if (t1 instanceof Components.interfaces.nsIDOMNSEditableElement)
editor = t1.editor;
ok(editor, "able to get editor for the element");
t1.focus();
synthesizeKey("A", {});
synthesizeKey("VK_BACK_SPACE", {});
try {
// Was the trailing br removed?
is(editor.documentIsEmpty, true, "trailing <br> correctly removed");
} catch (e) {
ok(false, "test failed with error "+e);
}
SimpleTest.finish();
}
</script>
</pre>
<textarea id="t1" rows="2" columns="80"></textarea>
</body>
</html>