1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "mozilla/Attributes.h"
6 #include "mozilla/dom/Element.h"
7 #include "mozilla/mozalloc.h"
11 #include "nsComputedDOMStyle.h"
14 #include "nsGkAtoms.h"
15 #include "nsHTMLCSSUtils.h"
16 #include "nsHTMLEditor.h"
18 #include "nsIContent.h"
20 #include "nsIDOMCSSPrimitiveValue.h"
21 #include "nsIDOMCSSStyleDeclaration.h"
22 #include "nsIDOMCSSValue.h"
23 #include "nsIDOMElement.h"
24 #include "nsIDOMEventTarget.h"
25 #include "nsIDOMHTMLElement.h"
26 #include "nsIDOMNode.h"
27 #include "nsIDOMWindow.h"
28 #include "nsIDocument.h"
29 #include "nsIDocumentObserver.h"
30 #include "nsIHTMLAbsPosEditor.h"
31 #include "nsIHTMLEditor.h"
32 #include "nsIHTMLInlineTableEditor.h"
33 #include "nsIHTMLObjectResizer.h"
34 #include "nsIMutationObserver.h"
36 #include "nsIPresShell.h"
37 #include "nsISupportsImpl.h"
38 #include "nsISupportsUtils.h"
39 #include "nsLiteralString.h"
40 #include "nsPresContext.h"
41 #include "nsReadableUtils.h"
43 #include "nsStringFwd.h"
44 #include "nsUnicharUtils.h"
46 #include "nsContentUtils.h" // for nsAutoScriptBlocker
48 class nsIDOMEventListener
;
51 using namespace mozilla
;
52 using namespace mozilla::dom
;
54 // retrieve an integer stored into a CSS computed float value
55 static int32_t GetCSSFloatValue(nsIDOMCSSStyleDeclaration
* aDecl
,
56 const nsAString
& aProperty
)
60 nsCOMPtr
<nsIDOMCSSValue
> value
;
61 // get the computed CSSValue of the property
62 nsresult res
= aDecl
->GetPropertyCSSValue(aProperty
, getter_AddRefs(value
));
63 if (NS_FAILED(res
) || !value
) return 0;
65 // check the type of the returned CSSValue; we handle here only
66 // pixel and enum types
67 nsCOMPtr
<nsIDOMCSSPrimitiveValue
> val
= do_QueryInterface(value
);
69 val
->GetPrimitiveType(&type
);
73 case nsIDOMCSSPrimitiveValue::CSS_PX
:
74 // the value is in pixels, just get it
75 res
= val
->GetFloatValue(nsIDOMCSSPrimitiveValue::CSS_PX
, &f
);
76 NS_ENSURE_SUCCESS(res
, 0);
78 case nsIDOMCSSPrimitiveValue::CSS_IDENT
: {
79 // the value is keyword, we have to map these keywords into
82 res
= val
->GetStringValue(str
);
83 if (str
.EqualsLiteral("thin"))
85 else if (str
.EqualsLiteral("medium"))
87 else if (str
.EqualsLiteral("thick"))
96 class nsElementDeletionObserver final
: public nsIMutationObserver
99 nsElementDeletionObserver(nsINode
* aNativeAnonNode
, nsINode
* aObservedNode
)
100 : mNativeAnonNode(aNativeAnonNode
), mObservedNode(aObservedNode
) {}
102 NS_DECL_NSIMUTATIONOBSERVER
104 ~nsElementDeletionObserver() {}
105 nsINode
* mNativeAnonNode
;
106 nsINode
* mObservedNode
;
109 NS_IMPL_ISUPPORTS(nsElementDeletionObserver
, nsIMutationObserver
)
110 NS_IMPL_NSIMUTATIONOBSERVER_CONTENT(nsElementDeletionObserver
)
113 nsElementDeletionObserver::NodeWillBeDestroyed(const nsINode
* aNode
)
115 NS_ASSERTION(aNode
== mNativeAnonNode
|| aNode
== mObservedNode
,
117 if (aNode
== mNativeAnonNode
) {
118 mObservedNode
->RemoveMutationObserver(this);
120 mNativeAnonNode
->RemoveMutationObserver(this);
121 static_cast<nsIContent
*>(mNativeAnonNode
)->UnbindFromTree();
127 // Returns in *aReturn an anonymous nsDOMElement of type aTag,
128 // child of aParentNode. If aIsCreatedHidden is true, the class
129 // "hidden" is added to the created element. If aAnonClass is not
130 // the empty string, it becomes the value of the attribute "_moz_anonclass"
132 nsHTMLEditor::CreateAnonymousElement(const nsAString
& aTag
, nsIDOMNode
* aParentNode
,
133 const nsAString
& aAnonClass
, bool aIsCreatedHidden
,
134 nsIDOMElement
** aReturn
)
136 NS_ENSURE_ARG_POINTER(aParentNode
);
137 NS_ENSURE_ARG_POINTER(aReturn
);
140 nsCOMPtr
<nsIContent
> parentContent( do_QueryInterface(aParentNode
) );
141 NS_ENSURE_TRUE(parentContent
, NS_OK
);
143 nsCOMPtr
<nsIDocument
> doc
= GetDocument();
144 NS_ENSURE_TRUE(doc
, NS_ERROR_NULL_POINTER
);
146 // Get the pres shell
147 nsCOMPtr
<nsIPresShell
> ps
= GetPresShell();
148 NS_ENSURE_TRUE(ps
, NS_ERROR_NOT_INITIALIZED
);
150 // Create a new node through the element factory
151 nsCOMPtr
<Element
> newContent
=
152 CreateHTMLContent(nsCOMPtr
<nsIAtom
>(do_GetAtom(aTag
)));
153 NS_ENSURE_STATE(newContent
);
155 nsCOMPtr
<nsIDOMElement
> newElement
= do_QueryInterface(newContent
);
156 NS_ENSURE_TRUE(newElement
, NS_ERROR_FAILURE
);
158 // add the "hidden" class if needed
160 if (aIsCreatedHidden
) {
161 res
= newElement
->SetAttribute(NS_LITERAL_STRING("class"),
162 NS_LITERAL_STRING("hidden"));
163 NS_ENSURE_SUCCESS(res
, res
);
166 // add an _moz_anonclass attribute if needed
167 if (!aAnonClass
.IsEmpty()) {
168 res
= newElement
->SetAttribute(NS_LITERAL_STRING("_moz_anonclass"),
170 NS_ENSURE_SUCCESS(res
, res
);
174 nsAutoScriptBlocker scriptBlocker
;
176 // establish parenthood of the element
177 newContent
->SetIsNativeAnonymousRoot();
178 res
= newContent
->BindToTree(doc
, parentContent
, parentContent
, true);
179 if (NS_FAILED(res
)) {
180 newContent
->UnbindFromTree();
185 nsElementDeletionObserver
* observer
=
186 new nsElementDeletionObserver(newContent
, parentContent
);
187 NS_ADDREF(observer
); // NodeWillBeDestroyed releases.
188 parentContent
->AddMutationObserver(observer
);
189 newContent
->AddMutationObserver(observer
);
192 // Editor anonymous content gets passed to RecreateFramesFor... which can't
193 // _really_ deal with anonymous content (because it can't get the frame tree
194 // ordering right). But for us the ordering doesn't matter so this is sort of
196 newContent
->SetProperty(nsGkAtoms::restylableAnonymousNode
,
197 reinterpret_cast<void*>(true));
200 // display the element
201 ps
->RecreateFramesFor(newContent
);
203 newElement
.forget(aReturn
);
207 // Removes event listener and calls DeleteRefToAnonymousNode.
209 nsHTMLEditor::RemoveListenerAndDeleteRef(const nsAString
& aEvent
,
210 nsIDOMEventListener
* aListener
,
213 nsIContent
* aParentContent
,
214 nsIPresShell
* aShell
)
216 nsCOMPtr
<nsIDOMEventTarget
> evtTarget(do_QueryInterface(aElement
));
218 evtTarget
->RemoveEventListener(aEvent
, aListener
, aUseCapture
);
220 DeleteRefToAnonymousNode(static_cast<nsIDOMElement
*>(GetAsDOMNode(aElement
)), aParentContent
, aShell
);
223 // Deletes all references to an anonymous element
225 nsHTMLEditor::DeleteRefToAnonymousNode(nsIDOMElement
* aElement
,
226 nsIContent
* aParentContent
,
227 nsIPresShell
* aShell
)
229 // call ContentRemoved() for the anonymous content
230 // node so its references get removed from the frame manager's
231 // undisplay map, and its layout frames get destroyed!
234 nsCOMPtr
<nsIContent
> content
= do_QueryInterface(aElement
);
236 nsAutoScriptBlocker scriptBlocker
;
237 // Need to check whether aShell has been destroyed (but not yet deleted).
238 // In that case presContext->GetPresShell() returns nullptr.
240 if (aShell
&& aShell
->GetPresContext() &&
241 aShell
->GetPresContext()->GetPresShell() == aShell
) {
242 nsCOMPtr
<nsIDocumentObserver
> docObserver
= do_QueryInterface(aShell
);
244 // Call BeginUpdate() so that the nsCSSFrameConstructor/PresShell
245 // knows we're messing with the frame tree.
246 nsCOMPtr
<nsIDocument
> document
= GetDocument();
248 docObserver
->BeginUpdate(document
, UPDATE_CONTENT_MODEL
);
250 // XXX This is wrong (bug 439258). Once it's fixed, the NS_WARNING
251 // in RestyleManager::RestyleForRemove should be changed back
253 docObserver
->ContentRemoved(content
->GetCurrentDoc(),
254 aParentContent
, content
, -1,
255 content
->GetPreviousSibling());
257 docObserver
->EndUpdate(document
, UPDATE_CONTENT_MODEL
);
260 content
->UnbindFromTree();
265 // The following method is mostly called by a selection listener. When a
266 // selection change is notified, the method is called to check if resizing
267 // handles, a grabber and/or inline table editing UI need to be displayed
270 nsHTMLEditor::CheckSelectionStateForAnonymousButtons(nsISelection
* aSelection
)
272 NS_ENSURE_ARG_POINTER(aSelection
);
274 // early way out if all contextual UI extensions are disabled
275 NS_ENSURE_TRUE(mIsObjectResizingEnabled
||
276 mIsAbsolutelyPositioningEnabled
||
277 mIsInlineTableEditingEnabled
, NS_OK
);
279 // Don't change selection state if we're moving.
284 nsCOMPtr
<nsIDOMElement
> focusElement
;
285 // let's get the containing element of the selection
286 nsresult res
= GetSelectionContainer(getter_AddRefs(focusElement
));
287 NS_ENSURE_TRUE(focusElement
, NS_OK
);
288 NS_ENSURE_SUCCESS(res
, res
);
290 // If we're not in a document, don't try to add resizers
291 nsCOMPtr
<dom::Element
> focusElementNode
= do_QueryInterface(focusElement
);
292 NS_ENSURE_STATE(focusElementNode
);
293 if (!focusElementNode
->IsInDoc()) {
298 nsAutoString focusTagName
;
299 res
= focusElement
->GetTagName(focusTagName
);
300 NS_ENSURE_SUCCESS(res
, res
);
301 ToLowerCase(focusTagName
);
302 nsCOMPtr
<nsIAtom
> focusTagAtom
= do_GetAtom(focusTagName
);
304 nsCOMPtr
<nsIDOMElement
> absPosElement
;
305 if (mIsAbsolutelyPositioningEnabled
) {
306 // Absolute Positioning support is enabled, is the selection contained
307 // in an absolutely positioned element ?
308 res
= GetAbsolutelyPositionedSelectionContainer(getter_AddRefs(absPosElement
));
309 NS_ENSURE_SUCCESS(res
, res
);
312 nsCOMPtr
<nsIDOMElement
> cellElement
;
313 if (mIsObjectResizingEnabled
|| mIsInlineTableEditingEnabled
) {
314 // Resizing or Inline Table Editing is enabled, we need to check if the
315 // selection is contained in a table cell
316 res
= GetElementOrParentByTagName(NS_LITERAL_STRING("td"),
318 getter_AddRefs(cellElement
));
319 NS_ENSURE_SUCCESS(res
, res
);
322 if (mIsObjectResizingEnabled
&& cellElement
) {
323 // we are here because Resizing is enabled AND selection is contained in
326 // get the enclosing table
327 if (nsGkAtoms::img
!= focusTagAtom
) {
328 // the element container of the selection is not an image, so we'll show
329 // the resizers around the table
330 nsCOMPtr
<nsIDOMNode
> tableNode
= GetEnclosingTable(cellElement
);
331 focusElement
= do_QueryInterface(tableNode
);
332 focusTagAtom
= nsGkAtoms::table
;
336 // we allow resizers only around images, tables, and absolutely positioned
337 // elements. If we don't have image/table, let's look at the latter case.
338 if (nsGkAtoms::img
!= focusTagAtom
&& nsGkAtoms::table
!= focusTagAtom
) {
339 focusElement
= absPosElement
;
342 // at this point, focusElement contains the element for Resizing,
343 // cellElement contains the element for InlineTableEditing
344 // absPosElement contains the element for Positioning
346 // Note: All the Hide/Show methods below may change attributes on real
347 // content which means a DOMAttrModified handler may cause arbitrary
348 // side effects while this code runs (bug 420439).
350 if (mIsAbsolutelyPositioningEnabled
&& mAbsolutelyPositionedObject
&&
351 absPosElement
!= GetAsDOMNode(mAbsolutelyPositionedObject
)) {
353 NS_ENSURE_SUCCESS(res
, res
);
354 NS_ASSERTION(!mAbsolutelyPositionedObject
, "HideGrabber failed");
357 if (mIsObjectResizingEnabled
&& mResizedObject
&&
358 GetAsDOMNode(mResizedObject
) != focusElement
) {
359 res
= HideResizers();
360 NS_ENSURE_SUCCESS(res
, res
);
361 NS_ASSERTION(!mResizedObject
, "HideResizers failed");
364 if (mIsInlineTableEditingEnabled
&& mInlineEditedCell
&&
365 mInlineEditedCell
!= cellElement
) {
366 res
= HideInlineTableEditingUI();
367 NS_ENSURE_SUCCESS(res
, res
);
368 NS_ASSERTION(!mInlineEditedCell
, "HideInlineTableEditingUI failed");
371 // now, let's display all contextual UI for good
372 nsIContent
* hostContent
= GetActiveEditingHost();
373 nsCOMPtr
<nsIDOMNode
> hostNode
= do_QueryInterface(hostContent
);
375 if (mIsObjectResizingEnabled
&& focusElement
&&
376 IsModifiableNode(focusElement
) && focusElement
!= hostNode
) {
377 if (nsGkAtoms::img
== focusTagAtom
) {
378 mResizedObjectIsAnImage
= true;
381 res
= RefreshResizers();
383 res
= ShowResizers(focusElement
);
384 NS_ENSURE_SUCCESS(res
, res
);
387 if (mIsAbsolutelyPositioningEnabled
&& absPosElement
&&
388 IsModifiableNode(absPosElement
) && absPosElement
!= hostNode
) {
389 if (mAbsolutelyPositionedObject
)
390 res
= RefreshGrabber();
392 res
= ShowGrabberOnElement(absPosElement
);
393 NS_ENSURE_SUCCESS(res
, res
);
396 if (mIsInlineTableEditingEnabled
&& cellElement
&&
397 IsModifiableNode(cellElement
) && cellElement
!= hostNode
) {
398 if (mInlineEditedCell
)
399 res
= RefreshInlineTableEditingUI();
401 res
= ShowInlineTableEditingUI(cellElement
);
407 // Resizing and Absolute Positioning need to know everything about the
408 // containing box of the element: position, size, margins, borders
410 nsHTMLEditor::GetPositionAndDimensions(nsIDOMElement
* aElement
,
411 int32_t & aX
, int32_t & aY
,
412 int32_t & aW
, int32_t & aH
,
413 int32_t & aBorderLeft
,
414 int32_t & aBorderTop
,
415 int32_t & aMarginLeft
,
416 int32_t & aMarginTop
)
418 nsCOMPtr
<Element
> element
= do_QueryInterface(aElement
);
419 NS_ENSURE_ARG_POINTER(element
);
421 // Is the element positioned ? let's check the cheap way first...
422 bool isPositioned
= false;
423 nsresult res
= aElement
->HasAttribute(NS_LITERAL_STRING("_moz_abspos"), &isPositioned
);
424 NS_ENSURE_SUCCESS(res
, res
);
426 // hmmm... the expensive way now...
427 nsAutoString positionStr
;
428 mHTMLCSSUtils
->GetComputedProperty(*element
, *nsGkAtoms::position
,
430 isPositioned
= positionStr
.EqualsLiteral("absolute");
434 // Yes, it is absolutely positioned
435 mResizedObjectIsAbsolutelyPositioned
= true;
437 // Get the all the computed css styles attached to the element node
438 RefPtr
<nsComputedDOMStyle
> cssDecl
=
439 mHTMLCSSUtils
->GetComputedStyle(element
);
440 NS_ENSURE_STATE(cssDecl
);
442 aBorderLeft
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("border-left-width"));
443 aBorderTop
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("border-top-width"));
444 aMarginLeft
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("margin-left"));
445 aMarginTop
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("margin-top"));
447 aX
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("left")) +
448 aMarginLeft
+ aBorderLeft
;
449 aY
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("top")) +
450 aMarginTop
+ aBorderTop
;
451 aW
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("width"));
452 aH
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("height"));
455 mResizedObjectIsAbsolutelyPositioned
= false;
456 nsCOMPtr
<nsIDOMHTMLElement
> htmlElement
= do_QueryInterface(aElement
);
458 return NS_ERROR_NULL_POINTER
;
460 GetElementOrigin(aElement
, aX
, aY
);
462 res
= htmlElement
->GetOffsetWidth(&aW
);
463 NS_ENSURE_SUCCESS(res
, res
);
464 res
= htmlElement
->GetOffsetHeight(&aH
);
476 nsHTMLEditor::SetAnonymousElementPosition(int32_t aX
, int32_t aY
, nsIDOMElement
*aElement
)
478 mHTMLCSSUtils
->SetCSSPropertyPixels(aElement
, NS_LITERAL_STRING("left"), aX
);
479 mHTMLCSSUtils
->SetCSSPropertyPixels(aElement
, NS_LITERAL_STRING("top"), aY
);