2013-01-24 21:24:57 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim: set ts=8 sts=4 et sw=4 tw=80: */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* An implementation for a Gecko-style content sink that knows how
|
|
|
|
* to build a content model (the "prototype" document) from XUL.
|
|
|
|
*
|
|
|
|
* For more information on XUL,
|
|
|
|
* see http://developer.mozilla.org/en/docs/XUL
|
|
|
|
*/
|
|
|
|
|
2013-03-17 00:55:09 -07:00
|
|
|
#include "nsXULContentSink.h"
|
|
|
|
|
2012-01-15 00:13:08 -08:00
|
|
|
#include "jsfriendapi.h"
|
2013-03-17 00:55:09 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsForwardReference.h"
|
2013-03-17 00:55:09 -07:00
|
|
|
#include "nsHTMLStyleSheet.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIContentSink.h"
|
2013-03-17 00:55:09 -07:00
|
|
|
#include "nsIDocument.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMEventListener.h"
|
|
|
|
#include "nsIDOMHTMLFormElement.h"
|
|
|
|
#include "nsIDOMXULDocument.h"
|
|
|
|
#include "nsIFormControl.h"
|
2014-06-19 19:01:40 -07:00
|
|
|
#include "mozilla/dom/NodeInfo.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIURL.h"
|
2014-02-27 15:04:46 -08:00
|
|
|
#include "nsNameSpaceManager.h"
|
2013-03-17 00:55:09 -07:00
|
|
|
#include "nsParserBase.h"
|
2013-01-04 19:12:24 -08:00
|
|
|
#include "nsViewManager.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIXULDocument.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsLayoutCID.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsRDFCID.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsXULElement.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
|
2007-09-05 02:13:57 -07:00
|
|
|
#include "nsXULPrototypeDocument.h" // XXXbe temporary
|
2010-06-28 15:49:35 -07:00
|
|
|
#include "mozilla/css/Loader.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsAttrName.h"
|
|
|
|
#include "nsXMLContentSink.h"
|
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "nsIScriptError.h"
|
2013-08-21 12:28:26 -07:00
|
|
|
#include "nsContentTypeParser.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2013-11-27 15:13:11 -08:00
|
|
|
static PRLogModuleInfo* gContentSinkLog;
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
XULContentSinkImpl::ContextStack::ContextStack()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mTop(nullptr), mDepth(0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
XULContentSinkImpl::ContextStack::~ContextStack()
|
|
|
|
{
|
|
|
|
while (mTop) {
|
|
|
|
Entry* doomed = mTop;
|
|
|
|
mTop = mTop->mNext;
|
|
|
|
delete doomed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
XULContentSinkImpl::ContextStack::Push(nsXULPrototypeNode* aNode, State aState)
|
|
|
|
{
|
|
|
|
Entry* entry = new Entry;
|
|
|
|
if (! entry)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
entry->mNode = aNode;
|
|
|
|
entry->mState = aState;
|
|
|
|
entry->mNext = mTop;
|
|
|
|
mTop = entry;
|
|
|
|
|
|
|
|
++mDepth;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
XULContentSinkImpl::ContextStack::Pop(State* aState)
|
|
|
|
{
|
|
|
|
if (mDepth == 0)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
Entry* entry = mTop;
|
|
|
|
mTop = mTop->mNext;
|
|
|
|
--mDepth;
|
|
|
|
|
|
|
|
*aState = entry->mState;
|
|
|
|
delete entry;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2008-10-08 04:41:52 -07:00
|
|
|
XULContentSinkImpl::ContextStack::GetTopNode(nsRefPtr<nsXULPrototypeNode>& aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mDepth == 0)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2008-10-08 04:41:52 -07:00
|
|
|
aNode = mTop->mNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2008-10-08 04:41:52 -07:00
|
|
|
XULContentSinkImpl::ContextStack::GetTopChildren(nsPrototypeArray** aChildren)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mDepth == 0)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
*aChildren = &(mTop->mChildren);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-11-12 06:47:14 -08:00
|
|
|
void
|
|
|
|
XULContentSinkImpl::ContextStack::Clear()
|
|
|
|
{
|
|
|
|
Entry *cur = mTop;
|
|
|
|
while (cur) {
|
|
|
|
// Release the root element (and its descendants).
|
|
|
|
Entry *next = cur->mNext;
|
|
|
|
delete cur;
|
|
|
|
cur = next;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mTop = nullptr;
|
2007-11-12 06:47:14 -08:00
|
|
|
mDepth = 0;
|
|
|
|
}
|
|
|
|
|
2013-03-08 18:53:00 -08:00
|
|
|
void
|
|
|
|
XULContentSinkImpl::ContextStack::Traverse(nsCycleCollectionTraversalCallback& aCb)
|
|
|
|
{
|
|
|
|
nsCycleCollectionTraversalCallback& cb = aCb;
|
|
|
|
for (ContextStack::Entry* tmp = mTop; tmp; tmp = tmp->mNext) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mChildren)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2007-03-24 22:02:10 -07:00
|
|
|
XULContentSinkImpl::XULContentSinkImpl()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mText(nullptr),
|
2007-03-22 10:30:00 -07:00
|
|
|
mTextLength(0),
|
|
|
|
mTextSize(0),
|
2011-10-17 07:59:28 -07:00
|
|
|
mConstrainSize(true),
|
2007-03-22 10:30:00 -07:00
|
|
|
mState(eInProlog),
|
2012-07-30 07:20:58 -07:00
|
|
|
mParser(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2013-11-27 15:13:11 -08:00
|
|
|
if (! gContentSinkLog)
|
|
|
|
gContentSinkLog = PR_NewLogModule("nsXULContentSink");
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
XULContentSinkImpl::~XULContentSinkImpl()
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(mParser); // XXX should've been released by now, unless error.
|
|
|
|
|
2007-11-12 06:47:14 -08:00
|
|
|
// The context stack _should_ be empty, unless something has gone wrong.
|
|
|
|
NS_ASSERTION(mContextStack.Depth() == 0, "Context stack not empty?");
|
|
|
|
mContextStack.Clear();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-12-04 23:55:37 -08:00
|
|
|
moz_free(mText);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// nsISupports interface
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(XULContentSinkImpl)
|
|
|
|
|
2013-03-08 18:53:00 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(XULContentSinkImpl)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mNodeInfoManager)
|
|
|
|
tmp->mContextStack.Clear();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPrototype)
|
|
|
|
NS_IF_RELEASE(tmp->mParser);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(XULContentSinkImpl)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNodeInfoManager)
|
|
|
|
tmp->mContextStack.Traverse(cb);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPrototype)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mParser)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(XULContentSinkImpl)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIXMLContentSink)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIXMLContentSink)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIExpatSink)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIContentSink)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(XULContentSinkImpl)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(XULContentSinkImpl)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// nsIContentSink interface
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-06-23 14:22:16 -07:00
|
|
|
XULContentSinkImpl::WillBuildModel(nsDTDMode aDTDMode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
#if FIXME
|
|
|
|
if (! mParentContentSink) {
|
|
|
|
// If we're _not_ an overlay, then notify the document that
|
|
|
|
// the load is beginning.
|
|
|
|
mDocument->BeginLoad();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
XULContentSinkImpl::DidBuildModel(bool aTerminated)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocument);
|
|
|
|
if (doc) {
|
|
|
|
doc->EndLoad();
|
2012-07-30 07:20:58 -07:00
|
|
|
mDocument = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Drop our reference to the parser to get rid of a circular
|
|
|
|
// reference.
|
|
|
|
NS_IF_RELEASE(mParser);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
XULContentSinkImpl::WillInterrupt(void)
|
|
|
|
{
|
|
|
|
// XXX Notify the docshell, if necessary
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
XULContentSinkImpl::WillResume(void)
|
|
|
|
{
|
|
|
|
// XXX Notify the docshell, if necessary
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-01-20 03:16:27 -08:00
|
|
|
XULContentSinkImpl::SetParser(nsParserBase* aParser)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_IF_RELEASE(mParser);
|
|
|
|
mParser = aParser;
|
|
|
|
NS_IF_ADDREF(mParser);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
XULContentSinkImpl::SetDocumentCharset(nsACString& aCharset)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocument);
|
|
|
|
if (doc) {
|
|
|
|
doc->SetDocumentCharacterSet(aCharset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsISupports *
|
|
|
|
XULContentSinkImpl::GetTarget()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocument);
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2007-03-24 22:02:10 -07:00
|
|
|
nsresult
|
|
|
|
XULContentSinkImpl::Init(nsIDocument* aDocument,
|
|
|
|
nsXULPrototypeDocument* aPrototype)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_PRECONDITION(aDocument != nullptr, "null ptr");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (! aDocument)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
mDocument = do_GetWeakReference(aDocument);
|
|
|
|
mPrototype = aPrototype;
|
|
|
|
|
|
|
|
mDocumentURL = mPrototype->GetURI();
|
|
|
|
|
|
|
|
// XXX this presumes HTTP header info is already set in document
|
|
|
|
// XXX if it isn't we need to set it here...
|
|
|
|
// XXXbz not like GetHeaderData on the proto doc _does_ anything....
|
|
|
|
nsAutoString preferredStyle;
|
|
|
|
rv = mPrototype->GetHeaderData(nsGkAtoms::headerDefaultStyle,
|
|
|
|
preferredStyle);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (!preferredStyle.IsEmpty()) {
|
|
|
|
aDocument->SetHeaderData(nsGkAtoms::headerDefaultStyle,
|
|
|
|
preferredStyle);
|
|
|
|
}
|
|
|
|
|
2007-04-20 15:59:18 -07:00
|
|
|
// Set the right preferred style on the document's CSSLoader.
|
|
|
|
aDocument->CSSLoader()->SetPreferredSheet(preferredStyle);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mNodeInfoManager = aPrototype->GetNodeInfoManager();
|
|
|
|
if (! mNodeInfoManager)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
mState = eInProlog;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Text buffering
|
|
|
|
//
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-01-04 07:02:17 -08:00
|
|
|
XULContentSinkImpl::IsDataInBuffer(char16_t* buffer, int32_t length)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < length; ++i) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (buffer[i] == ' ' ||
|
|
|
|
buffer[i] == '\t' ||
|
|
|
|
buffer[i] == '\n' ||
|
|
|
|
buffer[i] == '\r')
|
|
|
|
continue;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
XULContentSinkImpl::FlushText(bool aCreateTextNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
do {
|
|
|
|
// Don't do anything if there's no text to create a node from, or
|
|
|
|
// if they've told us not to create a text node
|
|
|
|
if (! mTextLength)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (! aCreateTextNode)
|
|
|
|
break;
|
|
|
|
|
2008-10-08 04:41:52 -07:00
|
|
|
nsRefPtr<nsXULPrototypeNode> node;
|
|
|
|
rv = mContextStack.GetTopNode(node);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool stripWhitespace = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (node->mType == nsXULPrototypeNode::eType_Element) {
|
2014-06-19 19:01:40 -07:00
|
|
|
mozilla::dom::NodeInfo *nodeInfo =
|
2008-10-08 04:41:52 -07:00
|
|
|
static_cast<nsXULPrototypeElement*>(node.get())->mNodeInfo;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (nodeInfo->NamespaceEquals(kNameSpaceID_XUL))
|
|
|
|
stripWhitespace = !nodeInfo->Equals(nsGkAtoms::label) &&
|
|
|
|
!nodeInfo->Equals(nsGkAtoms::description);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't bother if there's nothing but whitespace.
|
|
|
|
if (stripWhitespace && ! IsDataInBuffer(mText, mTextLength))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Don't bother if we're not in XUL document body
|
|
|
|
if (mState != eInDocumentElement || mContextStack.Depth() == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
nsXULPrototypeText* text = new nsXULPrototypeText();
|
|
|
|
if (! text)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
text->mValue.Assign(mText, mTextLength);
|
|
|
|
if (stripWhitespace)
|
|
|
|
text->mValue.Trim(" \t\n\r");
|
|
|
|
|
|
|
|
// hook it up
|
2012-07-30 07:20:58 -07:00
|
|
|
nsPrototypeArray* children = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = mContextStack.GetTopChildren(&children);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// transfer ownership of 'text' to the children array
|
|
|
|
children->AppendElement(text);
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
// Reset our text buffer
|
|
|
|
mTextLength = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsresult
|
2014-01-04 07:02:17 -08:00
|
|
|
XULContentSinkImpl::NormalizeAttributeString(const char16_t *aExpatName,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAttrName &aName)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t nameSpaceID;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIAtom> prefix, localName;
|
|
|
|
nsContentUtils::SplitExpatName(aExpatName, getter_AddRefs(prefix),
|
|
|
|
getter_AddRefs(localName), &nameSpaceID);
|
|
|
|
|
|
|
|
if (nameSpaceID == kNameSpaceID_None) {
|
|
|
|
aName.SetTo(localName);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-19 19:01:40 -07:00
|
|
|
nsRefPtr<mozilla::dom::NodeInfo> ni;
|
2008-09-12 15:32:18 -07:00
|
|
|
ni = mNodeInfoManager->GetNodeInfo(localName, prefix,
|
2011-06-14 00:56:49 -07:00
|
|
|
nameSpaceID,
|
|
|
|
nsIDOMNode::ATTRIBUTE_NODE);
|
2007-03-22 10:30:00 -07:00
|
|
|
aName.SetTo(ni);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-06-19 19:01:40 -07:00
|
|
|
XULContentSinkImpl::CreateElement(mozilla::dom::NodeInfo *aNodeInfo,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsXULPrototypeElement** aResult)
|
|
|
|
{
|
|
|
|
nsXULPrototypeElement* element = new nsXULPrototypeElement();
|
|
|
|
if (! element)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
element->mNodeInfo = aNodeInfo;
|
|
|
|
|
|
|
|
*aResult = element;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**** BEGIN NEW APIs ****/
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 07:02:17 -08:00
|
|
|
XULContentSinkImpl::HandleStartElement(const char16_t *aName,
|
|
|
|
const char16_t **aAtts,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aAttsCount,
|
|
|
|
uint32_t aLineNumber)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// XXX Hopefully the parser will flag this before we get here. If
|
|
|
|
// we're in the epilog, there should be no new elements
|
|
|
|
NS_PRECONDITION(mState != eInEpilog, "tag in XUL doc epilog");
|
|
|
|
NS_PRECONDITION(aAttsCount % 2 == 0, "incorrect aAttsCount");
|
|
|
|
// Adjust aAttsCount so it's the actual number of attributes
|
|
|
|
aAttsCount /= 2;
|
|
|
|
|
|
|
|
if (mState == eInEpilog)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
if (mState != eInScript) {
|
|
|
|
FlushText();
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t nameSpaceID;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIAtom> prefix, localName;
|
|
|
|
nsContentUtils::SplitExpatName(aName, getter_AddRefs(prefix),
|
|
|
|
getter_AddRefs(localName), &nameSpaceID);
|
|
|
|
|
2014-06-19 19:01:40 -07:00
|
|
|
nsRefPtr<mozilla::dom::NodeInfo> nodeInfo;
|
2011-06-14 00:56:49 -07:00
|
|
|
nodeInfo = mNodeInfoManager->GetNodeInfo(localName, prefix, nameSpaceID,
|
|
|
|
nsIDOMNode::ELEMENT_NODE);
|
2008-09-12 15:32:18 -07:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (mState) {
|
|
|
|
case eInProlog:
|
|
|
|
// We're the root document element
|
|
|
|
rv = OpenRoot(aAtts, aAttsCount, nodeInfo);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eInDocumentElement:
|
|
|
|
rv = OpenTag(aAtts, aAttsCount, aLineNumber, nodeInfo);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eInEpilog:
|
|
|
|
case eInScript:
|
2013-11-27 15:13:11 -08:00
|
|
|
PR_LOG(gContentSinkLog, PR_LOG_WARNING,
|
2007-03-22 10:30:00 -07:00
|
|
|
("xul: warning: unexpected tags in epilog at line %d",
|
|
|
|
aLineNumber));
|
|
|
|
rv = NS_ERROR_UNEXPECTED; // XXX
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 07:02:17 -08:00
|
|
|
XULContentSinkImpl::HandleEndElement(const char16_t *aName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Never EVER return anything but NS_OK or
|
|
|
|
// NS_ERROR_HTMLPARSER_BLOCK from this method. Doing so will blow
|
|
|
|
// the parser's little mind all over the planet.
|
|
|
|
nsresult rv;
|
|
|
|
|
2008-10-08 04:41:52 -07:00
|
|
|
nsRefPtr<nsXULPrototypeNode> node;
|
|
|
|
rv = mContextStack.GetTopNode(node);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (node->mType) {
|
|
|
|
case nsXULPrototypeNode::eType_Element: {
|
|
|
|
// Flush any text _now_, so that we'll get text nodes created
|
|
|
|
// before popping the stack.
|
|
|
|
FlushText();
|
|
|
|
|
|
|
|
// Pop the context stack and do prototype hookup.
|
2012-07-30 07:20:58 -07:00
|
|
|
nsPrototypeArray* children = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = mContextStack.GetTopChildren(&children);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsXULPrototypeElement* element =
|
2008-10-08 04:41:52 -07:00
|
|
|
static_cast<nsXULPrototypeElement*>(node.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t count = children->Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (count) {
|
2013-03-21 11:54:36 -07:00
|
|
|
element->mChildren.SetCapacity(count);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < count; ++i)
|
2008-10-08 04:41:52 -07:00
|
|
|
element->mChildren.AppendElement(children->ElementAt(i));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsXULPrototypeNode::eType_Script: {
|
|
|
|
nsXULPrototypeScript* script =
|
2008-10-08 04:41:52 -07:00
|
|
|
static_cast<nsXULPrototypeScript*>(node.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// If given a src= attribute, we must ignore script tag content.
|
2012-08-24 13:22:23 -07:00
|
|
|
if (!script->mSrcURI && !script->GetScriptObject()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocument);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
script->mOutOfLine = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (doc)
|
|
|
|
script->Compile(mText, mTextLength, mDocumentURL,
|
2014-05-27 07:15:00 -07:00
|
|
|
script->mLineNo, doc);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
FlushText(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_ERROR("didn't expect that");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mContextStack.Pop(&mState);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "context stack corrupted");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (mContextStack.Depth() == 0) {
|
|
|
|
// The root element should -always- be an element, because
|
|
|
|
// it'll have been created via XULContentSinkImpl::OpenRoot().
|
|
|
|
NS_ASSERTION(node->mType == nsXULPrototypeNode::eType_Element, "root is not an element");
|
|
|
|
if (node->mType != nsXULPrototypeNode::eType_Element)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
// Now that we're done parsing, set the prototype document's
|
|
|
|
// root element. This transfers ownership of the prototype
|
|
|
|
// element tree to the prototype document.
|
|
|
|
nsXULPrototypeElement* element =
|
2008-10-08 04:41:52 -07:00
|
|
|
static_cast<nsXULPrototypeElement*>(node.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mPrototype->SetRootElement(element);
|
|
|
|
mState = eInEpilog;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 07:02:17 -08:00
|
|
|
XULContentSinkImpl::HandleComment(const char16_t *aName)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
FlushText();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 07:02:17 -08:00
|
|
|
XULContentSinkImpl::HandleCDataSection(const char16_t *aData, uint32_t aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
FlushText();
|
|
|
|
return AddText(aData, aLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
XULContentSinkImpl::HandleDoctypeDecl(const nsAString & aSubset,
|
|
|
|
const nsAString & aName,
|
|
|
|
const nsAString & aSystemId,
|
|
|
|
const nsAString & aPublicId,
|
|
|
|
nsISupports* aCatalogData)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 07:02:17 -08:00
|
|
|
XULContentSinkImpl::HandleCharacterData(const char16_t *aData,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (aData && mState != eInProlog && mState != eInEpilog) {
|
|
|
|
return AddText(aData, aLength);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 07:02:17 -08:00
|
|
|
XULContentSinkImpl::HandleProcessingInstruction(const char16_t *aTarget,
|
|
|
|
const char16_t *aData)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
FlushText();
|
|
|
|
|
|
|
|
const nsDependentString target(aTarget);
|
|
|
|
const nsDependentString data(aData);
|
|
|
|
|
|
|
|
// Note: the created nsXULPrototypePI has mRefCnt == 1
|
2008-10-08 04:41:52 -07:00
|
|
|
nsRefPtr<nsXULPrototypePI> pi = new nsXULPrototypePI();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!pi)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
pi->mTarget = target;
|
|
|
|
pi->mData = data;
|
|
|
|
|
|
|
|
if (mState == eInProlog) {
|
|
|
|
// Note: passing in already addrefed pi
|
|
|
|
return mPrototype->AddProcessingInstruction(pi);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
2012-07-30 07:20:58 -07:00
|
|
|
nsPrototypeArray* children = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = mContextStack.GetTopChildren(&children);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!children->AppendElement(pi)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 07:02:17 -08:00
|
|
|
XULContentSinkImpl::HandleXMLDeclaration(const char16_t *aVersion,
|
|
|
|
const char16_t *aEncoding,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aStandalone)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 07:02:17 -08:00
|
|
|
XULContentSinkImpl::ReportError(const char16_t* aErrorText,
|
|
|
|
const char16_t* aSourceText,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIScriptError *aError,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool *_retval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aError && aSourceText && aErrorText, "Check arguments!!!");
|
|
|
|
|
|
|
|
// The expat driver should report the error.
|
2011-10-17 07:59:28 -07:00
|
|
|
*_retval = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// make sure to empty the context stack so that
|
|
|
|
// <parsererror> could become the root element.
|
2007-11-12 06:47:14 -08:00
|
|
|
mContextStack.Clear();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mState = eInProlog;
|
|
|
|
|
|
|
|
// Clear any buffered-up text we have. It's enough to set the length to 0.
|
|
|
|
// The buffer itself is allocated when we're created and deleted in our
|
|
|
|
// destructor, so don't mess with it.
|
|
|
|
mTextLength = 0;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXULDocument> doc = do_QueryReferent(mDocument);
|
|
|
|
if (doc && !doc->OnDocumentParserError()) {
|
|
|
|
// The overlay was broken. Don't add a messy element to the master doc.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t* noAtts[] = { 0, 0 };
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_NAMED_LITERAL_STRING(errorNs,
|
|
|
|
"http://www.mozilla.org/newlayout/xml/parsererror.xml");
|
|
|
|
|
|
|
|
nsAutoString parsererror(errorNs);
|
2014-01-04 07:02:17 -08:00
|
|
|
parsererror.Append((char16_t)0xFFFF);
|
2007-03-22 10:30:00 -07:00
|
|
|
parsererror.AppendLiteral("parsererror");
|
|
|
|
|
2014-05-30 00:36:53 -07:00
|
|
|
rv = HandleStartElement(parsererror.get(), noAtts, 0, 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
|
2012-05-30 10:43:39 -07:00
|
|
|
rv = HandleCharacterData(aErrorText, NS_strlen(aErrorText));
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
|
|
|
|
nsAutoString sourcetext(errorNs);
|
2014-01-04 07:02:17 -08:00
|
|
|
sourcetext.Append((char16_t)0xFFFF);
|
2007-03-22 10:30:00 -07:00
|
|
|
sourcetext.AppendLiteral("sourcetext");
|
|
|
|
|
2014-05-30 00:36:53 -07:00
|
|
|
rv = HandleStartElement(sourcetext.get(), noAtts, 0, 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
|
2012-05-30 10:43:39 -07:00
|
|
|
rv = HandleCharacterData(aSourceText, NS_strlen(aSourceText));
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
|
|
|
|
rv = HandleEndElement(sourcetext.get());
|
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
|
|
|
|
rv = HandleEndElement(parsererror.get());
|
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-01-04 07:02:17 -08:00
|
|
|
XULContentSinkImpl::OpenRoot(const char16_t** aAttributes,
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint32_t aAttrLen,
|
2014-06-19 19:01:40 -07:00
|
|
|
mozilla::dom::NodeInfo *aNodeInfo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mState == eInProlog, "how'd we get here?");
|
|
|
|
if (mState != eInProlog)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (aNodeInfo->Equals(nsGkAtoms::script, kNameSpaceID_XHTML) ||
|
|
|
|
aNodeInfo->Equals(nsGkAtoms::script, kNameSpaceID_XUL)) {
|
2013-11-27 15:13:11 -08:00
|
|
|
PR_LOG(gContentSinkLog, PR_LOG_ERROR,
|
2007-03-22 10:30:00 -07:00
|
|
|
("xul: script tag not allowed as root content element"));
|
|
|
|
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the element
|
|
|
|
nsXULPrototypeElement* element;
|
|
|
|
rv = CreateElement(aNodeInfo, &element);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
#ifdef PR_LOGGING
|
2013-11-27 15:13:11 -08:00
|
|
|
if (PR_LOG_TEST(gContentSinkLog, PR_LOG_ERROR)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAutoString anodeC;
|
|
|
|
aNodeInfo->GetName(anodeC);
|
2013-11-27 15:13:11 -08:00
|
|
|
PR_LOG(gContentSinkLog, PR_LOG_ERROR,
|
2007-03-22 10:30:00 -07:00
|
|
|
("xul: unable to create element '%s' at line %d",
|
|
|
|
NS_ConvertUTF16toUTF8(anodeC).get(),
|
|
|
|
-1)); // XXX pass in line number
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the element onto the context stack, so that child
|
|
|
|
// containers will hook up to us as their parent.
|
|
|
|
rv = mContextStack.Push(element, mState);
|
|
|
|
if (NS_FAILED(rv)) {
|
2007-05-24 07:10:02 -07:00
|
|
|
element->Release();
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the attributes
|
|
|
|
rv = AddAttributes(aAttributes, aAttrLen, element);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
mState = eInDocumentElement;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-01-04 07:02:17 -08:00
|
|
|
XULContentSinkImpl::OpenTag(const char16_t** aAttributes,
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint32_t aAttrLen,
|
|
|
|
const uint32_t aLineNumber,
|
2014-06-19 19:01:40 -07:00
|
|
|
mozilla::dom::NodeInfo *aNodeInfo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Create the element
|
|
|
|
nsXULPrototypeElement* element;
|
|
|
|
rv = CreateElement(aNodeInfo, &element);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
#ifdef PR_LOGGING
|
2013-11-27 15:13:11 -08:00
|
|
|
if (PR_LOG_TEST(gContentSinkLog, PR_LOG_ERROR)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAutoString anodeC;
|
|
|
|
aNodeInfo->GetName(anodeC);
|
2013-11-27 15:13:11 -08:00
|
|
|
PR_LOG(gContentSinkLog, PR_LOG_ERROR,
|
2007-03-22 10:30:00 -07:00
|
|
|
("xul: unable to create element '%s' at line %d",
|
|
|
|
NS_ConvertUTF16toUTF8(anodeC).get(),
|
|
|
|
aLineNumber));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link this element to its parent.
|
2012-07-30 07:20:58 -07:00
|
|
|
nsPrototypeArray* children = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = mContextStack.GetTopChildren(&children);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete element;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the attributes
|
|
|
|
rv = AddAttributes(aAttributes, aAttrLen, element);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
children->AppendElement(element);
|
|
|
|
|
|
|
|
if (aNodeInfo->Equals(nsGkAtoms::script, kNameSpaceID_XHTML) ||
|
|
|
|
aNodeInfo->Equals(nsGkAtoms::script, kNameSpaceID_XUL)) {
|
2012-04-05 12:20:44 -07:00
|
|
|
// Do scripty things now
|
2007-12-02 14:27:11 -08:00
|
|
|
rv = OpenScript(aAttributes, aLineNumber);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ASSERTION(mState == eInScript || mState == eInDocumentElement,
|
|
|
|
"Unexpected state");
|
2007-12-02 16:00:26 -08:00
|
|
|
if (mState == eInScript) {
|
2007-12-02 14:27:11 -08:00
|
|
|
// OpenScript has pushed the nsPrototypeScriptElement onto the
|
|
|
|
// stack, so we're done.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push the element onto the context stack, so that child
|
|
|
|
// containers will hook up to us as their parent.
|
|
|
|
rv = mContextStack.Push(element, mState);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
mState = eInDocumentElement;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-01-04 07:02:17 -08:00
|
|
|
XULContentSinkImpl::OpenScript(const char16_t** aAttributes,
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint32_t aLineNumber)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t langID = nsIProgrammingLanguage::JAVASCRIPT;
|
2013-02-08 03:49:28 -08:00
|
|
|
uint32_t version = JSVERSION_LATEST;
|
2012-04-24 15:31:28 -07:00
|
|
|
nsresult rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Look for SRC attribute and look for a LANGUAGE attribute
|
|
|
|
nsAutoString src;
|
|
|
|
while (*aAttributes) {
|
|
|
|
const nsDependentString key(aAttributes[0]);
|
|
|
|
if (key.EqualsLiteral("src")) {
|
|
|
|
src.Assign(aAttributes[1]);
|
|
|
|
}
|
|
|
|
else if (key.EqualsLiteral("type")) {
|
2008-12-16 18:11:07 -08:00
|
|
|
nsDependentString str(aAttributes[1]);
|
|
|
|
nsContentTypeParser parser(str);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAutoString mimeType;
|
2008-12-16 18:11:07 -08:00
|
|
|
rv = parser.GetType(mimeType);
|
2007-12-02 14:27:11 -08:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (rv == NS_ERROR_INVALID_ARG) {
|
|
|
|
// Might as well bail out now instead of setting langID to
|
|
|
|
// nsIProgrammingLanguage::UNKNOWN and bailing out later.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// We do want the warning here
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-04 00:43:59 -07:00
|
|
|
if (nsContentUtils::IsJavascriptMIMEType(mimeType)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
langID = nsIProgrammingLanguage::JAVASCRIPT;
|
2007-06-07 15:50:12 -07:00
|
|
|
version = JSVERSION_LATEST;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2012-05-09 07:24:24 -07:00
|
|
|
langID = nsIProgrammingLanguage::UNKNOWN;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (langID != nsIProgrammingLanguage::UNKNOWN) {
|
2013-01-24 21:24:57 -08:00
|
|
|
// Get the version string, and ensure the language supports it.
|
|
|
|
nsAutoString versionName;
|
|
|
|
rv = parser.GetParameter("version", versionName);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
version = nsContentUtils::ParseJavascriptVersion(versionName);
|
|
|
|
} else if (rv != NS_ERROR_INVALID_ARG) {
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (key.EqualsLiteral("language")) {
|
|
|
|
// Language is deprecated, and the impl in nsScriptLoader ignores the
|
|
|
|
// various version strings anyway. So we make no attempt to support
|
|
|
|
// languages other than JS for language=
|
|
|
|
nsAutoString lang(aAttributes[1]);
|
2013-02-04 13:37:21 -08:00
|
|
|
if (nsContentUtils::IsJavaScriptLanguage(lang)) {
|
|
|
|
version = JSVERSION_DEFAULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
langID = nsIProgrammingLanguage::JAVASCRIPT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aAttributes += 2;
|
|
|
|
}
|
2012-04-24 15:31:28 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Not all script languages have a "sandbox" concept. At time of
|
|
|
|
// writing, Python is the only other language, and it does not.
|
|
|
|
// For such languages, neither any inline script nor remote script are
|
|
|
|
// safe to execute from untrusted sources.
|
|
|
|
// So for such languages, we only allow script when the document
|
|
|
|
// itself is from chrome. We then don't bother to check the
|
|
|
|
// "src=" tag - we trust chrome to do the right thing.
|
|
|
|
// (See also similar code in nsScriptLoader.cpp)
|
|
|
|
nsCOMPtr<nsIDocument> doc(do_QueryReferent(mDocument));
|
|
|
|
if (langID != nsIProgrammingLanguage::UNKNOWN &&
|
|
|
|
langID != nsIProgrammingLanguage::JAVASCRIPT &&
|
|
|
|
doc && !nsContentUtils::IsChromeDoc(doc)) {
|
|
|
|
langID = nsIProgrammingLanguage::UNKNOWN;
|
|
|
|
NS_WARNING("Non JS language called from non chrome - ignored");
|
|
|
|
}
|
2012-04-24 15:31:28 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Don't process scripts that aren't known
|
|
|
|
if (langID != nsIProgrammingLanguage::UNKNOWN) {
|
2013-06-21 07:25:20 -07:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> globalObject;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (doc)
|
2013-06-21 07:25:20 -07:00
|
|
|
globalObject = do_QueryInterface(doc->GetWindow());
|
2008-10-08 04:41:52 -07:00
|
|
|
nsRefPtr<nsXULPrototypeScript> script =
|
2012-04-24 15:31:28 -07:00
|
|
|
new nsXULPrototypeScript(aLineNumber, version);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (! script)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// If there is a SRC attribute...
|
|
|
|
if (! src.IsEmpty()) {
|
|
|
|
// Use the SRC attribute value to load the URL
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = NS_NewURI(getter_AddRefs(script->mSrcURI), src, nullptr, mDocumentURL);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Check if this document is allowed to load a script from this source
|
|
|
|
// NOTE: if we ever allow scripts added via the DOM to run, we need to
|
|
|
|
// add a CheckLoadURI call for that as well.
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (!mSecMan)
|
|
|
|
mSecMan = do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocument, &rv);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = mSecMan->
|
|
|
|
CheckLoadURIWithPrincipal(doc->NodePrincipal(),
|
|
|
|
script->mSrcURI,
|
|
|
|
nsIScriptSecurityManager::ALLOW_CHROME);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to deserialize an out-of-line script from the FastLoad
|
|
|
|
// file right away. Otherwise we'll end up reloading the script and
|
|
|
|
// corrupting the FastLoad file trying to serialize it, in the case
|
|
|
|
// where it's already there.
|
2013-08-19 16:24:27 -07:00
|
|
|
script->DeserializeOutOfLine(nullptr, mPrototype);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
nsPrototypeArray* children = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = mContextStack.GetTopChildren(&children);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
children->AppendElement(script);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mConstrainSize = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mContextStack.Push(script, mState);
|
|
|
|
mState = eInScript;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-01-04 07:02:17 -08:00
|
|
|
XULContentSinkImpl::AddAttributes(const char16_t** aAttributes,
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint32_t aAttrLen,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsXULPrototypeElement* aElement)
|
|
|
|
{
|
|
|
|
// Add tag attributes to the element
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Create storage for the attributes
|
2012-07-30 07:20:58 -07:00
|
|
|
nsXULPrototypeAttribute* attrs = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aAttrLen > 0) {
|
|
|
|
attrs = new nsXULPrototypeAttribute[aAttrLen];
|
|
|
|
if (! attrs)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
aElement->mAttributes = attrs;
|
|
|
|
aElement->mNumAttributes = aAttrLen;
|
|
|
|
|
|
|
|
// Copy the attributes into the prototype
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < aAttrLen; ++i) {
|
|
|
|
rv = NormalizeAttributeString(aAttributes[i * 2], attrs[i].mName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aElement->SetAttrAt(i, nsDependentString(aAttributes[i * 2 + 1]),
|
|
|
|
mDocumentURL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2013-11-27 15:13:11 -08:00
|
|
|
if (PR_LOG_TEST(gContentSinkLog, PR_LOG_DEBUG)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAutoString extraWhiteSpace;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t cnt = mContextStack.Depth();
|
2007-03-22 10:30:00 -07:00
|
|
|
while (--cnt >= 0)
|
|
|
|
extraWhiteSpace.AppendLiteral(" ");
|
|
|
|
nsAutoString qnameC,valueC;
|
|
|
|
qnameC.Assign(aAttributes[0]);
|
|
|
|
valueC.Assign(aAttributes[1]);
|
2013-11-27 15:13:11 -08:00
|
|
|
PR_LOG(gContentSinkLog, PR_LOG_DEBUG,
|
2007-03-22 10:30:00 -07:00
|
|
|
("xul: %.5d. %s %s=%s",
|
|
|
|
-1, // XXX pass in line number
|
|
|
|
NS_ConvertUTF16toUTF8(extraWhiteSpace).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(qnameC).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(valueC).get()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-01-04 07:02:17 -08:00
|
|
|
XULContentSinkImpl::AddText(const char16_t* aText,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Create buffer when we first need it
|
|
|
|
if (0 == mTextSize) {
|
2014-01-04 07:02:17 -08:00
|
|
|
mText = (char16_t *) moz_malloc(sizeof(char16_t) * 4096);
|
2012-07-30 07:20:58 -07:00
|
|
|
if (nullptr == mText) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
mTextSize = 4096;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy data from string into our buffer; flush buffer when it fills up
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t offset = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
while (0 != aLength) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t amount = mTextSize - mTextLength;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (amount > aLength) {
|
|
|
|
amount = aLength;
|
|
|
|
}
|
|
|
|
if (0 == amount) {
|
|
|
|
if (mConstrainSize) {
|
|
|
|
nsresult rv = FlushText();
|
|
|
|
if (NS_OK != rv) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mTextSize += aLength;
|
2014-01-04 07:02:17 -08:00
|
|
|
mText = (char16_t *) moz_realloc(mText, sizeof(char16_t) * mTextSize);
|
2012-07-30 07:20:58 -07:00
|
|
|
if (nullptr == mText) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-04 07:02:17 -08:00
|
|
|
memcpy(&mText[mTextLength],aText + offset, sizeof(char16_t) * amount);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mTextLength += amount;
|
|
|
|
offset += amount;
|
|
|
|
aLength -= amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|