Bug 874212 - Move document.all to WebIDL; r=bz

The static jsids in nsDOMClassInfo are newly unused, except for sEnumerate_id,
which was unused before.

This also removes the test_nondomexception.html test, which relied on an
obscure detail of the exception being thrown from document.all; I haven't
found another API which does something equally silly.
This commit is contained in:
Ms2ger 2014-05-20 21:52:21 +02:00
parent 10a293d8cb
commit 729320d026
12 changed files with 106 additions and 328 deletions

View File

@ -6,43 +6,10 @@
#include "mozilla/dom/HTMLAllCollection.h"
#include "jsapi.h"
#include "mozilla/HoldDropJSObjects.h"
#include "nsContentUtils.h"
#include "nsDOMClassInfo.h"
#include "mozilla/dom/HTMLAllCollectionBinding.h"
#include "mozilla/dom/Nullable.h"
#include "mozilla/dom/UnionTypes.h"
#include "nsHTMLDocument.h"
#include "nsJSUtils.h"
#include "nsWrapperCacheInlines.h"
#include "xpcpublic.h"
using namespace mozilla;
using namespace mozilla::dom;
class nsHTMLDocumentSH
{
public:
static bool DocumentAllGetProperty(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
JS::MutableHandle<JS::Value> vp);
static bool DocumentAllNewResolve(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id,
JS::MutableHandle<JSObject*> objp);
static void ReleaseDocument(JSFreeOp *fop, JSObject *obj);
static bool CallToGetPropMapper(JSContext *cx, unsigned argc, JS::Value *vp);
};
const JSClass sHTMLDocumentAllClass = {
"HTML document.all class",
JSCLASS_HAS_PRIVATE | JSCLASS_PRIVATE_IS_NSISUPPORTS | JSCLASS_NEW_RESOLVE |
JSCLASS_EMULATES_UNDEFINED,
JS_PropertyStub, /* addProperty */
JS_DeletePropertyStub, /* delProperty */
nsHTMLDocumentSH::DocumentAllGetProperty, /* getProperty */
JS_StrictPropertyStub, /* setProperty */
JS_EnumerateStub,
(JSResolveOp)nsHTMLDocumentSH::DocumentAllNewResolve,
JS_ConvertStub,
nsHTMLDocumentSH::ReleaseDocument,
nsHTMLDocumentSH::CallToGetPropMapper
};
namespace mozilla {
namespace dom {
@ -51,37 +18,31 @@ HTMLAllCollection::HTMLAllCollection(nsHTMLDocument* aDocument)
: mDocument(aDocument)
{
MOZ_ASSERT(mDocument);
mozilla::HoldJSObjects(this);
SetIsDOMBinding();
}
HTMLAllCollection::~HTMLAllCollection()
{
mObject = nullptr;
mozilla::DropJSObjects(this);
}
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(HTMLAllCollection, AddRef)
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(HTMLAllCollection, Release)
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(HTMLAllCollection,
mDocument,
mCollection,
mNamedMap)
NS_IMPL_CYCLE_COLLECTION_CLASS(HTMLAllCollection)
NS_IMPL_CYCLE_COLLECTING_ADDREF(HTMLAllCollection)
NS_IMPL_CYCLE_COLLECTING_RELEASE(HTMLAllCollection)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(HTMLAllCollection)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCollection)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNamedMap)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(HTMLAllCollection)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(HTMLAllCollection)
tmp->mObject = nullptr;
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCollection)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mNamedMap)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(HTMLAllCollection)
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mObject)
NS_IMPL_CYCLE_COLLECTION_TRACE_END
nsINode*
HTMLAllCollection::GetParentObject() const
{
return mDocument;
}
uint32_t
HTMLAllCollection::Length()
@ -95,32 +56,6 @@ HTMLAllCollection::Item(uint32_t aIndex)
return Collection()->Item(aIndex);
}
JSObject*
HTMLAllCollection::GetObject(JSContext* aCx, ErrorResult& aRv)
{
MOZ_ASSERT(aCx);
if (!mObject) {
JS::Rooted<JSObject*> wrapper(aCx, mDocument->GetWrapper());
MOZ_ASSERT(wrapper);
JSAutoCompartment ac(aCx, wrapper);
JS::Rooted<JSObject*> global(aCx, JS_GetGlobalForObject(aCx, wrapper));
mObject = JS_NewObject(aCx, &sHTMLDocumentAllClass, JS::NullPtr(), global);
if (!mObject) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return nullptr;
}
// Make the JSObject hold a reference to the document.
JS_SetPrivate(mObject, ToSupports(mDocument));
NS_ADDREF(mDocument);
}
JS::ExposeObjectToActiveJS(mObject);
return mObject;
}
nsContentList*
HTMLAllCollection::Collection()
{
@ -186,197 +121,43 @@ HTMLAllCollection::GetDocumentAllList(const nsAString& aID)
return docAllList;
}
nsISupports*
HTMLAllCollection::GetNamedItem(const nsAString& aID,
nsWrapperCache** aCache)
void
HTMLAllCollection::NamedGetter(const nsAString& aID,
bool& aFound,
Nullable<OwningNodeOrHTMLCollection>& aResult)
{
nsContentList* docAllList = GetDocumentAllList(aID);
if (!docAllList) {
return nullptr;
aFound = false;
aResult.SetNull();
return;
}
// Check if there are more than 1 entries. Do this by getting the second one
// rather than the length since getting the length always requires walking
// the entire document.
nsIContent* cont = docAllList->Item(1, true);
if (cont) {
*aCache = docAllList;
return static_cast<nsINodeList*>(docAllList);
if (docAllList->Item(1, true)) {
aFound = true;
aResult.SetValue().SetAsHTMLCollection() = docAllList;
return;
}
// There's only 0 or 1 items. Return the first one or null.
*aCache = cont = docAllList->Item(0, true);
return cont;
if (nsIContent* node = docAllList->Item(0, true)) {
aFound = true;
aResult.SetValue().SetAsNode() = node;
return;
}
aFound = false;
aResult.SetNull();
}
JSObject*
HTMLAllCollection::WrapObject(JSContext* aCx)
{
return HTMLAllCollectionBinding::Wrap(aCx, this);
}
} // namespace dom
} // namespace mozilla
static nsHTMLDocument*
GetDocument(JSObject *obj)
{
MOZ_ASSERT(js::GetObjectJSClass(obj) == &sHTMLDocumentAllClass);
return static_cast<nsHTMLDocument*>(
static_cast<nsINode*>(JS_GetPrivate(obj)));
}
bool
nsHTMLDocumentSH::DocumentAllGetProperty(JSContext *cx, JS::Handle<JSObject*> obj_,
JS::Handle<jsid> id, JS::MutableHandle<JS::Value> vp)
{
JS::Rooted<JSObject*> obj(cx, obj_);
// document.all.item and .namedItem get their value in the
// newResolve hook, so nothing to do for those properties here. And
// we need to return early to prevent <div id="item"> from shadowing
// document.all.item(), etc.
if (nsDOMClassInfo::sItem_id == id || nsDOMClassInfo::sNamedItem_id == id) {
return true;
}
JS::Rooted<JSObject*> proto(cx);
while (js::GetObjectJSClass(obj) != &sHTMLDocumentAllClass) {
if (!js::GetObjectProto(cx, obj, &proto)) {
return false;
}
if (!proto) {
NS_ERROR("The JS engine lies!");
return true;
}
obj = proto;
}
HTMLAllCollection* allCollection = GetDocument(obj)->All();
nsISupports *result;
nsWrapperCache *cache;
if (JSID_IS_STRING(id)) {
if (nsDOMClassInfo::sLength_id == id) {
// Make sure <div id="length"> doesn't shadow document.all.length.
vp.setNumber(allCollection->Length());
return true;
}
// For all other strings, look for an element by id or name.
nsDependentJSString str(id);
result = allCollection->GetNamedItem(str, &cache);
} else if (JSID_IS_INT(id) && JSID_TO_INT(id) >= 0) {
// Map document.all[n] (where n is a number) to the n:th item in
// the document.all node list.
nsIContent* node = allCollection->Item(SafeCast<uint32_t>(JSID_TO_INT(id)));
result = node;
cache = node;
} else {
result = nullptr;
}
if (result) {
nsresult rv = nsContentUtils::WrapNative(cx, result, cache, vp);
if (NS_FAILED(rv)) {
xpc::Throw(cx, rv);
return false;
}
} else {
vp.setUndefined();
}
return true;
}
bool
nsHTMLDocumentSH::DocumentAllNewResolve(JSContext *cx, JS::Handle<JSObject*> obj,
JS::Handle<jsid> id,
JS::MutableHandle<JSObject*> objp)
{
JS::Rooted<JS::Value> v(cx);
if (nsDOMClassInfo::sItem_id == id || nsDOMClassInfo::sNamedItem_id == id) {
// Define the item() or namedItem() method.
JSFunction *fnc = ::JS_DefineFunctionById(cx, obj, id, CallToGetPropMapper,
0, JSPROP_ENUMERATE);
objp.set(obj);
return fnc != nullptr;
}
if (nsDOMClassInfo::sLength_id == id) {
// document.all.length. Any jsval other than undefined would do
// here, all we need is to get into the code below that defines
// this propery on obj, the rest happens in
// DocumentAllGetProperty().
v = JSVAL_ONE;
} else {
if (!DocumentAllGetProperty(cx, obj, id, &v)) {
return false;
}
}
bool ok = true;
if (v.get() != JSVAL_VOID) {
ok = ::JS_DefinePropertyById(cx, obj, id, v, 0);
objp.set(obj);
}
return ok;
}
void
nsHTMLDocumentSH::ReleaseDocument(JSFreeOp *fop, JSObject *obj)
{
nsIHTMLDocument* doc = GetDocument(obj);
if (doc) {
nsContentUtils::DeferredFinalize(doc);
}
}
bool
nsHTMLDocumentSH::CallToGetPropMapper(JSContext *cx, unsigned argc, jsval *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
// Handle document.all("foo") style access to document.all.
if (args.length() != 1) {
// XXX: Should throw NS_ERROR_XPC_NOT_ENOUGH_ARGS for argc < 1,
// and create a new NS_ERROR_XPC_TOO_MANY_ARGS for argc > 1? IE
// accepts nothing other than one arg.
xpc::Throw(cx, NS_ERROR_INVALID_ARG);
return false;
}
// Convert all types to string.
JS::Rooted<JSString*> str(cx, JS::ToString(cx, args[0]));
if (!str) {
return false;
}
// If we are called via document.all(id) instead of document.all.item(i) or
// another method, use the document.all callee object as self.
JS::Rooted<JSObject*> self(cx);
if (args.calleev().isObject() &&
JS_GetClass(&args.calleev().toObject()) == &sHTMLDocumentAllClass) {
self = &args.calleev().toObject();
} else {
self = JS_THIS_OBJECT(cx, vp);
if (!self)
return false;
}
size_t length;
JS::Anchor<JSString *> anchor(str);
const jschar *chars = ::JS_GetStringCharsAndLength(cx, str, &length);
if (!chars) {
return false;
}
return ::JS_GetUCProperty(cx, self, chars, length, args.rval());
}

