Bug 1750871 - run mochitest-remote on fission everywhere. r=releng-reviewers,aki
[gecko.git] / dom / base / AnonymousContent.cpp
blob34b8922718ed693586953556464785a66f8375bc
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 "AnonymousContent.h"
8 #include "mozilla/PresShell.h"
9 #include "mozilla/dom/Document.h"
10 #include "mozilla/dom/Element.h"
11 #include "mozilla/dom/Event.h"
12 #include "mozilla/dom/AnonymousContentBinding.h"
13 #include "nsComputedDOMStyle.h"
14 #include "nsCycleCollectionParticipant.h"
15 #include "nsIFrame.h"
16 #include "nsStyledElement.h"
17 #include "HTMLCanvasElement.h"
19 namespace mozilla::dom {
21 // Ref counting and cycle collection
22 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(AnonymousContent, AddRef)
23 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(AnonymousContent, Release)
24 NS_IMPL_CYCLE_COLLECTION(AnonymousContent, mContentNode)
26 AnonymousContent::AnonymousContent(already_AddRefed<Element> aContentNode)
27 : mContentNode(aContentNode) {
28 MOZ_ASSERT(mContentNode);
31 AnonymousContent::~AnonymousContent() = default;
33 void AnonymousContent::SetTextContentForElement(const nsAString& aElementId,
34 const nsAString& aText,
35 ErrorResult& aRv) {
36 Element* element = GetElementById(aElementId);
37 if (!element) {
38 aRv.Throw(NS_ERROR_NOT_AVAILABLE);
39 return;
42 element->SetTextContent(aText, aRv);
45 void AnonymousContent::GetTextContentForElement(const nsAString& aElementId,
46 DOMString& aText,
47 ErrorResult& aRv) {
48 Element* element = GetElementById(aElementId);
49 if (!element) {
50 aRv.Throw(NS_ERROR_NOT_AVAILABLE);
51 return;
54 element->GetTextContent(aText, aRv);
57 void AnonymousContent::SetAttributeForElement(const nsAString& aElementId,
58 const nsAString& aName,
59 const nsAString& aValue,
60 nsIPrincipal* aSubjectPrincipal,
61 ErrorResult& aRv) {
62 Element* element = GetElementById(aElementId);
63 if (!element) {
64 aRv.Throw(NS_ERROR_NOT_AVAILABLE);
65 return;
68 element->SetAttribute(aName, aValue, aSubjectPrincipal, aRv);
71 void AnonymousContent::GetAttributeForElement(const nsAString& aElementId,
72 const nsAString& aName,
73 DOMString& aValue,
74 ErrorResult& aRv) {
75 Element* element = GetElementById(aElementId);
76 if (!element) {
77 aRv.Throw(NS_ERROR_NOT_AVAILABLE);
78 return;
81 element->GetAttribute(aName, aValue);
84 void AnonymousContent::RemoveAttributeForElement(const nsAString& aElementId,
85 const nsAString& aName,
86 ErrorResult& aRv) {
87 Element* element = GetElementById(aElementId);
88 if (!element) {
89 aRv.Throw(NS_ERROR_NOT_AVAILABLE);
90 return;
93 element->RemoveAttribute(aName, aRv);
96 already_AddRefed<nsISupports> AnonymousContent::GetCanvasContext(
97 const nsAString& aElementId, const nsAString& aContextId,
98 ErrorResult& aRv) {
99 Element* element = GetElementById(aElementId);
101 if (!element) {
102 aRv.Throw(NS_ERROR_NOT_AVAILABLE);
103 return nullptr;
106 if (!element->IsHTMLElement(nsGkAtoms::canvas)) {
107 return nullptr;
110 nsCOMPtr<nsISupports> context;
112 HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(element);
113 canvas->GetContext(aContextId, getter_AddRefs(context));
115 return context.forget();
118 already_AddRefed<Animation> AnonymousContent::SetAnimationForElement(
119 JSContext* aContext, const nsAString& aElementId,
120 JS::Handle<JSObject*> aKeyframes,
121 const UnrestrictedDoubleOrKeyframeAnimationOptions& aOptions,
122 ErrorResult& aRv) {
123 Element* element = GetElementById(aElementId);
125 if (!element) {
126 aRv.Throw(NS_ERROR_NOT_AVAILABLE);
127 return nullptr;
130 return element->Animate(aContext, aKeyframes, aOptions, aRv);
133 void AnonymousContent::SetCutoutRectsForElement(
134 const nsAString& aElementId, const Sequence<OwningNonNull<DOMRect>>& aRects,
135 ErrorResult& aRv) {
136 Element* element = GetElementById(aElementId);
138 if (!element) {
139 aRv.Throw(NS_ERROR_NOT_AVAILABLE);
140 return;
143 nsRegion cutOutRegion;
144 for (const auto& r : aRects) {
145 CSSRect rect(r->X(), r->Y(), r->Width(), r->Height());
146 cutOutRegion.OrWith(CSSRect::ToAppUnits(rect));
149 element->SetProperty(nsGkAtoms::cutoutregion, new nsRegion(cutOutRegion),
150 nsINode::DeleteProperty<nsRegion>);
152 nsIFrame* frame = element->GetPrimaryFrame();
153 if (frame) {
154 frame->SchedulePaint();
158 Element* AnonymousContent::GetElementById(const nsAString& aElementId) {
159 // This can be made faster in the future if needed.
160 RefPtr<nsAtom> elementId = NS_Atomize(aElementId);
161 for (nsIContent* node = mContentNode; node;
162 node = node->GetNextNode(mContentNode)) {
163 if (!node->IsElement()) {
164 continue;
166 nsAtom* id = node->AsElement()->GetID();
167 if (id && id == elementId) {
168 return node->AsElement();
171 return nullptr;
174 bool AnonymousContent::WrapObject(JSContext* aCx,
175 JS::Handle<JSObject*> aGivenProto,
176 JS::MutableHandle<JSObject*> aReflector) {
177 return AnonymousContent_Binding::Wrap(aCx, this, aGivenProto, aReflector);
180 void AnonymousContent::GetComputedStylePropertyValue(
181 const nsAString& aElementId, const nsACString& aPropertyName,
182 nsACString& aResult, ErrorResult& aRv) {
183 Element* element = GetElementById(aElementId);
184 if (!element) {
185 aRv.Throw(NS_ERROR_NOT_AVAILABLE);
186 return;
189 if (!element->OwnerDoc()->GetPresShell()) {
190 aRv.Throw(NS_ERROR_NOT_AVAILABLE);
191 return;
194 RefPtr<nsComputedDOMStyle> cs = new nsComputedDOMStyle(
195 element, PseudoStyleType::NotPseudo, element->OwnerDoc(),
196 nsComputedDOMStyle::StyleType::All);
197 aRv = cs->GetPropertyValue(aPropertyName, aResult);
200 void AnonymousContent::GetTargetIdForEvent(Event& aEvent, DOMString& aResult) {
201 nsCOMPtr<Element> el = do_QueryInterface(aEvent.GetOriginalTarget());
202 if (el && el->IsInNativeAnonymousSubtree() && mContentNode->Contains(el)) {
203 aResult.SetKnownLiveAtom(el->GetID(), DOMString::eTreatNullAsNull);
204 return;
207 aResult.SetNull();
210 void AnonymousContent::SetStyle(const nsACString& aProperty,
211 const nsACString& aValue, ErrorResult& aRv) {
212 if (!mContentNode->IsHTMLElement()) {
213 aRv.Throw(NS_ERROR_NOT_AVAILABLE);
214 return;
217 nsGenericHTMLElement* element = nsGenericHTMLElement::FromNode(mContentNode);
218 nsCOMPtr<nsICSSDeclaration> declaration = element->Style();
219 declaration->SetProperty(aProperty, aValue, ""_ns, IgnoreErrors());
222 } // namespace mozilla::dom