/src/mozilla-central/dom/html/HTMLStyleElement.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | #include "mozilla/dom/HTMLStyleElement.h" |
7 | | #include "mozilla/dom/HTMLStyleElementBinding.h" |
8 | | #include "nsGkAtoms.h" |
9 | | #include "nsStyleConsts.h" |
10 | | #include "nsIDocument.h" |
11 | | #include "nsUnicharUtils.h" |
12 | | #include "nsThreadUtils.h" |
13 | | #include "nsContentUtils.h" |
14 | | #include "nsStubMutationObserver.h" |
15 | | |
16 | | NS_IMPL_NS_NEW_HTML_ELEMENT(Style) |
17 | | |
18 | | namespace mozilla { |
19 | | namespace dom { |
20 | | |
21 | | HTMLStyleElement::HTMLStyleElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo) |
22 | | : nsGenericHTMLElement(std::move(aNodeInfo)) |
23 | 0 | { |
24 | 0 | AddMutationObserver(this); |
25 | 0 | } |
26 | | |
27 | | HTMLStyleElement::~HTMLStyleElement() |
28 | 0 | { |
29 | 0 | } |
30 | | |
31 | | NS_IMPL_CYCLE_COLLECTION_CLASS(HTMLStyleElement) |
32 | | |
33 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(HTMLStyleElement, |
34 | 0 | nsGenericHTMLElement) |
35 | 0 | tmp->nsStyleLinkElement::Traverse(cb); |
36 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
37 | | |
38 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(HTMLStyleElement, |
39 | 0 | nsGenericHTMLElement) |
40 | 0 | tmp->nsStyleLinkElement::Unlink(); |
41 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
42 | | |
43 | | NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(HTMLStyleElement, |
44 | | nsGenericHTMLElement, |
45 | | nsIStyleSheetLinkingElement, |
46 | | nsIMutationObserver) |
47 | | |
48 | | NS_IMPL_ELEMENT_CLONE(HTMLStyleElement) |
49 | | |
50 | | |
51 | | bool |
52 | | HTMLStyleElement::Disabled() |
53 | 0 | { |
54 | 0 | StyleSheet* ss = GetSheet(); |
55 | 0 | return ss && ss->Disabled(); |
56 | 0 | } |
57 | | |
58 | | void |
59 | | HTMLStyleElement::SetDisabled(bool aDisabled) |
60 | 0 | { |
61 | 0 | if (StyleSheet* ss = GetSheet()) { |
62 | 0 | ss->SetDisabled(aDisabled); |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | | void |
67 | | HTMLStyleElement::CharacterDataChanged(nsIContent* aContent, |
68 | | const CharacterDataChangeInfo&) |
69 | 0 | { |
70 | 0 | ContentChanged(aContent); |
71 | 0 | } |
72 | | |
73 | | void |
74 | | HTMLStyleElement::ContentAppended(nsIContent* aFirstNewContent) |
75 | 0 | { |
76 | 0 | ContentChanged(aFirstNewContent->GetParent()); |
77 | 0 | } |
78 | | |
79 | | void |
80 | | HTMLStyleElement::ContentInserted(nsIContent* aChild) |
81 | 0 | { |
82 | 0 | ContentChanged(aChild); |
83 | 0 | } |
84 | | |
85 | | void |
86 | | HTMLStyleElement::ContentRemoved(nsIContent* aChild, |
87 | | nsIContent* aPreviousSibling) |
88 | 0 | { |
89 | 0 | ContentChanged(aChild); |
90 | 0 | } |
91 | | |
92 | | void |
93 | | HTMLStyleElement::ContentChanged(nsIContent* aContent) |
94 | 0 | { |
95 | 0 | mTriggeringPrincipal = nullptr; |
96 | 0 | if (nsContentUtils::IsInSameAnonymousTree(this, aContent)) { |
97 | 0 | Unused << UpdateStyleSheetInternal(nullptr, nullptr); |
98 | 0 | } |
99 | 0 | } |
100 | | |
101 | | nsresult |
102 | | HTMLStyleElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent, |
103 | | nsIContent* aBindingParent) |
104 | 0 | { |
105 | 0 | nsresult rv = nsGenericHTMLElement::BindToTree(aDocument, aParent, |
106 | 0 | aBindingParent); |
107 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
108 | 0 |
|
109 | 0 | void (HTMLStyleElement::*update)() = &HTMLStyleElement::UpdateStyleSheetInternal; |
110 | 0 | nsContentUtils::AddScriptRunner( |
111 | 0 | NewRunnableMethod("dom::HTMLStyleElement::BindToTree", this, update)); |
112 | 0 |
|
113 | 0 | return rv; |
114 | 0 | } |
115 | | |
116 | | void |
117 | | HTMLStyleElement::UnbindFromTree(bool aDeep, bool aNullParent) |
118 | 0 | { |
119 | 0 | nsCOMPtr<nsIDocument> oldDoc = GetUncomposedDoc(); |
120 | 0 | ShadowRoot* oldShadow = GetContainingShadow(); |
121 | 0 |
|
122 | 0 | nsGenericHTMLElement::UnbindFromTree(aDeep, aNullParent); |
123 | 0 |
|
124 | 0 | if (oldShadow && GetContainingShadow()) { |
125 | 0 | // The style is in a shadow tree and is still in the |
126 | 0 | // shadow tree. Thus the sheets in the shadow DOM |
127 | 0 | // do not need to be updated. |
128 | 0 | return; |
129 | 0 | } |
130 | 0 | |
131 | 0 | Unused << UpdateStyleSheetInternal(oldDoc, oldShadow); |
132 | 0 | } |
133 | | |
134 | | nsresult |
135 | | HTMLStyleElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName, |
136 | | const nsAttrValue* aValue, |
137 | | const nsAttrValue* aOldValue, |
138 | | nsIPrincipal* aSubjectPrincipal, |
139 | | bool aNotify) |
140 | 0 | { |
141 | 0 | if (aNameSpaceID == kNameSpaceID_None) { |
142 | 0 | if (aName == nsGkAtoms::title || |
143 | 0 | aName == nsGkAtoms::media || |
144 | 0 | aName == nsGkAtoms::type) { |
145 | 0 | Unused << UpdateStyleSheetInternal(nullptr, nullptr, ForceUpdate::Yes); |
146 | 0 | } |
147 | 0 | } |
148 | 0 |
|
149 | 0 | return nsGenericHTMLElement::AfterSetAttr(aNameSpaceID, aName, aValue, |
150 | 0 | aOldValue, aSubjectPrincipal, aNotify); |
151 | 0 | } |
152 | | |
153 | | void |
154 | | HTMLStyleElement::GetInnerHTML(nsAString& aInnerHTML, OOMReporter& aError) |
155 | 0 | { |
156 | 0 | if (!nsContentUtils::GetNodeTextContent(this, false, aInnerHTML, fallible)) { |
157 | 0 | aError.ReportOOM(); |
158 | 0 | } |
159 | 0 | } |
160 | | |
161 | | void |
162 | | HTMLStyleElement::SetInnerHTML(const nsAString& aInnerHTML, |
163 | | nsIPrincipal* aScriptedPrincipal, |
164 | | ErrorResult& aError) |
165 | 0 | { |
166 | 0 | SetTextContentInternal(aInnerHTML, aScriptedPrincipal, aError); |
167 | 0 | } |
168 | | |
169 | | void |
170 | | HTMLStyleElement::SetTextContentInternal(const nsAString& aTextContent, |
171 | | nsIPrincipal* aScriptedPrincipal, |
172 | | ErrorResult& aError) |
173 | 0 | { |
174 | 0 | // Per spec, if we're setting text content to an empty string and don't |
175 | 0 | // already have any children, we should not trigger any mutation observers, or |
176 | 0 | // re-parse the stylesheet. |
177 | 0 | if (aTextContent.IsEmpty() && !GetFirstChild()) { |
178 | 0 | nsIPrincipal* principal = mTriggeringPrincipal ? mTriggeringPrincipal.get() : NodePrincipal(); |
179 | 0 | if (principal == aScriptedPrincipal) { |
180 | 0 | return; |
181 | 0 | } |
182 | 0 | } |
183 | 0 | |
184 | 0 | SetEnableUpdates(false); |
185 | 0 |
|
186 | 0 | aError = nsContentUtils::SetNodeTextContent(this, aTextContent, true); |
187 | 0 |
|
188 | 0 | SetEnableUpdates(true); |
189 | 0 |
|
190 | 0 | mTriggeringPrincipal = aScriptedPrincipal; |
191 | 0 |
|
192 | 0 | Unused << UpdateStyleSheetInternal(nullptr, nullptr); |
193 | 0 | } |
194 | | |
195 | | Maybe<nsStyleLinkElement::SheetInfo> |
196 | | HTMLStyleElement::GetStyleSheetInfo() |
197 | 0 | { |
198 | 0 | if (!IsCSSMimeTypeAttribute(*this)) { |
199 | 0 | return Nothing(); |
200 | 0 | } |
201 | 0 | |
202 | 0 | nsAutoString title; |
203 | 0 | nsAutoString media; |
204 | 0 | GetTitleAndMediaForElement(*this, title, media); |
205 | 0 |
|
206 | 0 | nsCOMPtr<nsIPrincipal> prin = mTriggeringPrincipal; |
207 | 0 | return Some(SheetInfo { |
208 | 0 | *OwnerDoc(), |
209 | 0 | this, |
210 | 0 | nullptr, |
211 | 0 | prin.forget(), |
212 | 0 | net::ReferrerPolicy::RP_Unset, |
213 | 0 | CORS_NONE, |
214 | 0 | title, |
215 | 0 | media, |
216 | 0 | HasAlternateRel::No, |
217 | 0 | IsInline::Yes, |
218 | 0 | }); |
219 | 0 | } |
220 | | |
221 | | JSObject* |
222 | | HTMLStyleElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto) |
223 | 0 | { |
224 | 0 | return HTMLStyleElement_Binding::Wrap(aCx, this, aGivenProto); |
225 | 0 | } |
226 | | |
227 | | } // namespace dom |
228 | | } // namespace mozilla |
229 | | |