/src/mozilla-central/dom/base/AnonymousContent.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 | | |
7 | | #include "AnonymousContent.h" |
8 | | #include "mozilla/dom/Element.h" |
9 | | #include "mozilla/dom/AnonymousContentBinding.h" |
10 | | #include "nsComputedDOMStyle.h" |
11 | | #include "nsCycleCollectionParticipant.h" |
12 | | #include "nsIDocument.h" |
13 | | #include "nsIFrame.h" |
14 | | #include "nsStyledElement.h" |
15 | | #include "HTMLCanvasElement.h" |
16 | | |
17 | | namespace mozilla { |
18 | | namespace dom { |
19 | | |
20 | | // Ref counting and cycle collection |
21 | | NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(AnonymousContent, AddRef) |
22 | | NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(AnonymousContent, Release) |
23 | | NS_IMPL_CYCLE_COLLECTION(AnonymousContent, mContentNode) |
24 | | |
25 | | AnonymousContent::AnonymousContent(already_AddRefed<Element> aContentNode) |
26 | | : mContentNode(aContentNode) |
27 | 0 | { |
28 | 0 | MOZ_ASSERT(mContentNode); |
29 | 0 | } |
30 | | |
31 | 0 | AnonymousContent::~AnonymousContent() = default; |
32 | | |
33 | | void |
34 | | AnonymousContent::SetTextContentForElement(const nsAString& aElementId, |
35 | | const nsAString& aText, |
36 | | ErrorResult& aRv) |
37 | 0 | { |
38 | 0 | Element* element = GetElementById(aElementId); |
39 | 0 | if (!element) { |
40 | 0 | aRv.Throw(NS_ERROR_NOT_AVAILABLE); |
41 | 0 | return; |
42 | 0 | } |
43 | 0 | |
44 | 0 | element->SetTextContent(aText, aRv); |
45 | 0 | } |
46 | | |
47 | | void |
48 | | AnonymousContent::GetTextContentForElement(const nsAString& aElementId, |
49 | | DOMString& aText, |
50 | | ErrorResult& aRv) |
51 | 0 | { |
52 | 0 | Element* element = GetElementById(aElementId); |
53 | 0 | if (!element) { |
54 | 0 | aRv.Throw(NS_ERROR_NOT_AVAILABLE); |
55 | 0 | return; |
56 | 0 | } |
57 | 0 | |
58 | 0 | element->GetTextContent(aText, aRv); |
59 | 0 | } |
60 | | |
61 | | void |
62 | | AnonymousContent::SetAttributeForElement(const nsAString& aElementId, |
63 | | const nsAString& aName, |
64 | | const nsAString& aValue, |
65 | | nsIPrincipal* aSubjectPrincipal, |
66 | | ErrorResult& aRv) |
67 | 0 | { |
68 | 0 | Element* element = GetElementById(aElementId); |
69 | 0 | if (!element) { |
70 | 0 | aRv.Throw(NS_ERROR_NOT_AVAILABLE); |
71 | 0 | return; |
72 | 0 | } |
73 | 0 | |
74 | 0 | element->SetAttribute(aName, aValue, aSubjectPrincipal, aRv); |
75 | 0 | } |
76 | | |
77 | | void |
78 | | AnonymousContent::GetAttributeForElement(const nsAString& aElementId, |
79 | | const nsAString& aName, |
80 | | DOMString& aValue, |
81 | | ErrorResult& aRv) |
82 | 0 | { |
83 | 0 | Element* element = GetElementById(aElementId); |
84 | 0 | if (!element) { |
85 | 0 | aRv.Throw(NS_ERROR_NOT_AVAILABLE); |
86 | 0 | return; |
87 | 0 | } |
88 | 0 | |
89 | 0 | element->GetAttribute(aName, aValue); |
90 | 0 | } |
91 | | |
92 | | void |
93 | | AnonymousContent::RemoveAttributeForElement(const nsAString& aElementId, |
94 | | const nsAString& aName, |
95 | | ErrorResult& aRv) |
96 | 0 | { |
97 | 0 | Element* element = GetElementById(aElementId); |
98 | 0 | if (!element) { |
99 | 0 | aRv.Throw(NS_ERROR_NOT_AVAILABLE); |
100 | 0 | return; |
101 | 0 | } |
102 | 0 | |
103 | 0 | element->RemoveAttribute(aName, aRv); |
104 | 0 | } |
105 | | |
106 | | already_AddRefed<nsISupports> |
107 | | AnonymousContent::GetCanvasContext(const nsAString& aElementId, |
108 | | const nsAString& aContextId, |
109 | | ErrorResult& aRv) |
110 | 0 | { |
111 | 0 | Element* element = GetElementById(aElementId); |
112 | 0 |
|
113 | 0 | if (!element) { |
114 | 0 | aRv.Throw(NS_ERROR_NOT_AVAILABLE); |
115 | 0 | return nullptr; |
116 | 0 | } |
117 | 0 | |
118 | 0 | if (!element->IsHTMLElement(nsGkAtoms::canvas)) { |
119 | 0 | return nullptr; |
120 | 0 | } |
121 | 0 | |
122 | 0 | nsCOMPtr<nsISupports> context; |
123 | 0 |
|
124 | 0 | HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(element); |
125 | 0 | canvas->GetContext(aContextId, getter_AddRefs(context)); |
126 | 0 |
|
127 | 0 | return context.forget(); |
128 | 0 | } |
129 | | |
130 | | already_AddRefed<Animation> |
131 | | AnonymousContent::SetAnimationForElement(JSContext* aContext, |
132 | | const nsAString& aElementId, |
133 | | JS::Handle<JSObject*> aKeyframes, |
134 | | const UnrestrictedDoubleOrKeyframeAnimationOptions& aOptions, |
135 | | ErrorResult& aRv) |
136 | 0 | { |
137 | 0 | Element* element = GetElementById(aElementId); |
138 | 0 |
|
139 | 0 | if (!element) { |
140 | 0 | aRv.Throw(NS_ERROR_NOT_AVAILABLE); |
141 | 0 | return nullptr; |
142 | 0 | } |
143 | 0 | |
144 | 0 | return element->Animate(aContext, aKeyframes, aOptions, aRv); |
145 | 0 | } |
146 | | |
147 | | void |
148 | | AnonymousContent::SetCutoutRectsForElement(const nsAString& aElementId, |
149 | | const Sequence<OwningNonNull<DOMRect>>& aRects, |
150 | | ErrorResult& aRv) |
151 | 0 | { |
152 | 0 | Element* element = GetElementById(aElementId); |
153 | 0 |
|
154 | 0 | if (!element) { |
155 | 0 | aRv.Throw(NS_ERROR_NOT_AVAILABLE); |
156 | 0 | return; |
157 | 0 | } |
158 | 0 | |
159 | 0 | nsRegion cutOutRegion; |
160 | 0 | for (const auto& r : aRects) { |
161 | 0 | CSSRect rect(r->X(), r->Y(), r->Width(), r->Height()); |
162 | 0 | cutOutRegion.OrWith(CSSRect::ToAppUnits(rect)); |
163 | 0 | } |
164 | 0 |
|
165 | 0 | element->SetProperty(nsGkAtoms::cutoutregion, new nsRegion(cutOutRegion), |
166 | 0 | nsINode::DeleteProperty<nsRegion>); |
167 | 0 |
|
168 | 0 | nsIFrame* frame = element->GetPrimaryFrame(); |
169 | 0 | if (frame) { |
170 | 0 | frame->SchedulePaint(); |
171 | 0 | } |
172 | 0 | } |
173 | | |
174 | | Element* |
175 | | AnonymousContent::GetElementById(const nsAString& aElementId) |
176 | 0 | { |
177 | 0 | // This can be made faster in the future if needed. |
178 | 0 | RefPtr<nsAtom> elementId = NS_Atomize(aElementId); |
179 | 0 | for (nsIContent* node = mContentNode; node; |
180 | 0 | node = node->GetNextNode(mContentNode)) { |
181 | 0 | if (!node->IsElement()) { |
182 | 0 | continue; |
183 | 0 | } |
184 | 0 | nsAtom* id = node->AsElement()->GetID(); |
185 | 0 | if (id && id == elementId) { |
186 | 0 | return node->AsElement(); |
187 | 0 | } |
188 | 0 | } |
189 | 0 | return nullptr; |
190 | 0 | } |
191 | | |
192 | | bool |
193 | | AnonymousContent::WrapObject(JSContext* aCx, |
194 | | JS::Handle<JSObject*> aGivenProto, |
195 | | JS::MutableHandle<JSObject*> aReflector) |
196 | 0 | { |
197 | 0 | return AnonymousContent_Binding::Wrap(aCx, this, aGivenProto, aReflector); |
198 | 0 | } |
199 | | |
200 | | void |
201 | | AnonymousContent::GetComputedStylePropertyValue(const nsAString& aElementId, |
202 | | const nsAString& aPropertyName, |
203 | | DOMString& aResult, |
204 | | ErrorResult& aRv) |
205 | 0 | { |
206 | 0 | Element* element = GetElementById(aElementId); |
207 | 0 | if (!element) { |
208 | 0 | aRv.Throw(NS_ERROR_NOT_AVAILABLE); |
209 | 0 | return; |
210 | 0 | } |
211 | 0 | |
212 | 0 | nsIPresShell* shell = element->OwnerDoc()->GetShell(); |
213 | 0 | if (!shell) { |
214 | 0 | aRv.Throw(NS_ERROR_NOT_AVAILABLE); |
215 | 0 | return; |
216 | 0 | } |
217 | 0 | |
218 | 0 | RefPtr<nsComputedDOMStyle> cs = |
219 | 0 | new nsComputedDOMStyle(element, |
220 | 0 | NS_LITERAL_STRING(""), |
221 | 0 | element->OwnerDoc(), |
222 | 0 | nsComputedDOMStyle::eAll); |
223 | 0 | aRv = cs->GetPropertyValue(aPropertyName, aResult); |
224 | 0 | } |
225 | | |
226 | | } // namespace dom |
227 | | } // namespace mozilla |