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 MOZ_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
);
191 // display the element
192 ps
->RecreateFramesFor(newContent
);
194 newElement
.forget(aReturn
);
198 // Removes event listener and calls DeleteRefToAnonymousNode.
200 nsHTMLEditor::RemoveListenerAndDeleteRef(const nsAString
& aEvent
,
201 nsIDOMEventListener
* aListener
,
203 nsIDOMElement
* aElement
,
204 nsIContent
* aParentContent
,
205 nsIPresShell
* aShell
)
207 nsCOMPtr
<nsIDOMEventTarget
> evtTarget(do_QueryInterface(aElement
));
209 evtTarget
->RemoveEventListener(aEvent
, aListener
, aUseCapture
);
211 DeleteRefToAnonymousNode(aElement
, aParentContent
, aShell
);
214 // Deletes all references to an anonymous element
216 nsHTMLEditor::DeleteRefToAnonymousNode(nsIDOMElement
* aElement
,
217 nsIContent
* aParentContent
,
218 nsIPresShell
* aShell
)
220 // call ContentRemoved() for the anonymous content
221 // node so its references get removed from the frame manager's
222 // undisplay map, and its layout frames get destroyed!
225 nsCOMPtr
<nsIContent
> content
= do_QueryInterface(aElement
);
227 nsAutoScriptBlocker scriptBlocker
;
228 // Need to check whether aShell has been destroyed (but not yet deleted).
229 // In that case presContext->GetPresShell() returns nullptr.
231 if (aShell
&& aShell
->GetPresContext() &&
232 aShell
->GetPresContext()->GetPresShell() == aShell
) {
233 nsCOMPtr
<nsIDocumentObserver
> docObserver
= do_QueryInterface(aShell
);
235 // Call BeginUpdate() so that the nsCSSFrameConstructor/PresShell
236 // knows we're messing with the frame tree.
237 nsCOMPtr
<nsIDocument
> document
= GetDocument();
239 docObserver
->BeginUpdate(document
, UPDATE_CONTENT_MODEL
);
241 // XXX This is wrong (bug 439258). Once it's fixed, the NS_WARNING
242 // in RestyleManager::RestyleForRemove should be changed back
244 docObserver
->ContentRemoved(content
->GetCurrentDoc(),
245 aParentContent
, content
, -1,
246 content
->GetPreviousSibling());
248 docObserver
->EndUpdate(document
, UPDATE_CONTENT_MODEL
);
251 content
->UnbindFromTree();
256 // The following method is mostly called by a selection listener. When a
257 // selection change is notified, the method is called to check if resizing
258 // handles, a grabber and/or inline table editing UI need to be displayed
261 nsHTMLEditor::CheckSelectionStateForAnonymousButtons(nsISelection
* aSelection
)
263 NS_ENSURE_ARG_POINTER(aSelection
);
265 // early way out if all contextual UI extensions are disabled
266 NS_ENSURE_TRUE(mIsObjectResizingEnabled
||
267 mIsAbsolutelyPositioningEnabled
||
268 mIsInlineTableEditingEnabled
, NS_OK
);
270 // Don't change selection state if we're moving.
275 nsCOMPtr
<nsIDOMElement
> focusElement
;
276 // let's get the containing element of the selection
277 nsresult res
= GetSelectionContainer(getter_AddRefs(focusElement
));
278 NS_ENSURE_TRUE(focusElement
, NS_OK
);
279 NS_ENSURE_SUCCESS(res
, res
);
281 // If we're not in a document, don't try to add resizers
282 nsCOMPtr
<dom::Element
> focusElementNode
= do_QueryInterface(focusElement
);
283 NS_ENSURE_STATE(focusElementNode
);
284 if (!focusElementNode
->IsInDoc()) {
289 nsAutoString focusTagName
;
290 res
= focusElement
->GetTagName(focusTagName
);
291 NS_ENSURE_SUCCESS(res
, res
);
292 ToLowerCase(focusTagName
);
293 nsCOMPtr
<nsIAtom
> focusTagAtom
= do_GetAtom(focusTagName
);
295 nsCOMPtr
<nsIDOMElement
> absPosElement
;
296 if (mIsAbsolutelyPositioningEnabled
) {
297 // Absolute Positioning support is enabled, is the selection contained
298 // in an absolutely positioned element ?
299 res
= GetAbsolutelyPositionedSelectionContainer(getter_AddRefs(absPosElement
));
300 NS_ENSURE_SUCCESS(res
, res
);
303 nsCOMPtr
<nsIDOMElement
> cellElement
;
304 if (mIsObjectResizingEnabled
|| mIsInlineTableEditingEnabled
) {
305 // Resizing or Inline Table Editing is enabled, we need to check if the
306 // selection is contained in a table cell
307 res
= GetElementOrParentByTagName(NS_LITERAL_STRING("td"),
309 getter_AddRefs(cellElement
));
310 NS_ENSURE_SUCCESS(res
, res
);
313 if (mIsObjectResizingEnabled
&& cellElement
) {
314 // we are here because Resizing is enabled AND selection is contained in
317 // get the enclosing table
318 if (nsGkAtoms::img
!= focusTagAtom
) {
319 // the element container of the selection is not an image, so we'll show
320 // the resizers around the table
321 nsCOMPtr
<nsIDOMNode
> tableNode
= GetEnclosingTable(cellElement
);
322 focusElement
= do_QueryInterface(tableNode
);
323 focusTagAtom
= nsGkAtoms::table
;
327 // we allow resizers only around images, tables, and absolutely positioned
328 // elements. If we don't have image/table, let's look at the latter case.
329 if (nsGkAtoms::img
!= focusTagAtom
&& nsGkAtoms::table
!= focusTagAtom
) {
330 focusElement
= absPosElement
;
333 // at this point, focusElement contains the element for Resizing,
334 // cellElement contains the element for InlineTableEditing
335 // absPosElement contains the element for Positioning
337 // Note: All the Hide/Show methods below may change attributes on real
338 // content which means a DOMAttrModified handler may cause arbitrary
339 // side effects while this code runs (bug 420439).
341 if (mIsAbsolutelyPositioningEnabled
&& mAbsolutelyPositionedObject
&&
342 absPosElement
!= mAbsolutelyPositionedObject
) {
344 NS_ENSURE_SUCCESS(res
, res
);
345 NS_ASSERTION(!mAbsolutelyPositionedObject
, "HideGrabber failed");
348 if (mIsObjectResizingEnabled
&& mResizedObject
&&
349 mResizedObject
!= focusElement
) {
350 res
= HideResizers();
351 NS_ENSURE_SUCCESS(res
, res
);
352 NS_ASSERTION(!mResizedObject
, "HideResizers failed");
355 if (mIsInlineTableEditingEnabled
&& mInlineEditedCell
&&
356 mInlineEditedCell
!= cellElement
) {
357 res
= HideInlineTableEditingUI();
358 NS_ENSURE_SUCCESS(res
, res
);
359 NS_ASSERTION(!mInlineEditedCell
, "HideInlineTableEditingUI failed");
362 // now, let's display all contextual UI for good
363 nsIContent
* hostContent
= GetActiveEditingHost();
364 nsCOMPtr
<nsIDOMNode
> hostNode
= do_QueryInterface(hostContent
);
366 if (mIsObjectResizingEnabled
&& focusElement
&&
367 IsModifiableNode(focusElement
) && focusElement
!= hostNode
) {
368 if (nsGkAtoms::img
== focusTagAtom
) {
369 mResizedObjectIsAnImage
= true;
372 res
= RefreshResizers();
374 res
= ShowResizers(focusElement
);
375 NS_ENSURE_SUCCESS(res
, res
);
378 if (mIsAbsolutelyPositioningEnabled
&& absPosElement
&&
379 IsModifiableNode(absPosElement
) && absPosElement
!= hostNode
) {
380 if (mAbsolutelyPositionedObject
)
381 res
= RefreshGrabber();
383 res
= ShowGrabberOnElement(absPosElement
);
384 NS_ENSURE_SUCCESS(res
, res
);
387 if (mIsInlineTableEditingEnabled
&& cellElement
&&
388 IsModifiableNode(cellElement
) && cellElement
!= hostNode
) {
389 if (mInlineEditedCell
)
390 res
= RefreshInlineTableEditingUI();
392 res
= ShowInlineTableEditingUI(cellElement
);
398 // Resizing and Absolute Positioning need to know everything about the
399 // containing box of the element: position, size, margins, borders
401 nsHTMLEditor::GetPositionAndDimensions(nsIDOMElement
* aElement
,
402 int32_t & aX
, int32_t & aY
,
403 int32_t & aW
, int32_t & aH
,
404 int32_t & aBorderLeft
,
405 int32_t & aBorderTop
,
406 int32_t & aMarginLeft
,
407 int32_t & aMarginTop
)
409 NS_ENSURE_ARG_POINTER(aElement
);
411 // Is the element positioned ? let's check the cheap way first...
412 bool isPositioned
= false;
413 nsresult res
= aElement
->HasAttribute(NS_LITERAL_STRING("_moz_abspos"), &isPositioned
);
414 NS_ENSURE_SUCCESS(res
, res
);
416 // hmmm... the expensive way now...
417 nsAutoString positionStr
;
418 mHTMLCSSUtils
->GetComputedProperty(aElement
, nsGkAtoms::position
,
420 isPositioned
= positionStr
.EqualsLiteral("absolute");
424 // Yes, it is absolutely positioned
425 mResizedObjectIsAbsolutelyPositioned
= true;
427 // Get the all the computed css styles attached to the element node
428 nsRefPtr
<nsComputedDOMStyle
> cssDecl
=
429 mHTMLCSSUtils
->GetComputedStyle(aElement
);
430 NS_ENSURE_STATE(cssDecl
);
432 aBorderLeft
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("border-left-width"));
433 aBorderTop
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("border-top-width"));
434 aMarginLeft
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("margin-left"));
435 aMarginTop
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("margin-top"));
437 aX
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("left")) +
438 aMarginLeft
+ aBorderLeft
;
439 aY
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("top")) +
440 aMarginTop
+ aBorderTop
;
441 aW
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("width"));
442 aH
= GetCSSFloatValue(cssDecl
, NS_LITERAL_STRING("height"));
445 mResizedObjectIsAbsolutelyPositioned
= false;
446 nsCOMPtr
<nsIDOMHTMLElement
> htmlElement
= do_QueryInterface(aElement
);
448 return NS_ERROR_NULL_POINTER
;
450 GetElementOrigin(aElement
, aX
, aY
);
452 res
= htmlElement
->GetOffsetWidth(&aW
);
453 NS_ENSURE_SUCCESS(res
, res
);
454 res
= htmlElement
->GetOffsetHeight(&aH
);
466 nsHTMLEditor::SetAnonymousElementPosition(int32_t aX
, int32_t aY
, nsIDOMElement
*aElement
)
468 mHTMLCSSUtils
->SetCSSPropertyPixels(aElement
, NS_LITERAL_STRING("left"), aX
);
469 mHTMLCSSUtils
->SetCSSPropertyPixels(aElement
, NS_LITERAL_STRING("top"), aY
);