editor/libeditor/html/nsHTMLAnonymousUtils.cpp

changeset 2
7e26c7da4463
equal deleted inserted replaced
-1:000000000000 0:1827880ee6cd
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/. */
4
5 #include "mozilla/Attributes.h"
6 #include "mozilla/dom/Element.h"
7 #include "mozilla/mozalloc.h"
8 #include "nsAString.h"
9 #include "nsAutoPtr.h"
10 #include "nsCOMPtr.h"
11 #include "nsComputedDOMStyle.h"
12 #include "nsDebug.h"
13 #include "nsEditProperty.h"
14 #include "nsError.h"
15 #include "nsHTMLCSSUtils.h"
16 #include "nsHTMLEditor.h"
17 #include "nsIAtom.h"
18 #include "nsIContent.h"
19 #include "nsID.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"
35 #include "nsINode.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"
42 #include "nsString.h"
43 #include "nsStringFwd.h"
44 #include "nsUnicharUtils.h"
45 #include "nscore.h"
46 #include "nsContentUtils.h" // for nsAutoScriptBlocker
47
48 class nsIDOMEventListener;
49 class nsISelection;
50
51 using namespace mozilla;
52
53 // retrieve an integer stored into a CSS computed float value
54 static int32_t GetCSSFloatValue(nsIDOMCSSStyleDeclaration * aDecl,
55 const nsAString & aProperty)
56 {
57 MOZ_ASSERT(aDecl);
58
59 nsCOMPtr<nsIDOMCSSValue> value;
60 // get the computed CSSValue of the property
61 nsresult res = aDecl->GetPropertyCSSValue(aProperty, getter_AddRefs(value));
62 if (NS_FAILED(res) || !value) return 0;
63
64 // check the type of the returned CSSValue; we handle here only
65 // pixel and enum types
66 nsCOMPtr<nsIDOMCSSPrimitiveValue> val = do_QueryInterface(value);
67 uint16_t type;
68 val->GetPrimitiveType(&type);
69
70 float f = 0;
71 switch (type) {
72 case nsIDOMCSSPrimitiveValue::CSS_PX:
73 // the value is in pixels, just get it
74 res = val->GetFloatValue(nsIDOMCSSPrimitiveValue::CSS_PX, &f);
75 NS_ENSURE_SUCCESS(res, 0);
76 break;
77 case nsIDOMCSSPrimitiveValue::CSS_IDENT: {
78 // the value is keyword, we have to map these keywords into
79 // numeric values
80 nsAutoString str;
81 res = val->GetStringValue(str);
82 if (str.EqualsLiteral("thin"))
83 f = 1;
84 else if (str.EqualsLiteral("medium"))
85 f = 3;
86 else if (str.EqualsLiteral("thick"))
87 f = 5;
88 break;
89 }
90 }
91
92 return (int32_t) f;
93 }
94
95 class nsElementDeletionObserver MOZ_FINAL : public nsIMutationObserver
96 {
97 public:
98 nsElementDeletionObserver(nsINode* aNativeAnonNode, nsINode* aObservedNode)
99 : mNativeAnonNode(aNativeAnonNode), mObservedNode(aObservedNode) {}
100 NS_DECL_ISUPPORTS
101 NS_DECL_NSIMUTATIONOBSERVER
102 protected:
103 nsINode* mNativeAnonNode;
104 nsINode* mObservedNode;
105 };
106
107 NS_IMPL_ISUPPORTS(nsElementDeletionObserver, nsIMutationObserver)
108 NS_IMPL_NSIMUTATIONOBSERVER_CONTENT(nsElementDeletionObserver)
109
110 void
111 nsElementDeletionObserver::NodeWillBeDestroyed(const nsINode* aNode)
112 {
113 NS_ASSERTION(aNode == mNativeAnonNode || aNode == mObservedNode,
114 "Wrong aNode!");
115 if (aNode == mNativeAnonNode) {
116 mObservedNode->RemoveMutationObserver(this);
117 } else {
118 mNativeAnonNode->RemoveMutationObserver(this);
119 static_cast<nsIContent*>(mNativeAnonNode)->UnbindFromTree();
120 }
121
122 NS_RELEASE_THIS();
123 }
124
125 // Returns in *aReturn an anonymous nsDOMElement of type aTag,
126 // child of aParentNode. If aIsCreatedHidden is true, the class
127 // "hidden" is added to the created element. If aAnonClass is not
128 // the empty string, it becomes the value of the attribute "_moz_anonclass"
129 nsresult
130 nsHTMLEditor::CreateAnonymousElement(const nsAString & aTag, nsIDOMNode * aParentNode,
131 const nsAString & aAnonClass, bool aIsCreatedHidden,
132 nsIDOMElement ** aReturn)
133 {
134 NS_ENSURE_ARG_POINTER(aParentNode);
135 NS_ENSURE_ARG_POINTER(aReturn);
136 *aReturn = nullptr;
137
138 nsCOMPtr<nsIContent> parentContent( do_QueryInterface(aParentNode) );
139 NS_ENSURE_TRUE(parentContent, NS_OK);
140
141 nsCOMPtr<nsIDocument> doc = GetDocument();
142 NS_ENSURE_TRUE(doc, NS_ERROR_NULL_POINTER);
143
144 // Get the pres shell
145 nsCOMPtr<nsIPresShell> ps = GetPresShell();
146 NS_ENSURE_TRUE(ps, NS_ERROR_NOT_INITIALIZED);
147
148 // Create a new node through the element factory
149 nsCOMPtr<dom::Element> newContent;
150 nsresult res = CreateHTMLContent(aTag, getter_AddRefs(newContent));
151 NS_ENSURE_SUCCESS(res, res);
152
153 nsCOMPtr<nsIDOMElement> newElement = do_QueryInterface(newContent);
154 NS_ENSURE_TRUE(newElement, NS_ERROR_FAILURE);
155
156 // add the "hidden" class if needed
157 if (aIsCreatedHidden) {
158 res = newElement->SetAttribute(NS_LITERAL_STRING("class"),
159 NS_LITERAL_STRING("hidden"));
160 NS_ENSURE_SUCCESS(res, res);
161 }
162
163 // add an _moz_anonclass attribute if needed
164 if (!aAnonClass.IsEmpty()) {
165 res = newElement->SetAttribute(NS_LITERAL_STRING("_moz_anonclass"),
166 aAnonClass);
167 NS_ENSURE_SUCCESS(res, res);
168 }
169
170 {
171 nsAutoScriptBlocker scriptBlocker;
172
173 // establish parenthood of the element
174 newContent->SetIsNativeAnonymousRoot();
175 res = newContent->BindToTree(doc, parentContent, parentContent, true);
176 if (NS_FAILED(res)) {
177 newContent->UnbindFromTree();
178 return res;
179 }
180 }
181
182 nsElementDeletionObserver* observer =
183 new nsElementDeletionObserver(newContent, parentContent);
184 NS_ADDREF(observer); // NodeWillBeDestroyed releases.
185 parentContent->AddMutationObserver(observer);
186 newContent->AddMutationObserver(observer);
187
188 // display the element
189 ps->RecreateFramesFor(newContent);
190
191 newElement.forget(aReturn);
192 return NS_OK;
193 }
194
195 // Removes event listener and calls DeleteRefToAnonymousNode.
196 void
197 nsHTMLEditor::RemoveListenerAndDeleteRef(const nsAString& aEvent,
198 nsIDOMEventListener* aListener,
199 bool aUseCapture,
200 nsIDOMElement* aElement,
201 nsIContent * aParentContent,
202 nsIPresShell* aShell)
203 {
204 nsCOMPtr<nsIDOMEventTarget> evtTarget(do_QueryInterface(aElement));
205 if (evtTarget) {
206 evtTarget->RemoveEventListener(aEvent, aListener, aUseCapture);
207 }
208 DeleteRefToAnonymousNode(aElement, aParentContent, aShell);
209 }
210
211 // Deletes all references to an anonymous element
212 void
213 nsHTMLEditor::DeleteRefToAnonymousNode(nsIDOMElement* aElement,
214 nsIContent* aParentContent,
215 nsIPresShell* aShell)
216 {
217 // call ContentRemoved() for the anonymous content
218 // node so its references get removed from the frame manager's
219 // undisplay map, and its layout frames get destroyed!
220
221 if (aElement) {
222 nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
223 if (content) {
224 nsAutoScriptBlocker scriptBlocker;
225 // Need to check whether aShell has been destroyed (but not yet deleted).
226 // In that case presContext->GetPresShell() returns nullptr.
227 // See bug 338129.
228 if (aShell && aShell->GetPresContext() &&
229 aShell->GetPresContext()->GetPresShell() == aShell) {
230 nsCOMPtr<nsIDocumentObserver> docObserver = do_QueryInterface(aShell);
231 if (docObserver) {
232 // Call BeginUpdate() so that the nsCSSFrameConstructor/PresShell
233 // knows we're messing with the frame tree.
234 nsCOMPtr<nsIDocument> document = GetDocument();
235 if (document)
236 docObserver->BeginUpdate(document, UPDATE_CONTENT_MODEL);
237
238 // XXX This is wrong (bug 439258). Once it's fixed, the NS_WARNING
239 // in RestyleManager::RestyleForRemove should be changed back
240 // to an assertion.
241 docObserver->ContentRemoved(content->GetCurrentDoc(),
242 aParentContent, content, -1,
243 content->GetPreviousSibling());
244 if (document)
245 docObserver->EndUpdate(document, UPDATE_CONTENT_MODEL);
246 }
247 }
248 content->UnbindFromTree();
249 }
250 }
251 }
252
253 // The following method is mostly called by a selection listener. When a
254 // selection change is notified, the method is called to check if resizing
255 // handles, a grabber and/or inline table editing UI need to be displayed
256 // or refreshed
257 NS_IMETHODIMP
258 nsHTMLEditor::CheckSelectionStateForAnonymousButtons(nsISelection * aSelection)
259 {
260 NS_ENSURE_ARG_POINTER(aSelection);
261
262 // early way out if all contextual UI extensions are disabled
263 NS_ENSURE_TRUE(mIsObjectResizingEnabled ||
264 mIsAbsolutelyPositioningEnabled ||
265 mIsInlineTableEditingEnabled, NS_OK);
266
267 // Don't change selection state if we're moving.
268 if (mIsMoving) {
269 return NS_OK;
270 }
271
272 nsCOMPtr<nsIDOMElement> focusElement;
273 // let's get the containing element of the selection
274 nsresult res = GetSelectionContainer(getter_AddRefs(focusElement));
275 NS_ENSURE_TRUE(focusElement, NS_OK);
276 NS_ENSURE_SUCCESS(res, res);
277
278 // If we're not in a document, don't try to add resizers
279 nsCOMPtr<dom::Element> focusElementNode = do_QueryInterface(focusElement);
280 NS_ENSURE_STATE(focusElementNode);
281 if (!focusElementNode->IsInDoc()) {
282 return NS_OK;
283 }
284
285 // what's its tag?
286 nsAutoString focusTagName;
287 res = focusElement->GetTagName(focusTagName);
288 NS_ENSURE_SUCCESS(res, res);
289 ToLowerCase(focusTagName);
290 nsCOMPtr<nsIAtom> focusTagAtom = do_GetAtom(focusTagName);
291
292 nsCOMPtr<nsIDOMElement> absPosElement;
293 if (mIsAbsolutelyPositioningEnabled) {
294 // Absolute Positioning support is enabled, is the selection contained
295 // in an absolutely positioned element ?
296 res = GetAbsolutelyPositionedSelectionContainer(getter_AddRefs(absPosElement));
297 NS_ENSURE_SUCCESS(res, res);
298 }
299
300 nsCOMPtr<nsIDOMElement> cellElement;
301 if (mIsObjectResizingEnabled || mIsInlineTableEditingEnabled) {
302 // Resizing or Inline Table Editing is enabled, we need to check if the
303 // selection is contained in a table cell
304 res = GetElementOrParentByTagName(NS_LITERAL_STRING("td"),
305 nullptr,
306 getter_AddRefs(cellElement));
307 NS_ENSURE_SUCCESS(res, res);
308 }
309
310 if (mIsObjectResizingEnabled && cellElement) {
311 // we are here because Resizing is enabled AND selection is contained in
312 // a cell
313
314 // get the enclosing table
315 if (nsEditProperty::img != focusTagAtom) {
316 // the element container of the selection is not an image, so we'll show
317 // the resizers around the table
318 nsCOMPtr<nsIDOMNode> tableNode = GetEnclosingTable(cellElement);
319 focusElement = do_QueryInterface(tableNode);
320 focusTagAtom = nsEditProperty::table;
321 }
322 }
323
324 // we allow resizers only around images, tables, and absolutely positioned
325 // elements. If we don't have image/table, let's look at the latter case.
326 if (nsEditProperty::img != focusTagAtom &&
327 nsEditProperty::table != focusTagAtom)
328 focusElement = absPosElement;
329
330 // at this point, focusElement contains the element for Resizing,
331 // cellElement contains the element for InlineTableEditing
332 // absPosElement contains the element for Positioning
333
334 // Note: All the Hide/Show methods below may change attributes on real
335 // content which means a DOMAttrModified handler may cause arbitrary
336 // side effects while this code runs (bug 420439).
337
338 if (mIsAbsolutelyPositioningEnabled && mAbsolutelyPositionedObject &&
339 absPosElement != mAbsolutelyPositionedObject) {
340 res = HideGrabber();
341 NS_ENSURE_SUCCESS(res, res);
342 NS_ASSERTION(!mAbsolutelyPositionedObject, "HideGrabber failed");
343 }
344
345 if (mIsObjectResizingEnabled && mResizedObject &&
346 mResizedObject != focusElement) {
347 res = HideResizers();
348 NS_ENSURE_SUCCESS(res, res);
349 NS_ASSERTION(!mResizedObject, "HideResizers failed");
350 }
351
352 if (mIsInlineTableEditingEnabled && mInlineEditedCell &&
353 mInlineEditedCell != cellElement) {
354 res = HideInlineTableEditingUI();
355 NS_ENSURE_SUCCESS(res, res);
356 NS_ASSERTION(!mInlineEditedCell, "HideInlineTableEditingUI failed");
357 }
358
359 // now, let's display all contextual UI for good
360 nsIContent* hostContent = GetActiveEditingHost();
361 nsCOMPtr<nsIDOMNode> hostNode = do_QueryInterface(hostContent);
362
363 if (mIsObjectResizingEnabled && focusElement &&
364 IsModifiableNode(focusElement) && focusElement != hostNode) {
365 if (nsEditProperty::img == focusTagAtom)
366 mResizedObjectIsAnImage = true;
367 if (mResizedObject)
368 res = RefreshResizers();
369 else
370 res = ShowResizers(focusElement);
371 NS_ENSURE_SUCCESS(res, res);
372 }
373
374 if (mIsAbsolutelyPositioningEnabled && absPosElement &&
375 IsModifiableNode(absPosElement) && absPosElement != hostNode) {
376 if (mAbsolutelyPositionedObject)
377 res = RefreshGrabber();
378 else
379 res = ShowGrabberOnElement(absPosElement);
380 NS_ENSURE_SUCCESS(res, res);
381 }
382
383 if (mIsInlineTableEditingEnabled && cellElement &&
384 IsModifiableNode(cellElement) && cellElement != hostNode) {
385 if (mInlineEditedCell)
386 res = RefreshInlineTableEditingUI();
387 else
388 res = ShowInlineTableEditingUI(cellElement);
389 }
390
391 return res;
392 }
393
394 // Resizing and Absolute Positioning need to know everything about the
395 // containing box of the element: position, size, margins, borders
396 nsresult
397 nsHTMLEditor::GetPositionAndDimensions(nsIDOMElement * aElement,
398 int32_t & aX, int32_t & aY,
399 int32_t & aW, int32_t & aH,
400 int32_t & aBorderLeft,
401 int32_t & aBorderTop,
402 int32_t & aMarginLeft,
403 int32_t & aMarginTop)
404 {
405 NS_ENSURE_ARG_POINTER(aElement);
406
407 // Is the element positioned ? let's check the cheap way first...
408 bool isPositioned = false;
409 nsresult res = aElement->HasAttribute(NS_LITERAL_STRING("_moz_abspos"), &isPositioned);
410 NS_ENSURE_SUCCESS(res, res);
411 if (!isPositioned) {
412 // hmmm... the expensive way now...
413 nsAutoString positionStr;
414 mHTMLCSSUtils->GetComputedProperty(aElement, nsEditProperty::cssPosition,
415 positionStr);
416 isPositioned = positionStr.EqualsLiteral("absolute");
417 }
418
419 if (isPositioned) {
420 // Yes, it is absolutely positioned
421 mResizedObjectIsAbsolutelyPositioned = true;
422
423 // Get the all the computed css styles attached to the element node
424 nsRefPtr<nsComputedDOMStyle> cssDecl =
425 mHTMLCSSUtils->GetComputedStyle(aElement);
426 NS_ENSURE_STATE(cssDecl);
427
428 aBorderLeft = GetCSSFloatValue(cssDecl, NS_LITERAL_STRING("border-left-width"));
429 aBorderTop = GetCSSFloatValue(cssDecl, NS_LITERAL_STRING("border-top-width"));
430 aMarginLeft = GetCSSFloatValue(cssDecl, NS_LITERAL_STRING("margin-left"));
431 aMarginTop = GetCSSFloatValue(cssDecl, NS_LITERAL_STRING("margin-top"));
432
433 aX = GetCSSFloatValue(cssDecl, NS_LITERAL_STRING("left")) +
434 aMarginLeft + aBorderLeft;
435 aY = GetCSSFloatValue(cssDecl, NS_LITERAL_STRING("top")) +
436 aMarginTop + aBorderTop;
437 aW = GetCSSFloatValue(cssDecl, NS_LITERAL_STRING("width"));
438 aH = GetCSSFloatValue(cssDecl, NS_LITERAL_STRING("height"));
439 }
440 else {
441 mResizedObjectIsAbsolutelyPositioned = false;
442 nsCOMPtr<nsIDOMHTMLElement> htmlElement = do_QueryInterface(aElement);
443 if (!htmlElement) {
444 return NS_ERROR_NULL_POINTER;
445 }
446 GetElementOrigin(aElement, aX, aY);
447
448 res = htmlElement->GetOffsetWidth(&aW);
449 NS_ENSURE_SUCCESS(res, res);
450 res = htmlElement->GetOffsetHeight(&aH);
451
452 aBorderLeft = 0;
453 aBorderTop = 0;
454 aMarginLeft = 0;
455 aMarginTop = 0;
456 }
457 return res;
458 }
459
460 // self-explanatory
461 void
462 nsHTMLEditor::SetAnonymousElementPosition(int32_t aX, int32_t aY, nsIDOMElement *aElement)
463 {
464 mHTMLCSSUtils->SetCSSPropertyPixels(aElement, NS_LITERAL_STRING("left"), aX);
465 mHTMLCSSUtils->SetCSSPropertyPixels(aElement, NS_LITERAL_STRING("top"), aY);
466 }

mercurial