michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Implementation of DOM Core's nsIDOMDocumentFragment. michael@0: */ michael@0: michael@0: #include "mozilla/dom/DocumentFragment.h" michael@0: #include "mozilla/dom/Element.h" // for NS_IMPL_ELEMENT_CLONE michael@0: #include "nsINodeInfo.h" michael@0: #include "nsNodeInfoManager.h" michael@0: #include "nsError.h" michael@0: #include "nsGkAtoms.h" michael@0: #include "nsDOMString.h" michael@0: #include "nsContentUtils.h" // for NS_INTERFACE_MAP_ENTRY_TEAROFF michael@0: #include "mozilla/dom/DocumentFragmentBinding.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsIDocument.h" michael@0: #include "mozilla/IntegerPrintfMacros.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: JSObject* michael@0: DocumentFragment::WrapNode(JSContext *aCx) michael@0: { michael@0: return DocumentFragmentBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: bool michael@0: DocumentFragment::IsNodeOfType(uint32_t aFlags) const michael@0: { michael@0: return !(aFlags & ~(eCONTENT | eDOCUMENT_FRAGMENT)); michael@0: } michael@0: michael@0: nsIAtom* michael@0: DocumentFragment::DoGetID() const michael@0: { michael@0: return nullptr; michael@0: } michael@0: michael@0: nsIAtom* michael@0: DocumentFragment::GetIDAttributeName() const michael@0: { michael@0: return nullptr; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: DocumentFragment::QuerySelector(const nsAString& aSelector, michael@0: nsIDOMElement **aReturn) michael@0: { michael@0: return nsINode::QuerySelector(aSelector, aReturn); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: DocumentFragment::QuerySelectorAll(const nsAString& aSelector, michael@0: nsIDOMNodeList **aReturn) michael@0: { michael@0: return nsINode::QuerySelectorAll(aSelector, aReturn); michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: void michael@0: DocumentFragment::List(FILE* out, int32_t aIndent) const michael@0: { michael@0: int32_t indent; michael@0: for (indent = aIndent; --indent >= 0; ) { michael@0: fputs(" ", out); michael@0: } michael@0: michael@0: fprintf(out, "DocumentFragment@%p", (void *)this); michael@0: michael@0: fprintf(out, " flags=[%08x]", static_cast(GetFlags())); michael@0: fprintf(out, " refcount=%" PRIuPTR "<", mRefCnt.get()); michael@0: michael@0: nsIContent* child = GetFirstChild(); michael@0: if (child) { michael@0: fputs("\n", out); michael@0: michael@0: for (; child; child = child->GetNextSibling()) { michael@0: child->List(out, aIndent + 1); michael@0: } michael@0: michael@0: for (indent = aIndent; --indent >= 0; ) { michael@0: fputs(" ", out); michael@0: } michael@0: } michael@0: michael@0: fputs(">\n", out); michael@0: } michael@0: michael@0: void michael@0: DocumentFragment::DumpContent(FILE* out, int32_t aIndent, michael@0: bool aDumpAll) const michael@0: { michael@0: int32_t indent; michael@0: for (indent = aIndent; --indent >= 0; ) { michael@0: fputs(" ", out); michael@0: } michael@0: michael@0: fputs("", out); michael@0: michael@0: if(aIndent) { michael@0: fputs("\n", out); michael@0: } michael@0: michael@0: for (nsIContent* child = GetFirstChild(); michael@0: child; michael@0: child = child->GetNextSibling()) { michael@0: int32_t indent = aIndent ? aIndent + 1 : 0; michael@0: child->DumpContent(out, indent, aDumpAll); michael@0: } michael@0: for (indent = aIndent; --indent >= 0; ) { michael@0: fputs(" ", out); michael@0: } michael@0: fputs("", out); michael@0: michael@0: if(aIndent) { michael@0: fputs("\n", out); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* static */ already_AddRefed michael@0: DocumentFragment::Constructor(const GlobalObject& aGlobal, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsCOMPtr window = do_QueryInterface(aGlobal.GetAsSupports()); michael@0: if (!window || !window->GetDoc()) { michael@0: aRv.Throw(NS_ERROR_FAILURE); michael@0: return nullptr; michael@0: } michael@0: michael@0: return window->GetDoc()->CreateDocumentFragment(); michael@0: } michael@0: michael@0: // QueryInterface implementation for DocumentFragment michael@0: NS_INTERFACE_MAP_BEGIN(DocumentFragment) michael@0: NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY michael@0: NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(DocumentFragment) michael@0: NS_INTERFACE_MAP_ENTRY(nsIContent) michael@0: NS_INTERFACE_MAP_ENTRY(nsINode) michael@0: NS_INTERFACE_MAP_ENTRY(nsIDOMDocumentFragment) michael@0: NS_INTERFACE_MAP_ENTRY(nsIDOMNode) michael@0: NS_INTERFACE_MAP_ENTRY(nsIDOMEventTarget) michael@0: NS_INTERFACE_MAP_ENTRY(mozilla::dom::EventTarget) michael@0: NS_INTERFACE_MAP_ENTRY_TEAROFF(nsISupportsWeakReference, michael@0: new nsNodeSupportsWeakRefTearoff(this)) michael@0: // DOM bindings depend on the identity pointer being the michael@0: // same as nsINode (which nsIContent inherits). michael@0: NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContent) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: NS_IMPL_ADDREF_INHERITED(DocumentFragment, FragmentOrElement) michael@0: NS_IMPL_RELEASE_INHERITED(DocumentFragment, FragmentOrElement) michael@0: michael@0: NS_IMPL_ELEMENT_CLONE(DocumentFragment) michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla