Bug 834209 - Return HTMLCollection from HTMLDocument named getter; r=bkelly

The spec says HTMLCollection, and this appears to be what all other
browsers do.

https://html.spec.whatwg.org/#dom-window-nameditem
https://html.spec.whatwg.org/#dom-document-nameditem

MozReview-Commit-ID: 87aABNaQmiz

--HG--
extra : rebase_source : dfc6e445fd00d718632464357727be378da41e6c
This commit is contained in:
Aryeh Gregor 2017-08-20 17:20:24 +03:00
Родитель e401b67d1c
Коммит b7375b6f60
5 изменённых файлов: 121 добавлений и 19 удалений

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

@ -196,6 +196,7 @@
#include "mozilla/dom/Comment.h" #include "mozilla/dom/Comment.h"
#include "nsTextNode.h" #include "nsTextNode.h"
#include "mozilla/dom/Link.h" #include "mozilla/dom/Link.h"
#include "mozilla/dom/HTMLCollectionBinding.h"
#include "mozilla/dom/HTMLElementBinding.h" #include "mozilla/dom/HTMLElementBinding.h"
#include "nsXULAppAPI.h" #include "nsXULAppAPI.h"
#include "mozilla/dom/Touch.h" #include "mozilla/dom/Touch.h"
@ -512,11 +513,130 @@ nsIdentifierMapEntry::SetImageElement(Element* aElement)
} }
} }
namespace mozilla {
namespace dom {
class SimpleHTMLCollection final : public nsSimpleContentList
, public nsIHTMLCollection
{
public:
explicit SimpleHTMLCollection(nsINode* aRoot) : nsSimpleContentList(aRoot) {}
NS_DECL_ISUPPORTS_INHERITED
// nsIDOMHTMLCollection
NS_DECL_NSIDOMHTMLCOLLECTION
virtual nsINode* GetParentObject() override
{
return nsSimpleContentList::GetParentObject();
}
virtual Element* GetElementAt(uint32_t aIndex) override
{
return mElements.SafeElementAt(aIndex)->AsElement();
}
virtual Element* GetFirstNamedElement(const nsAString& aName,
bool& aFound) override
{
aFound = false;
nsCOMPtr<nsIAtom> name = NS_Atomize(aName);
for (uint32_t i = 0; i < mElements.Length(); i++) {
MOZ_DIAGNOSTIC_ASSERT(mElements[i]);
Element* element = mElements[i]->AsElement();
if (element->GetID() == name ||
(element->HasName() &&
element->GetParsedAttr(nsGkAtoms::name)->GetAtomValue() == name)) {
aFound = true;
return element;
}
}
return nullptr;
}
virtual void GetSupportedNames(nsTArray<nsString>& aNames) override
{
AutoTArray<nsIAtom*, 8> atoms;
for (uint32_t i = 0; i < mElements.Length(); i++) {
MOZ_DIAGNOSTIC_ASSERT(mElements[i]);
Element* element = mElements[i]->AsElement();
nsIAtom* id = element->GetID();
MOZ_ASSERT(id != nsGkAtoms::_empty);
if (id && !atoms.Contains(id)) {
atoms.AppendElement(id);
}
if (element->HasName()) {
nsIAtom* name = element->GetParsedAttr(nsGkAtoms::name)->GetAtomValue();
MOZ_ASSERT(name && name != nsGkAtoms::_empty);
if (name && !atoms.Contains(name)) {
atoms.AppendElement(name);
}
}
}
nsString* names = aNames.AppendElements(atoms.Length());
for (uint32_t i = 0; i < atoms.Length(); i++) {
atoms[i]->ToString(names[i]);
}
}
virtual JSObject* GetWrapperPreserveColorInternal() override
{
return nsWrapperCache::GetWrapperPreserveColor();
}
virtual void PreserveWrapperInternal(nsISupports* aScriptObjectHolder) override
{
nsWrapperCache::PreserveWrapper(aScriptObjectHolder);
}
virtual JSObject* WrapObject(JSContext *aCx,
JS::Handle<JSObject*> aGivenProto) override
{
return HTMLCollectionBinding::Wrap(aCx, this, aGivenProto);
}
using nsBaseContentList::Length;
using nsBaseContentList::Item;
using nsIHTMLCollection::NamedItem;
private:
virtual ~SimpleHTMLCollection() {}
};
NS_IMPL_ISUPPORTS_INHERITED(SimpleHTMLCollection, nsSimpleContentList,
nsIHTMLCollection, nsIDOMHTMLCollection)
NS_IMETHODIMP
SimpleHTMLCollection::GetLength(uint32_t* aLength)
{
*aLength = Length();
return NS_OK;
}
NS_IMETHODIMP
SimpleHTMLCollection::Item(uint32_t aIdx, nsIDOMNode** aRetVal)
{
nsCOMPtr<nsIDOMNode> retVal = Item(aIdx)->AsDOMNode();
retVal.forget(aRetVal);
return NS_OK;
}
NS_IMETHODIMP
SimpleHTMLCollection::NamedItem(const nsAString& aName, nsIDOMNode** aRetVal)
{
nsCOMPtr<nsIDOMNode> retVal = NamedItem(aName)->AsDOMNode();
retVal.forget(aRetVal);
return NS_OK;
}
} // namespace dom
} // namespace mozilla
void void
nsIdentifierMapEntry::AddNameElement(nsINode* aNode, Element* aElement) nsIdentifierMapEntry::AddNameElement(nsINode* aNode, Element* aElement)
{ {
if (!mNameContentList) { if (!mNameContentList) {
mNameContentList = new nsSimpleContentList(aNode); mNameContentList = new SimpleHTMLCollection(aNode);
} }
mNameContentList->AppendElement(aElement); mNameContentList->AppendElement(aElement);

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

@ -1,5 +0,0 @@
[nameditem-04.html]
type: testharness
[If there are two forms, a collection should be returned. (name)]
expected: FAIL

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

@ -1,5 +0,0 @@
[nameditem-05.html]
type: testharness
[If there are two embeds, a collection should be returned. (name)]
expected: FAIL

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

@ -1,5 +0,0 @@
[nameditem-06.html]
type: testharness
[If there are two imgs, a collection should be returned. (name)]
expected: FAIL

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

@ -1,8 +1,5 @@
[nameditem-07.html] [nameditem-07.html]
type: testharness type: testharness
[If there are two objects, a collection should be returned. (name)]
expected: FAIL
[If there are two objects, a collection should be returned. (id)] [If there are two objects, a collection should be returned. (id)]
expected: FAIL expected: FAIL