Bug 573258 - Support a.text as defined in HTML5; r=Olli.Pettay approval2.0=benjamin

This commit is contained in:
Ms2ger 2010-07-30 16:48:57 -07:00
parent 9cc2e9ec05
commit dc206bff4c
4 changed files with 63 additions and 37 deletions

View File

@ -50,17 +50,11 @@
#include "nsPresContext.h"
#include "nsIEventStateManager.h"
// For GetText().
#include "nsIContentIterator.h"
#include "nsIDOMText.h"
#include "nsHTMLDNSPrefetch.h"
#include "Link.h"
using namespace mozilla::dom;
nsresult NS_NewPreContentIterator(nsIContentIterator** aInstancePtrResult);
class nsHTMLAnchorElement : public nsGenericHTMLElement,
public nsIDOMHTMLAnchorElement,
public nsIDOMNSHTMLAnchorElement2,
@ -68,6 +62,9 @@ class nsHTMLAnchorElement : public nsGenericHTMLElement,
public Link
{
public:
using nsGenericElement::GetText;
using nsGenericElement::SetText;
nsHTMLAnchorElement(already_AddRefed<nsINodeInfo> aNodeInfo);
virtual ~nsHTMLAnchorElement();
@ -369,38 +366,16 @@ IMPL_URI_PART(Hash)
NS_IMETHODIMP
nsHTMLAnchorElement::GetText(nsAString& aText)
{
aText.Truncate();
// Since this is a Netscape 4 proprietary attribute, we have to implement
// the same behavior. Basically it is returning the last text node of
// of the anchor. Returns an empty string if there is no text node.
// The nsIContentIterator does exactly what we want, if we start the
// iteration from the end.
nsCOMPtr<nsIContentIterator> iter;
nsresult rv = NS_NewPreContentIterator(getter_AddRefs(iter));
NS_ENSURE_SUCCESS(rv, rv);
// Initialize the content iterator with the children of the anchor
iter->Init(this);
// Last() positions the iterator to the last child of the anchor,
// starting at the deepest level of children, just like NS4 does.
iter->Last();
while (!iter->IsDone()) {
nsCOMPtr<nsIDOMText> textNode(do_QueryInterface(iter->GetCurrentNode()));
if(textNode) {
// The current node is a text node. Get its value and break the loop.
textNode->GetData(aText);
break;
}
iter->Prev();
}
nsContentUtils::GetNodeTextContent(this, PR_TRUE, aText);
return NS_OK;
}
NS_IMETHODIMP
nsHTMLAnchorElement::SetText(const nsAString& aText)
{
return nsContentUtils::SetNodeTextContent(this, aText, PR_FALSE);
}
NS_IMETHODIMP
nsHTMLAnchorElement::ToString(nsAString& aSource)
{

View File

@ -144,6 +144,7 @@ _TEST_FILES = test_bug589.html \
test_bug347174_xslp.html \
347174transformable.xml \
347174transform.xsl \
test_a_text.html \
test_anchor_href_cache_invalidation.html \
test_bug481335.xhtml \
test_bug500885.html \

View File

@ -0,0 +1,45 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for a.text</title>
<script src="/MochiKit/packed.js"></script>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
<link rel="help" href="http://www.whatwg.org/html/#dom-a-text"/>
</head>
<body>
<div id="content">
<a href="a">a b c</a>
<a href="b">a <!--b--> c</a>
<a href="c">a <b>b</b> c</a>
</div>
<pre id="test">
<script>
var d = document.getElementById("content")
.appendChild(document.createElement("a"));
d.href = "d";
d.appendChild(document.createTextNode("a "));
d.appendChild(document.createTextNode("b "));
d.appendChild(document.createTextNode("c "));
var expected = ["a b c", "a c", "a b c", "a b c "];
var list = document.getElementById("content").getElementsByTagName("a");
for (var i = 0, il = list.length; i < il; ++i) {
is(list[i].text, list[i].textContent);
is(list[i].text, expected[i]);
list[i].text = "x";
is(list[i].text, "x");
is(list[i].textContent, "x");
is(list[i].firstChild.data, "x");
is(list[i].childNodes.length, 1);
list[i].textContent = "y";
is(list[i].text, "y");
is(list[i].textContent, "y");
is(list[i].firstChild.data, "y");
is(list[i].childNodes.length, 1);
}
</script>
</pre>
</body>
</html>

View File

@ -39,7 +39,7 @@
#include "domstubs.idl"
[scriptable, uuid(a6cf911c-15b3-11d2-932e-00805f8add32)]
[scriptable, uuid(24c39afa-44f7-4cd4-9e63-0504a581a081)]
interface nsIDOMNSHTMLAnchorElement : nsISupports
{
attribute DOMString protocol;
@ -49,7 +49,12 @@ interface nsIDOMNSHTMLAnchorElement : nsISupports
attribute DOMString search;
attribute DOMString port;
attribute DOMString hash;
readonly attribute DOMString text;
/**
* An alias for the textContent attribute.
*/
[Null(Stringify)]
attribute DOMString text;
DOMString toString();
};