gecko-dev/dom/base/Attr.cpp

337 строки
7.8 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
2012-05-21 15:12:37 +04:00
/* 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/. */
1999-01-12 11:45:23 +03:00
/*
* Implementation of DOM Core's Attr node.
*/
#include "mozilla/dom/Attr.h"
2013-04-09 19:29:47 +04:00
#include "mozilla/dom/AttrBinding.h"
#include "mozilla/dom/Element.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/InternalMutationEvent.h"
#include "nsContentCreatorFunctions.h"
#include "nsError.h"
#include "nsUnicharUtils.h"
#include "nsDOMString.h"
#include "nsIContentInlines.h"
#include "nsIDocument.h"
#include "nsGkAtoms.h"
#include "nsCOMArray.h"
#include "nsNameSpaceManager.h"
#include "nsNodeUtils.h"
#include "nsTextNode.h"
#include "mozAutoDocUpdate.h"
#include "nsWrapperCacheInlines.h"
1999-01-12 11:45:23 +03:00
nsIAttribute::nsIAttribute(nsDOMAttributeMap* aAttrMap,
already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
: nsINode(aNodeInfo), mAttrMap(aAttrMap)
{
}
nsIAttribute::~nsIAttribute()
{
}
namespace mozilla {
namespace dom {
1999-01-12 11:45:23 +03:00
//----------------------------------------------------------------------
bool Attr::sInitialized;
1999-01-12 11:45:23 +03:00
Attr::Attr(nsDOMAttributeMap *aAttrMap,
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
const nsAString &aValue)
: nsIAttribute(aAttrMap, aNodeInfo), mValue(aValue)
1999-01-12 11:45:23 +03:00
{
MOZ_ASSERT(mNodeInfo, "We must get a nodeinfo here!");
MOZ_ASSERT(mNodeInfo->NodeType() == ATTRIBUTE_NODE,
"Wrong nodeType");
1999-01-12 11:45:23 +03:00
// We don't add a reference to our content. It will tell us
// to drop our reference when it goes away.
}
NS_IMPL_CYCLE_COLLECTION_CLASS(Attr)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Attr)
if (!nsINode::Traverse(tmp, cb)) {
return NS_SUCCESS_INTERRUPTED_TRAVERSE;
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAttrMap)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(Attr)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Attr)
nsINode::Unlink(tmp);
NS_IMPL_CYCLE_COLLECTION_UNLINK(mAttrMap)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(Attr)
Element* ownerElement = tmp->GetElement();
if (tmp->HasKnownLiveWrapper()) {
if (ownerElement) {
// The attribute owns the element via attribute map so we can
// mark it when the attribute is certainly alive.
mozilla::dom::FragmentOrElement::MarkNodeChildren(ownerElement);
}
return true;
}
if (ownerElement &&
mozilla::dom::FragmentOrElement::CanSkip(ownerElement, true)) {
return true;
}
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(Attr)
return tmp->HasKnownLiveWrapperAndDoesNotNeedTracing(static_cast<nsIAttribute*>(tmp));
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(Attr)
return tmp->HasKnownLiveWrapper();
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
// QueryInterface implementation for Attr
NS_INTERFACE_TABLE_HEAD(Attr)
NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
NS_INTERFACE_TABLE(Attr, nsINode, nsIAttribute, nsIDOMNode,
nsIDOMEventTarget, EventTarget)
NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(Attr)
NS_INTERFACE_MAP_ENTRY_TEAROFF(nsISupportsWeakReference,
new nsNodeSupportsWeakRefTearoff(this))
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(Attr)
NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(Attr,
nsNodeUtils::LastRelease(this))
void
Attr::SetMap(nsDOMAttributeMap *aMap)
{
if (mAttrMap && !aMap && sInitialized) {
// We're breaking a relationship with content and not getting a new one,
// need to locally cache value. GetValue() does that.
GetValue(mValue);
}
mAttrMap = aMap;
}
Element*
Attr::GetElement() const
{
if (!mAttrMap) {
return nullptr;
}
nsIContent* content = mAttrMap->GetContent();
return content ? content->AsElement() : nullptr;
}
nsresult
Attr::SetOwnerDocument(nsIDocument* aDocument)
{
NS_ASSERTION(aDocument, "Missing document");
nsIDocument *doc = OwnerDoc();
NS_ASSERTION(doc != aDocument, "bad call to Attr::SetOwnerDocument");
doc->DeleteAllPropertiesFor(this);
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<mozilla::dom::NodeInfo> newNodeInfo;
newNodeInfo = aDocument->NodeInfoManager()->
GetNodeInfo(mNodeInfo->NameAtom(), mNodeInfo->GetPrefixAtom(),
mNodeInfo->NamespaceID(), ATTRIBUTE_NODE);
NS_ASSERTION(newNodeInfo, "GetNodeInfo lies");
mNodeInfo.swap(newNodeInfo);
return NS_OK;
}
void
Attr::GetName(nsAString& aName)
1999-01-12 11:45:23 +03:00
{
aName = NodeName();
1999-01-12 11:45:23 +03:00
}
void
Attr::GetValue(nsAString& aValue)
1999-01-12 11:45:23 +03:00
{
Element* element = GetElement();
if (element) {
RefPtr<nsAtom> nameAtom = mNodeInfo->NameAtom();
element->GetAttr(mNodeInfo->NamespaceID(), nameAtom, aValue);
}
else {
aValue = mValue;
1999-01-12 11:45:23 +03:00
}
}
2013-04-09 19:29:47 +04:00
void
Attr::SetValue(const nsAString& aValue, nsIPrincipal* aTriggeringPrincipal, ErrorResult& aRv)
1999-01-12 11:45:23 +03:00
{
Element* element = GetElement();
if (!element) {
mValue = aValue;
2013-04-09 19:29:47 +04:00
return;
}
RefPtr<nsAtom> nameAtom = mNodeInfo->NameAtom();
aRv = element->SetAttr(mNodeInfo->NamespaceID(),
2013-04-09 19:29:47 +04:00
nameAtom,
mNodeInfo->GetPrefixAtom(),
aValue,
aTriggeringPrincipal,
2013-04-09 19:29:47 +04:00
true);
}
void
Attr::SetValue(const nsAString& aValue, ErrorResult& aRv)
2013-04-09 19:29:47 +04:00
{
SetValue(aValue, nullptr, aRv);
1999-01-12 11:45:23 +03:00
}
2013-04-09 19:29:47 +04:00
bool
Attr::Specified() const
{
return true;
}
Element*
Attr::GetOwnerElement(ErrorResult& aRv)
{
return GetElement();
}
void
Attr::GetNodeValueInternal(nsAString& aNodeValue)
1999-01-12 11:45:23 +03:00
{
GetValue(aNodeValue);
1999-01-12 11:45:23 +03:00
}
void
Attr::SetNodeValueInternal(const nsAString& aNodeValue, ErrorResult& aError)
1999-01-12 11:45:23 +03:00
{
SetValue(aNodeValue, nullptr, aError);
1999-01-12 11:45:23 +03:00
}
nsresult
Attr::Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult,
bool aPreallocateChildren) const
1999-01-12 11:45:23 +03:00
{
nsAutoString value;
const_cast<Attr*>(this)->GetValue(value);
1999-01-12 11:45:23 +03:00
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo;
*aResult = new Attr(nullptr, ni.forget(), value);
if (!*aResult) {
1999-01-12 11:45:23 +03:00
return NS_ERROR_OUT_OF_MEMORY;
}
NS_ADDREF(*aResult);
return NS_OK;
1999-01-12 11:45:23 +03:00
}
already_AddRefed<nsIURI>
Attr::GetBaseURI(bool aTryUseXHRDocBaseURI) const
{
Element* parent = GetElement();
return parent ? parent->GetBaseURI(aTryUseXHRDocBaseURI) : nullptr;
}
void
Attr::GetTextContentInternal(nsAString& aTextContent,
OOMReporter& aError)
{
GetValue(aTextContent);
}
void
Attr::SetTextContentInternal(const nsAString& aTextContent,
nsIPrincipal* aSubjectPrincipal,
ErrorResult& aError)
{
SetNodeValueInternal(aTextContent, aError);
}
bool
Attr::IsNodeOfType(uint32_t aFlags) const
{
return !(aFlags & ~eATTRIBUTE);
}
uint32_t
Attr::GetChildCount() const
{
return 0;
}
nsIContent *
Attr::GetChildAt_Deprecated(uint32_t aIndex) const
{
return nullptr;
}
int32_t
Attr::ComputeIndexOf(const nsINode* aPossibleChild) const
{
return -1;
}
nsresult
Attr::InsertChildBefore(nsIContent* aKid, nsIContent* aBeforeThis,
bool aNotify)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
nsresult
Attr::InsertChildAt_Deprecated(nsIContent* aKid, uint32_t aIndex,
bool aNotify)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
void
Attr::RemoveChildAt_Deprecated(uint32_t aIndex, bool aNotify)
{
}
void
Attr::RemoveChildNode(nsIContent* aKid, bool aNotify)
{
}
nsresult
Attr::GetEventTargetParent(EventChainPreVisitor& aVisitor)
{
aVisitor.mCanHandle = true;
return NS_OK;
}
void
Attr::Initialize()
{
sInitialized = true;
}
void
Attr::Shutdown()
{
sInitialized = false;
}
2013-04-09 19:29:47 +04:00
JSObject*
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv The only manual changes here are to BindingUtils.h, BindingUtils.cpp, Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp, dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp, Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp, Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The rest of this diff was generated by running the following commands: find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
Attr::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
2013-04-09 19:29:47 +04:00
{
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv The only manual changes here are to BindingUtils.h, BindingUtils.cpp, Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp, dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp, Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp, Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The rest of this diff was generated by running the following commands: find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
return AttrBinding::Wrap(aCx, this, aGivenProto);
2013-04-09 19:29:47 +04:00
}
} // namespace dom
} // namespace mozilla