2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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
|
|
|
|
|
|
|
#include "nsIDOMLinkStyle.h"
|
|
|
|
#include "nsIDOMStyleSheet.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIStyleSheet.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsStyleLinkElement.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsXMLProcessingInstruction.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsGkAtoms.h"
|
2009-05-20 01:00:04 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2011-08-11 06:29:50 -07:00
|
|
|
#include "nsContentUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-09 05:31:24 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
class nsXMLStylesheetPI : public nsXMLProcessingInstruction,
|
|
|
|
public nsStyleLinkElement
|
|
|
|
{
|
|
|
|
public:
|
2010-07-23 02:49:57 -07:00
|
|
|
nsXMLStylesheetPI(already_AddRefed<nsINodeInfo> aNodeInfo, const nsAString& aData);
|
2007-03-22 10:30:00 -07:00
|
|
|
virtual ~nsXMLStylesheetPI();
|
|
|
|
|
|
|
|
// nsISupports
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2012-10-07 19:39:09 -07:00
|
|
|
// CC
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsXMLStylesheetPI,
|
|
|
|
nsXMLProcessingInstruction)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// nsIDOMNode
|
2012-10-09 05:31:24 -07:00
|
|
|
virtual void SetNodeValueInternal(const nsAString& aNodeValue,
|
|
|
|
mozilla::ErrorResult& aError);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// nsIContent
|
|
|
|
virtual nsresult BindToTree(nsIDocument* aDocument, nsIContent* aParent,
|
|
|
|
nsIContent* aBindingParent,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aCompileEventHandlers);
|
|
|
|
virtual void UnbindFromTree(bool aDeep = true,
|
|
|
|
bool aNullParent = true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// nsIStyleSheetLinkingElement
|
|
|
|
virtual void OverrideBaseURI(nsIURI* aNewBaseURI);
|
|
|
|
|
|
|
|
// nsStyleLinkElement
|
|
|
|
NS_IMETHOD GetCharset(nsAString& aCharset);
|
|
|
|
|
2010-07-23 02:49:57 -07:00
|
|
|
virtual nsXPCClassInfo* GetClassInfo();
|
2007-03-22 10:30:00 -07:00
|
|
|
protected:
|
|
|
|
nsCOMPtr<nsIURI> mOverriddenBaseURI;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
already_AddRefed<nsIURI> GetStyleSheetURL(bool* aIsInline);
|
2007-03-22 10:30:00 -07:00
|
|
|
void GetStyleSheetInfo(nsAString& aTitle,
|
|
|
|
nsAString& aType,
|
|
|
|
nsAString& aMedia,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* aIsAlternate);
|
2007-03-22 10:30:00 -07:00
|
|
|
virtual nsGenericDOMDataNode* CloneDataNode(nsINodeInfo *aNodeInfo,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aCloneText) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// nsISupports implementation
|
|
|
|
|
2010-07-23 02:49:57 -07:00
|
|
|
DOMCI_NODE_DATA(XMLStylesheetProcessingInstruction, nsXMLStylesheetPI)
|
2010-01-12 05:08:43 -08:00
|
|
|
|
2012-10-07 19:39:09 -07:00
|
|
|
NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(nsXMLStylesheetPI)
|
2008-11-03 02:31:47 -08:00
|
|
|
NS_NODE_INTERFACE_TABLE4(nsXMLStylesheetPI, nsIDOMNode,
|
|
|
|
nsIDOMProcessingInstruction, nsIDOMLinkStyle,
|
|
|
|
nsIStyleSheetLinkingElement)
|
2010-03-17 08:09:05 -07:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(XMLStylesheetProcessingInstruction)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsXMLProcessingInstruction)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(nsXMLStylesheetPI, nsXMLProcessingInstruction)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsXMLStylesheetPI, nsXMLProcessingInstruction)
|
|
|
|
|
2012-10-07 19:39:09 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsXMLStylesheetPI)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsXMLStylesheetPI,
|
|
|
|
nsXMLProcessingInstruction)
|
|
|
|
tmp->nsStyleLinkElement::Traverse(cb);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsXMLStylesheetPI,
|
|
|
|
nsXMLProcessingInstruction)
|
|
|
|
tmp->nsStyleLinkElement::Unlink();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-23 02:49:57 -07:00
|
|
|
nsXMLStylesheetPI::nsXMLStylesheetPI(already_AddRefed<nsINodeInfo> aNodeInfo,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsAString& aData)
|
2011-06-14 00:56:49 -07:00
|
|
|
: nsXMLProcessingInstruction(aNodeInfo, aData)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXMLStylesheetPI::~nsXMLStylesheetPI()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIContent
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsXMLStylesheetPI::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
|
|
|
|
nsIContent* aBindingParent,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aCompileEventHandlers)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv = nsXMLProcessingInstruction::BindToTree(aDocument, aParent,
|
|
|
|
aBindingParent,
|
|
|
|
aCompileEventHandlers);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-04-20 16:21:35 -07:00
|
|
|
void (nsXMLStylesheetPI::*update)() = &nsXMLStylesheetPI::UpdateStyleSheetInternal;
|
|
|
|
nsContentUtils::AddScriptRunner(NS_NewRunnableMethod(this, update));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
nsXMLStylesheetPI::UnbindFromTree(bool aDeep, bool aNullParent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocument> oldDoc = GetCurrentDoc();
|
|
|
|
|
|
|
|
nsXMLProcessingInstruction::UnbindFromTree(aDeep, aNullParent);
|
2007-04-20 15:59:18 -07:00
|
|
|
UpdateStyleSheetInternal(oldDoc);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsIDOMNode
|
|
|
|
|
2012-10-09 05:31:24 -07:00
|
|
|
void
|
|
|
|
nsXMLStylesheetPI::SetNodeValueInternal(const nsAString& aNodeValue,
|
|
|
|
ErrorResult& aError)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-10-21 00:38:41 -07:00
|
|
|
nsGenericDOMDataNode::SetNodeValueInternal(aNodeValue, aError);
|
2012-10-09 05:31:24 -07:00
|
|
|
if (!aError.Failed()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
UpdateStyleSheetInternal(nullptr, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsStyleLinkElement
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsXMLStylesheetPI::GetCharset(nsAString& aCharset)
|
|
|
|
{
|
|
|
|
return GetAttrValue(nsGkAtoms::charset, aCharset) ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ void
|
|
|
|
nsXMLStylesheetPI::OverrideBaseURI(nsIURI* aNewBaseURI)
|
|
|
|
{
|
|
|
|
mOverriddenBaseURI = aNewBaseURI;
|
|
|
|
}
|
|
|
|
|
2009-07-28 09:07:31 -07:00
|
|
|
already_AddRefed<nsIURI>
|
2011-09-28 23:19:26 -07:00
|
|
|
nsXMLStylesheetPI::GetStyleSheetURL(bool* aIsInline)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aIsInline = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsAutoString href;
|
2008-11-28 13:21:55 -08:00
|
|
|
if (!GetAttrValue(nsGkAtoms::href, href)) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIURI *baseURL;
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString charset;
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument *document = OwnerDoc();
|
2011-10-18 04:19:44 -07:00
|
|
|
baseURL = mOverriddenBaseURI ?
|
|
|
|
mOverriddenBaseURI.get() :
|
|
|
|
document->GetDocBaseURI();
|
|
|
|
charset = document->GetDocumentCharacterSet();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-28 09:07:31 -07:00
|
|
|
nsCOMPtr<nsIURI> aURI;
|
|
|
|
NS_NewURI(getter_AddRefs(aURI), href, charset.get(), baseURL);
|
|
|
|
return aURI.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXMLStylesheetPI::GetStyleSheetInfo(nsAString& aTitle,
|
|
|
|
nsAString& aType,
|
|
|
|
nsAString& aMedia,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* aIsAlternate)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
aTitle.Truncate();
|
|
|
|
aType.Truncate();
|
|
|
|
aMedia.Truncate();
|
2011-10-17 07:59:28 -07:00
|
|
|
*aIsAlternate = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// xml-stylesheet PI is special only in prolog
|
|
|
|
if (!nsContentUtils::InProlog(this)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString data;
|
|
|
|
GetData(data);
|
|
|
|
|
2012-02-27 03:57:48 -08:00
|
|
|
nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::title, aTitle);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsAutoString alternate;
|
2012-02-27 03:57:48 -08:00
|
|
|
nsContentUtils::GetPseudoAttributeValue(data,
|
|
|
|
nsGkAtoms::alternate,
|
|
|
|
alternate);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// if alternate, does it have title?
|
|
|
|
if (alternate.EqualsLiteral("yes")) {
|
|
|
|
if (aTitle.IsEmpty()) { // alternates must have title
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*aIsAlternate = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-27 03:57:48 -08:00
|
|
|
nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::media, aMedia);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsAutoString type;
|
2012-02-27 03:57:48 -08:00
|
|
|
nsContentUtils::GetPseudoAttributeValue(data, nsGkAtoms::type, type);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsAutoString mimeType, notUsed;
|
2012-02-27 03:57:48 -08:00
|
|
|
nsContentUtils::SplitMimeType(type, mimeType, notUsed);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mimeType.IsEmpty() && !mimeType.LowerCaseEqualsLiteral("text/css")) {
|
|
|
|
aType.Assign(type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get here we assume that we're loading a css file, so set the
|
|
|
|
// type to 'text/css'
|
|
|
|
aType.AssignLiteral("text/css");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGenericDOMDataNode*
|
2011-09-28 23:19:26 -07:00
|
|
|
nsXMLStylesheetPI::CloneDataNode(nsINodeInfo *aNodeInfo, bool aCloneText) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsAutoString data;
|
|
|
|
nsGenericDOMDataNode::GetData(data);
|
2010-07-23 02:49:57 -07:00
|
|
|
nsCOMPtr<nsINodeInfo> ni = aNodeInfo;
|
|
|
|
return new nsXMLStylesheetPI(ni.forget(), data);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewXMLStylesheetProcessingInstruction(nsIContent** aInstancePtrResult,
|
|
|
|
nsNodeInfoManager *aNodeInfoManager,
|
|
|
|
const nsAString& aData)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aNodeInfoManager, "Missing nodeinfo manager");
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
*aInstancePtrResult = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsINodeInfo> ni;
|
2008-09-12 15:32:18 -07:00
|
|
|
ni = aNodeInfoManager->GetNodeInfo(nsGkAtoms::processingInstructionTagName,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, kNameSpaceID_None,
|
2011-06-14 00:56:49 -07:00
|
|
|
nsIDOMNode::PROCESSING_INSTRUCTION_NODE,
|
|
|
|
nsGkAtoms::xml_stylesheet);
|
2008-09-25 15:46:52 -07:00
|
|
|
NS_ENSURE_TRUE(ni, NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-23 02:49:57 -07:00
|
|
|
nsXMLStylesheetPI *instance = new nsXMLStylesheetPI(ni.forget(), aData);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!instance) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(*aInstancePtrResult = instance);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|