View File

@ -7,49 +7,84 @@
#ifndef mozilla_dom_HTMLAllCollection_h
#define mozilla_dom_HTMLAllCollection_h
#include "js/RootingAPI.h"
#include "nsAutoPtr.h"
#include "nsCycleCollectionParticipant.h"
#include "nsISupportsImpl.h"
#include "nsRefPtrHashtable.h"
#include "nsWrapperCache.h"
#include <stdint.h>
class nsContentList;
class nsHTMLDocument;
class nsIContent;
class nsWrapperCache;
class nsINode;
namespace mozilla {
class ErrorResult;
namespace dom {
class HTMLAllCollection
class OwningNodeOrHTMLCollection;
template<typename> class Nullable;
class HTMLAllCollection MOZ_FINAL : public nsISupports
, public nsWrapperCache
{
public:
HTMLAllCollection(nsHTMLDocument* aDocument);
~HTMLAllCollection();
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(HTMLAllCollection)
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(HTMLAllCollection)
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(HTMLAllCollection)
virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
nsINode* GetParentObject() const;
uint32_t Length();
nsIContent* Item(uint32_t aIndex);
void Item(const nsAString& aName, Nullable<OwningNodeOrHTMLCollection>& aResult)
{
NamedItem(aName, aResult);
}
nsIContent* IndexedGetter(uint32_t aIndex, bool& aFound)
{
nsIContent* result = Item(aIndex);
aFound = !!result;
return result;
}
JSObject* GetObject(JSContext* aCx, ErrorResult& aRv);
nsISupports* GetNamedItem(const nsAString& aID, nsWrapperCache** aCache);
void NamedItem(const nsAString& aName,
Nullable<OwningNodeOrHTMLCollection>& aResult)
{
bool found = false;
NamedGetter(aName, found, aResult);
}
void NamedGetter(const nsAString& aName,
bool& aFound,
Nullable<OwningNodeOrHTMLCollection>& aResult);
void GetSupportedNames(unsigned flags, nsTArray<nsString>& aNames)
{
}
bool NameIsEnumerable(const nsAString& aName)
{
return false;
}
void LegacyCall(JS::Handle<JS::Value>, const nsAString& aName,
Nullable<OwningNodeOrHTMLCollection>& aResult)
{
NamedItem(aName, aResult);
}
private:
nsContentList* Collection();
/**
* Returns the NodeList for document.all[aID], or null if there isn't one.
* Returns the HTMLCollection for document.all[aID], or null if there isn't one.
*/
nsContentList* GetDocumentAllList(const nsAString& aID);
JS::Heap<JSObject*> mObject;
nsRefPtr<nsHTMLDocument> mDocument;
nsRefPtr<nsContentList> mCollection;
nsRefPtrHashtable<nsStringHashKey, nsContentList> mNamedMap;

View File

@ -2564,12 +2564,6 @@ nsHTMLDocument::All()
return mAll;
}
JSObject*
nsHTMLDocument::GetAll(JSContext* aCx, ErrorResult& aRv)
{
return All()->GetObject(aCx, aRv);
}
static void
NotifyEditableStateChange(nsINode *aNode, nsIDocument *aDocument)
{

View File

@ -104,7 +104,6 @@ public:
NS_DECL_NSIDOMHTMLDOCUMENT
mozilla::dom::HTMLAllCollection* All();
JSObject* GetAll(JSContext* aCx, mozilla::ErrorResult& aRv);
nsISupports* ResolveName(const nsAString& aName, nsWrapperCache **aCache);

View File

@ -485,10 +485,6 @@ bool nsDOMClassInfo::sIsInitialized = false;
jsid nsDOMClassInfo::sLocation_id = JSID_VOID;
jsid nsDOMClassInfo::sConstructor_id = JSID_VOID;
jsid nsDOMClassInfo::sLength_id = JSID_VOID;
jsid nsDOMClassInfo::sItem_id = JSID_VOID;
jsid nsDOMClassInfo::sNamedItem_id = JSID_VOID;
jsid nsDOMClassInfo::sEnumerate_id = JSID_VOID;
jsid nsDOMClassInfo::sTop_id = JSID_VOID;
jsid nsDOMClassInfo::sDocument_id = JSID_VOID;
jsid nsDOMClassInfo::sWrappedJSObject_id = JSID_VOID;
@ -595,10 +591,6 @@ nsDOMClassInfo::DefineStaticJSVals(JSContext *cx)
SET_JSID_TO_STRING(sLocation_id, cx, "location");
SET_JSID_TO_STRING(sConstructor_id, cx, "constructor");
SET_JSID_TO_STRING(sLength_id, cx, "length");
SET_JSID_TO_STRING(sItem_id, cx, "item");
SET_JSID_TO_STRING(sNamedItem_id, cx, "namedItem");
SET_JSID_TO_STRING(sEnumerate_id, cx, "enumerateProperties");
SET_JSID_TO_STRING(sTop_id, cx, "top");
SET_JSID_TO_STRING(sDocument_id, cx, "document");
SET_JSID_TO_STRING(sWrappedJSObject_id, cx, "wrappedJSObject");
@ -1717,9 +1709,6 @@ nsDOMClassInfo::ShutDown()
sLocation_id = JSID_VOID;
sConstructor_id = JSID_VOID;
sLength_id = JSID_VOID;
sItem_id = JSID_VOID;
sEnumerate_id = JSID_VOID;
sTop_id = JSID_VOID;
sDocument_id = JSID_VOID;
sWrappedJSObject_id = JSID_VOID;

View File

@ -166,10 +166,6 @@ protected:
public:
static jsid sLocation_id;
static jsid sConstructor_id;
static jsid sLength_id;
static jsid sItem_id;
static jsid sNamedItem_id;
static jsid sEnumerate_id;
static jsid sTop_id;
static jsid sDocument_id;
static jsid sWrappedJSObject_id;

View File

@ -48,12 +48,9 @@ support-files =
[test_named_frames.html]
[test_navigator_resolve_identity.html]
[test_navigator_language.html]
[test_nondomexception.html]
[test_openDialogChromeOnly.html]
[test_open_null_features.html]
skip-if = (buildapp == 'b2g' && toolkit != 'gonk') # Fails on b2g-desktop, tracked in bug 1011874
[test_postMessage_solidus.html]
[test_screen_orientation.html]
[test_settimeout_extra_arguments.html]

View File

@ -1,28 +0,0 @@
<!-- intentionally omiting doctype because this test requires document.all -->
<html>
<head>
<title>Test for non-DOM module exceptions</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="application/javascript;version=1.7">
"use strict";
try {
document.all();
} catch (e) {
is(typeof e, "object");
is(e.filename, location);
is(e.lineNumber, 18);
}
</script>
</pre>
</body>
</html>

View File

@ -381,6 +381,8 @@ var interfaceNamesInGlobalScope =
"HashChangeEvent",
// IMPORTANT: Do not change this list without review from a DOM peer!
"History",
// IMPORTANT: Do not change this list without review from a DOM peer!
"HTMLAllCollection",
// IMPORTANT: Do not change this list without review from a DOM peer!
"HTMLAnchorElement",
// IMPORTANT: Do not change this list without review from a DOM peer!

View File

@ -0,0 +1,13 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Emulates undefined through Codegen.py. */
interface HTMLAllCollection {
readonly attribute unsigned long length;
getter Node? (unsigned long index);
Node? item(unsigned long index);
(Node or HTMLCollection)? item(DOMString name);
legacycaller (Node or HTMLCollection)? (DOMString name);
getter (Node or HTMLCollection)? namedItem(DOMString name);
};

View File

@ -72,8 +72,7 @@ interface HTMLDocument : Document {
void clear();
[Throws]
readonly attribute object all;
readonly attribute HTMLAllCollection all;
// https://dvcs.w3.org/hg/editing/raw-file/tip/editing.html#selections
[Throws]

View File

@ -124,6 +124,7 @@ WEBIDL_FILES = [
'GeometryUtils.webidl',
'GetUserMediaRequest.webidl',
'History.webidl',
'HTMLAllCollection.webidl',
'HTMLAnchorElement.webidl',
'HTMLAppletElement.webidl',
'HTMLAreaElement.webidl',