1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/Element.h"
8 #include "mozilla/dom/SVGStyleElement.h"
9 #include "nsContentUtils.h"
10 #include "mozilla/dom/SVGStyleElementBinding.h"
12 NS_IMPL_NS_NEW_NAMESPACED_SVG_ELEMENT(Style
)
18 SVGStyleElement::WrapNode(JSContext
*aCx
, JS::Handle
<JSObject
*> aGivenProto
)
20 return SVGStyleElementBinding::Wrap(aCx
, this, aGivenProto
);
23 //----------------------------------------------------------------------
24 // nsISupports methods
26 NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(SVGStyleElement
,
28 nsIStyleSheetLinkingElement
,
31 NS_IMPL_CYCLE_COLLECTION_CLASS(SVGStyleElement
)
33 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(SVGStyleElement
,
35 tmp
->nsStyleLinkElement::Traverse(cb
);
36 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
38 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(SVGStyleElement
,
40 tmp
->nsStyleLinkElement::Unlink();
41 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
43 //----------------------------------------------------------------------
46 SVGStyleElement::SVGStyleElement(already_AddRefed
<mozilla::dom::NodeInfo
>& aNodeInfo
)
47 : SVGStyleElementBase(aNodeInfo
)
49 AddMutationObserver(this);
52 SVGStyleElement::~SVGStyleElement()
56 //----------------------------------------------------------------------
60 NS_IMPL_ELEMENT_CLONE_WITH_INIT(SVGStyleElement
)
63 //----------------------------------------------------------------------
67 SVGStyleElement::BindToTree(nsIDocument
* aDocument
, nsIContent
* aParent
,
68 nsIContent
* aBindingParent
,
69 bool aCompileEventHandlers
)
71 nsresult rv
= SVGStyleElementBase::BindToTree(aDocument
, aParent
,
73 aCompileEventHandlers
);
74 NS_ENSURE_SUCCESS(rv
, rv
);
76 void (SVGStyleElement::*update
)() = &SVGStyleElement::UpdateStyleSheetInternal
;
77 nsContentUtils::AddScriptRunner(
78 NewRunnableMethod("dom::SVGStyleElement::BindToTree", this, update
));
84 SVGStyleElement::UnbindFromTree(bool aDeep
, bool aNullParent
)
86 nsCOMPtr
<nsIDocument
> oldDoc
= GetUncomposedDoc();
87 ShadowRoot
* oldShadow
= GetContainingShadow();
88 SVGStyleElementBase::UnbindFromTree(aDeep
, aNullParent
);
89 UpdateStyleSheetInternal(oldDoc
, oldShadow
);
93 SVGStyleElement::AfterSetAttr(int32_t aNameSpaceID
, nsAtom
* aName
,
94 const nsAttrValue
* aValue
,
95 const nsAttrValue
* aOldValue
,
96 nsIPrincipal
* aMaybeScriptedPrincipal
,
99 if (aNameSpaceID
== kNameSpaceID_None
) {
100 if (aName
== nsGkAtoms::title
||
101 aName
== nsGkAtoms::media
||
102 aName
== nsGkAtoms::type
) {
103 UpdateStyleSheetInternal(nullptr, nullptr, true);
107 return SVGStyleElementBase::AfterSetAttr(aNameSpaceID
, aName
, aValue
,
108 aOldValue
, aMaybeScriptedPrincipal
,
113 SVGStyleElement::ParseAttribute(int32_t aNamespaceID
,
115 const nsAString
& aValue
,
116 nsIPrincipal
* aMaybeScriptedPrincipal
,
117 nsAttrValue
& aResult
)
119 if (aNamespaceID
== kNameSpaceID_None
&&
120 aAttribute
== nsGkAtoms::crossorigin
) {
121 ParseCORSValue(aValue
, aResult
);
125 return SVGStyleElementBase::ParseAttribute(aNamespaceID
, aAttribute
, aValue
,
126 aMaybeScriptedPrincipal
, aResult
);
129 //----------------------------------------------------------------------
130 // nsIMutationObserver methods
133 SVGStyleElement::CharacterDataChanged(nsIContent
* aContent
,
134 const CharacterDataChangeInfo
&)
136 ContentChanged(aContent
);
140 SVGStyleElement::ContentAppended(nsIContent
* aFirstNewContent
)
142 ContentChanged(aFirstNewContent
->GetParent());
146 SVGStyleElement::ContentInserted(nsIContent
* aChild
)
148 ContentChanged(aChild
);
152 SVGStyleElement::ContentRemoved(nsIContent
* aChild
,
153 nsIContent
* aPreviousSibling
)
155 ContentChanged(aChild
);
159 SVGStyleElement::ContentChanged(nsIContent
* aContent
)
161 if (nsContentUtils::IsInSameAnonymousTree(this, aContent
)) {
162 UpdateStyleSheetInternal(nullptr, nullptr);
166 //----------------------------------------------------------------------
169 SVGStyleElement::GetXmlspace(nsAString
& aXmlspace
)
171 GetAttr(kNameSpaceID_XML
, nsGkAtoms::space
, aXmlspace
);
175 SVGStyleElement::SetXmlspace(const nsAString
& aXmlspace
, ErrorResult
& rv
)
177 rv
= SetAttr(kNameSpaceID_XML
, nsGkAtoms::space
, aXmlspace
, true);
181 SVGStyleElement::GetMedia(nsAString
& aMedia
)
183 GetAttr(kNameSpaceID_None
, nsGkAtoms::media
, aMedia
);
187 SVGStyleElement::SetMedia(const nsAString
& aMedia
, ErrorResult
& rv
)
189 rv
= SetAttr(kNameSpaceID_None
, nsGkAtoms::media
, aMedia
, true);
193 SVGStyleElement::GetType(nsAString
& aType
)
195 GetAttr(kNameSpaceID_None
, nsGkAtoms::type
, aType
);
199 SVGStyleElement::SetType(const nsAString
& aType
, ErrorResult
& rv
)
201 rv
= SetAttr(kNameSpaceID_None
, nsGkAtoms::type
, aType
, true);
205 SVGStyleElement::GetTitle(nsAString
& aTitle
)
207 GetAttr(kNameSpaceID_None
, nsGkAtoms::title
, aTitle
);
211 SVGStyleElement::SetTitle(const nsAString
& aTitle
, ErrorResult
& rv
)
213 rv
= SetAttr(kNameSpaceID_None
, nsGkAtoms::title
, aTitle
, true);
216 //----------------------------------------------------------------------
217 // nsStyleLinkElement methods
219 already_AddRefed
<nsIURI
>
220 SVGStyleElement::GetStyleSheetURL(bool* aIsInline
, nsIPrincipal
** aTriggeringPrincipal
)
223 *aTriggeringPrincipal
= nullptr;
228 SVGStyleElement::GetStyleSheetInfo(nsAString
& aTitle
,
233 *aIsAlternate
= false;
236 GetAttr(kNameSpaceID_None
, nsGkAtoms::title
, title
);
237 title
.CompressWhitespace();
238 aTitle
.Assign(title
);
240 GetAttr(kNameSpaceID_None
, nsGkAtoms::media
, aMedia
);
241 // The SVG spec is formulated in terms of the CSS2 spec,
242 // which specifies that media queries are case insensitive.
243 nsContentUtils::ASCIIToLower(aMedia
);
245 GetAttr(kNameSpaceID_None
, nsGkAtoms::type
, aType
);
246 if (aType
.IsEmpty()) {
247 aType
.AssignLiteral("text/css");
252 SVGStyleElement::GetCORSMode() const
254 return AttrValueToCORSMode(GetParsedAttr(nsGkAtoms::crossorigin
));
258 } // namespace mozilla