Bug 1700051: part 30) Narrow scope of `newOffset`. r=smaug
[gecko.git] / editor / libeditor / HTMLEditUtils.cpp
blob21bb840e38a450c212fcc9849316ed2c13c5428d
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "HTMLEditUtils.h"
8 #include "CSSEditUtils.h" // for CSSEditUtils
9 #include "WSRunObject.h" // for WSRunScanner
11 #include "mozilla/ArrayUtils.h" // for ArrayLength
12 #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc.
13 #include "mozilla/EditAction.h" // for EditAction
14 #include "mozilla/EditorBase.h" // for EditorBase, EditorType
15 #include "mozilla/EditorDOMPoint.h" // for EditorDOMPoint, etc.
16 #include "mozilla/EditorUtils.h" // for EditorUtils
17 #include "mozilla/dom/Element.h" // for Element, nsINode
18 #include "mozilla/dom/HTMLAnchorElement.h"
19 #include "mozilla/dom/Text.h" // for Text
21 #include "nsAString.h" // for nsAString::IsEmpty
22 #include "nsAtom.h" // for nsAtom
23 #include "nsCaseTreatment.h"
24 #include "nsCOMPtr.h" // for nsCOMPtr, operator==, etc.
25 #include "nsDebug.h" // for NS_ASSERTION, etc.
26 #include "nsElementTable.h" // for nsHTMLElement
27 #include "nsError.h" // for NS_SUCCEEDED
28 #include "nsGkAtoms.h" // for nsGkAtoms, nsGkAtoms::a, etc.
29 #include "nsHTMLTags.h"
30 #include "nsLiteralString.h" // for NS_LITERAL_STRING
31 #include "nsNameSpaceManager.h" // for kNameSpaceID_None
32 #include "nsString.h" // for nsAutoString
33 #include "nsStyledElement.h"
34 #include "nsTextFragment.h" // for nsTextFragment
36 namespace mozilla {
38 using namespace dom;
39 using EditorType = EditorBase::EditorType;
41 template EditorDOMPoint HTMLEditUtils::GetPreviousEditablePoint(
42 nsIContent& aContent, const Element* aAncestorLimiter,
43 InvisibleWhiteSpaces aInvisibleWhiteSpaces,
44 TableBoundary aHowToTreatTableBoundary);
45 template EditorRawDOMPoint HTMLEditUtils::GetPreviousEditablePoint(
46 nsIContent& aContent, const Element* aAncestorLimiter,
47 InvisibleWhiteSpaces aInvisibleWhiteSpaces,
48 TableBoundary aHowToTreatTableBoundary);
49 template EditorDOMPoint HTMLEditUtils::GetNextEditablePoint(
50 nsIContent& aContent, const Element* aAncestorLimiter,
51 InvisibleWhiteSpaces aInvisibleWhiteSpaces,
52 TableBoundary aHowToTreatTableBoundary);
53 template EditorRawDOMPoint HTMLEditUtils::GetNextEditablePoint(
54 nsIContent& aContent, const Element* aAncestorLimiter,
55 InvisibleWhiteSpaces aInvisibleWhiteSpaces,
56 TableBoundary aHowToTreatTableBoundary);
58 bool HTMLEditUtils::CanContentsBeJoined(const nsIContent& aLeftContent,
59 const nsIContent& aRightContent,
60 StyleDifference aStyleDifference) {
61 if (aLeftContent.NodeInfo()->NameAtom() !=
62 aRightContent.NodeInfo()->NameAtom()) {
63 return false;
65 if (aStyleDifference == StyleDifference::Ignore ||
66 !aLeftContent.IsElement()) {
67 return true;
69 if (aStyleDifference == StyleDifference::CompareIfSpanElements &&
70 !aLeftContent.IsHTMLElement(nsGkAtoms::span)) {
71 return true;
73 if (!aLeftContent.IsElement() || !aRightContent.IsElement()) {
74 return false;
76 nsStyledElement* leftStyledElement =
77 nsStyledElement::FromNode(const_cast<nsIContent*>(&aLeftContent));
78 if (!leftStyledElement) {
79 return false;
81 nsStyledElement* rightStyledElement =
82 nsStyledElement::FromNode(const_cast<nsIContent*>(&aRightContent));
83 if (!rightStyledElement) {
84 return false;
86 return CSSEditUtils::DoStyledElementsHaveSameStyle(*leftStyledElement,
87 *rightStyledElement);
90 bool HTMLEditUtils::IsBlockElement(const nsIContent& aContent) {
91 if (!aContent.IsElement()) {
92 return false;
94 if (aContent.IsHTMLElement(nsGkAtoms::br)) { // shortcut for TextEditor
95 MOZ_ASSERT(!nsHTMLElement::IsBlock(nsHTMLTags::AtomTagToId(nsGkAtoms::br)));
96 return false;
98 // We want to treat these as block nodes even though nsHTMLElement says
99 // they're not.
100 if (aContent.IsAnyOfHTMLElements(
101 nsGkAtoms::body, nsGkAtoms::head, nsGkAtoms::tbody, nsGkAtoms::thead,
102 nsGkAtoms::tfoot, nsGkAtoms::tr, nsGkAtoms::th, nsGkAtoms::td,
103 nsGkAtoms::dt, nsGkAtoms::dd)) {
104 return true;
107 return nsHTMLElement::IsBlock(
108 nsHTMLTags::AtomTagToId(aContent.NodeInfo()->NameAtom()));
112 * IsInlineStyle() returns true if aNode is an inline style.
114 bool HTMLEditUtils::IsInlineStyle(nsINode* aNode) {
115 MOZ_ASSERT(aNode);
116 return aNode->IsAnyOfHTMLElements(
117 nsGkAtoms::b, nsGkAtoms::i, nsGkAtoms::u, nsGkAtoms::tt, nsGkAtoms::s,
118 nsGkAtoms::strike, nsGkAtoms::big, nsGkAtoms::small, nsGkAtoms::sub,
119 nsGkAtoms::sup, nsGkAtoms::font);
122 bool HTMLEditUtils::IsRemovableInlineStyleElement(Element& aElement) {
123 if (!aElement.IsHTMLElement()) {
124 return false;
126 // https://w3c.github.io/editing/execCommand.html#removeformat-candidate
127 if (aElement.IsAnyOfHTMLElements(
128 nsGkAtoms::abbr, // Chrome ignores, but does not make sense.
129 nsGkAtoms::acronym, nsGkAtoms::b,
130 nsGkAtoms::bdi, // Chrome ignores, but does not make sense.
131 nsGkAtoms::bdo, nsGkAtoms::big, nsGkAtoms::cite, nsGkAtoms::code,
132 // nsGkAtoms::del, Chrome ignores, but does not make sense but
133 // execCommand unofficial draft excludes this. Spec issue:
134 // https://github.com/w3c/editing/issues/192
135 nsGkAtoms::dfn, nsGkAtoms::em, nsGkAtoms::font, nsGkAtoms::i,
136 nsGkAtoms::ins, nsGkAtoms::kbd,
137 nsGkAtoms::mark, // Chrome ignores, but does not make sense.
138 nsGkAtoms::nobr, nsGkAtoms::q, nsGkAtoms::s, nsGkAtoms::samp,
139 nsGkAtoms::small, nsGkAtoms::span, nsGkAtoms::strike,
140 nsGkAtoms::strong, nsGkAtoms::sub, nsGkAtoms::sup, nsGkAtoms::tt,
141 nsGkAtoms::u, nsGkAtoms::var)) {
142 return true;
144 // If it's a <blink> element, we can remove it.
145 nsAutoString tagName;
146 aElement.GetTagName(tagName);
147 return tagName.LowerCaseEqualsASCII("blink");
151 * IsFormatNode() returns true if aNode is a format node.
153 bool HTMLEditUtils::IsFormatNode(nsINode* aNode) {
154 MOZ_ASSERT(aNode);
155 return aNode->IsAnyOfHTMLElements(
156 nsGkAtoms::p, nsGkAtoms::pre, nsGkAtoms::h1, nsGkAtoms::h2, nsGkAtoms::h3,
157 nsGkAtoms::h4, nsGkAtoms::h5, nsGkAtoms::h6, nsGkAtoms::address);
161 * IsNodeThatCanOutdent() returns true if aNode is a list, list item or
162 * blockquote.
164 bool HTMLEditUtils::IsNodeThatCanOutdent(nsINode* aNode) {
165 MOZ_ASSERT(aNode);
166 return aNode->IsAnyOfHTMLElements(nsGkAtoms::ul, nsGkAtoms::ol, nsGkAtoms::dl,
167 nsGkAtoms::li, nsGkAtoms::dd, nsGkAtoms::dt,
168 nsGkAtoms::blockquote);
172 * IsHeader() returns true if aNode is an html header.
174 bool HTMLEditUtils::IsHeader(nsINode& aNode) {
175 return aNode.IsAnyOfHTMLElements(nsGkAtoms::h1, nsGkAtoms::h2, nsGkAtoms::h3,
176 nsGkAtoms::h4, nsGkAtoms::h5, nsGkAtoms::h6);
180 * IsListItem() returns true if aNode is an html list item.
182 bool HTMLEditUtils::IsListItem(nsINode* aNode) {
183 MOZ_ASSERT(aNode);
184 return aNode->IsAnyOfHTMLElements(nsGkAtoms::li, nsGkAtoms::dd,
185 nsGkAtoms::dt);
189 * IsAnyTableElement() returns true if aNode is an html table, td, tr, ...
191 bool HTMLEditUtils::IsAnyTableElement(nsINode* aNode) {
192 MOZ_ASSERT(aNode);
193 return aNode->IsAnyOfHTMLElements(
194 nsGkAtoms::table, nsGkAtoms::tr, nsGkAtoms::td, nsGkAtoms::th,
195 nsGkAtoms::thead, nsGkAtoms::tfoot, nsGkAtoms::tbody, nsGkAtoms::caption);
199 * IsAnyTableElementButNotTable() returns true if aNode is an html td, tr, ...
200 * (doesn't include table)
202 bool HTMLEditUtils::IsAnyTableElementButNotTable(nsINode* aNode) {
203 MOZ_ASSERT(aNode);
204 return aNode->IsAnyOfHTMLElements(nsGkAtoms::tr, nsGkAtoms::td, nsGkAtoms::th,
205 nsGkAtoms::thead, nsGkAtoms::tfoot,
206 nsGkAtoms::tbody, nsGkAtoms::caption);
210 * IsTable() returns true if aNode is an html table.
212 bool HTMLEditUtils::IsTable(nsINode* aNode) {
213 return aNode && aNode->IsHTMLElement(nsGkAtoms::table);
217 * IsTableRow() returns true if aNode is an html tr.
219 bool HTMLEditUtils::IsTableRow(nsINode* aNode) {
220 return aNode && aNode->IsHTMLElement(nsGkAtoms::tr);
224 * IsTableCell() returns true if aNode is an html td or th.
226 bool HTMLEditUtils::IsTableCell(nsINode* aNode) {
227 MOZ_ASSERT(aNode);
228 return aNode->IsAnyOfHTMLElements(nsGkAtoms::td, nsGkAtoms::th);
232 * IsTableCellOrCaption() returns true if aNode is an html td or th or caption.
234 bool HTMLEditUtils::IsTableCellOrCaption(nsINode& aNode) {
235 return aNode.IsAnyOfHTMLElements(nsGkAtoms::td, nsGkAtoms::th,
236 nsGkAtoms::caption);
240 * IsAnyListElement() returns true if aNode is an html list.
242 bool HTMLEditUtils::IsAnyListElement(nsINode* aNode) {
243 MOZ_ASSERT(aNode);
244 return aNode->IsAnyOfHTMLElements(nsGkAtoms::ul, nsGkAtoms::ol,
245 nsGkAtoms::dl);
249 * IsPre() returns true if aNode is an html pre node.
251 bool HTMLEditUtils::IsPre(nsINode* aNode) {
252 return aNode && aNode->IsHTMLElement(nsGkAtoms::pre);
256 * IsImage() returns true if aNode is an html image node.
258 bool HTMLEditUtils::IsImage(nsINode* aNode) {
259 return aNode && aNode->IsHTMLElement(nsGkAtoms::img);
262 bool HTMLEditUtils::IsLink(nsINode* aNode) {
263 MOZ_ASSERT(aNode);
265 if (!aNode->IsContent()) {
266 return false;
269 RefPtr<dom::HTMLAnchorElement> anchor =
270 dom::HTMLAnchorElement::FromNodeOrNull(aNode->AsContent());
271 if (!anchor) {
272 return false;
275 nsAutoString tmpText;
276 anchor->GetHref(tmpText);
277 return !tmpText.IsEmpty();
280 bool HTMLEditUtils::IsNamedAnchor(nsINode* aNode) {
281 MOZ_ASSERT(aNode);
282 if (!aNode->IsHTMLElement(nsGkAtoms::a)) {
283 return false;
286 nsAutoString text;
287 return aNode->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::name,
288 text) &&
289 !text.IsEmpty();
293 * IsMozDiv() returns true if aNode is an html div node with |type = _moz|.
295 bool HTMLEditUtils::IsMozDiv(nsINode* aNode) {
296 MOZ_ASSERT(aNode);
297 return aNode->IsHTMLElement(nsGkAtoms::div) &&
298 aNode->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type,
299 u"_moz"_ns, eIgnoreCase);
303 * IsMailCite() returns true if aNode is an html blockquote with |type=cite|.
305 bool HTMLEditUtils::IsMailCite(nsINode* aNode) {
306 MOZ_ASSERT(aNode);
308 // don't ask me why, but our html mailcites are id'd by "type=cite"...
309 if (aNode->IsElement() &&
310 aNode->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::type,
311 u"cite"_ns, eIgnoreCase)) {
312 return true;
315 // ... but our plaintext mailcites by "_moz_quote=true". go figure.
316 if (aNode->IsElement() &&
317 aNode->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::mozquote,
318 u"true"_ns, eIgnoreCase)) {
319 return true;
322 return false;
326 * IsFormWidget() returns true if aNode is a form widget of some kind.
328 bool HTMLEditUtils::IsFormWidget(nsINode* aNode) {
329 MOZ_ASSERT(aNode);
330 return aNode->IsAnyOfHTMLElements(nsGkAtoms::textarea, nsGkAtoms::select,
331 nsGkAtoms::button, nsGkAtoms::output,
332 nsGkAtoms::progress, nsGkAtoms::meter,
333 nsGkAtoms::input);
336 bool HTMLEditUtils::SupportsAlignAttr(nsINode& aNode) {
337 return aNode.IsAnyOfHTMLElements(
338 nsGkAtoms::hr, nsGkAtoms::table, nsGkAtoms::tbody, nsGkAtoms::tfoot,
339 nsGkAtoms::thead, nsGkAtoms::tr, nsGkAtoms::td, nsGkAtoms::th,
340 nsGkAtoms::div, nsGkAtoms::p, nsGkAtoms::h1, nsGkAtoms::h2, nsGkAtoms::h3,
341 nsGkAtoms::h4, nsGkAtoms::h5, nsGkAtoms::h6);
344 bool HTMLEditUtils::IsVisibleTextNode(Text& aText,
345 Element* aEditingHost /* = nullptr */) {
346 if (!aText.TextDataLength()) {
347 return false;
350 if (!aText.TextIsOnlyWhitespace()) {
351 return true;
354 if (!aEditingHost) {
355 aEditingHost = HTMLEditUtils::
356 GetInclusiveAncestorEditableBlockElementOrInlineEditingHost(aText);
358 WSScanResult nextWSScanResult =
359 WSRunScanner::ScanNextVisibleNodeOrBlockBoundary(
360 aEditingHost, EditorRawDOMPoint(&aText, 0));
361 return nextWSScanResult.InNormalWhiteSpacesOrText() &&
362 nextWSScanResult.TextPtr() == &aText;
365 bool HTMLEditUtils::IsInVisibleTextFrames(nsPresContext* aPresContext,
366 Text& aText) {
367 MOZ_ASSERT(aPresContext);
369 nsIFrame* frame = aText.GetPrimaryFrame();
370 if (!frame) {
371 return false;
374 nsCOMPtr<nsISelectionController> selectionController;
375 nsresult rv = frame->GetSelectionController(
376 aPresContext, getter_AddRefs(selectionController));
377 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
378 "nsIFrame::GetSelectionController() failed");
379 if (NS_FAILED(rv) || !selectionController) {
380 return false;
383 if (!aText.TextDataLength()) {
384 return false;
387 // Ask the selection controller for information about whether any of the
388 // data in the node is really rendered. This is really something that
389 // frames know about, but we aren't supposed to talk to frames. So we put
390 // a call in the selection controller interface, since it's already in bed
391 // with frames anyway. (This is a fix for bug 22227, and a partial fix for
392 // bug 46209.)
393 bool isVisible = false;
394 rv = selectionController->CheckVisibilityContent(
395 &aText, 0, aText.TextDataLength(), &isVisible);
396 NS_WARNING_ASSERTION(
397 NS_SUCCEEDED(rv),
398 "nsISelectionController::CheckVisibilityContent() failed");
399 return NS_SUCCEEDED(rv) && isVisible;
402 // We use bitmasks to test containment of elements. Elements are marked to be
403 // in certain groups by setting the mGroup member of the `ElementInfo` struct
404 // to the corresponding GROUP_ values (OR'ed together). Similarly, elements are
405 // marked to allow containment of certain groups by setting the
406 // mCanContainGroups member of the `ElementInfo` struct to the corresponding
407 // GROUP_ values (OR'ed together).
408 // Testing containment then simply consists of checking whether the
409 // mCanContainGroups bitmask of an element and the mGroup bitmask of a
410 // potential child overlap.
412 #define GROUP_NONE 0
414 // body, head, html
415 #define GROUP_TOPLEVEL (1 << 1)
417 // base, link, meta, script, style, title
418 #define GROUP_HEAD_CONTENT (1 << 2)
420 // b, big, i, s, small, strike, tt, u
421 #define GROUP_FONTSTYLE (1 << 3)
423 // abbr, acronym, cite, code, datalist, del, dfn, em, ins, kbd, mark, rb, rp
424 // rt, rtc, ruby, samp, strong, var
425 #define GROUP_PHRASE (1 << 4)
427 // a, applet, basefont, bdi, bdo, br, font, iframe, img, map, meter, object,
428 // output, picture, progress, q, script, span, sub, sup
429 #define GROUP_SPECIAL (1 << 5)
431 // button, form, input, label, select, textarea
432 #define GROUP_FORMCONTROL (1 << 6)
434 // address, applet, article, aside, blockquote, button, center, del, details,
435 // dialog, dir, div, dl, fieldset, figure, footer, form, h1, h2, h3, h4, h5,
436 // h6, header, hgroup, hr, iframe, ins, main, map, menu, nav, noframes,
437 // noscript, object, ol, p, pre, table, section, summary, ul
438 #define GROUP_BLOCK (1 << 7)
440 // frame, frameset
441 #define GROUP_FRAME (1 << 8)
443 // col, tbody
444 #define GROUP_TABLE_CONTENT (1 << 9)
446 // tr
447 #define GROUP_TBODY_CONTENT (1 << 10)
449 // td, th
450 #define GROUP_TR_CONTENT (1 << 11)
452 // col
453 #define GROUP_COLGROUP_CONTENT (1 << 12)
455 // param
456 #define GROUP_OBJECT_CONTENT (1 << 13)
458 // li
459 #define GROUP_LI (1 << 14)
461 // area
462 #define GROUP_MAP_CONTENT (1 << 15)
464 // optgroup, option
465 #define GROUP_SELECT_CONTENT (1 << 16)
467 // option
468 #define GROUP_OPTIONS (1 << 17)
470 // dd, dt
471 #define GROUP_DL_CONTENT (1 << 18)
473 // p
474 #define GROUP_P (1 << 19)
476 // text, white-space, newline, comment
477 #define GROUP_LEAF (1 << 20)
479 // XXX This is because the editor does sublists illegally.
480 // ol, ul
481 #define GROUP_OL_UL (1 << 21)
483 // h1, h2, h3, h4, h5, h6
484 #define GROUP_HEADING (1 << 22)
486 // figcaption
487 #define GROUP_FIGCAPTION (1 << 23)
489 // picture members (img, source)
490 #define GROUP_PICTURE_CONTENT (1 << 24)
492 #define GROUP_INLINE_ELEMENT \
493 (GROUP_FONTSTYLE | GROUP_PHRASE | GROUP_SPECIAL | GROUP_FORMCONTROL | \
494 GROUP_LEAF)
496 #define GROUP_FLOW_ELEMENT (GROUP_INLINE_ELEMENT | GROUP_BLOCK)
498 struct ElementInfo final {
499 #ifdef DEBUG
500 nsHTMLTag mTag;
501 #endif
502 // See `GROUP_NONE`'s comment.
503 uint32_t mGroup;
504 // See `GROUP_NONE`'s comment.
505 uint32_t mCanContainGroups;
506 bool mIsContainer;
507 bool mCanContainSelf;
510 #ifdef DEBUG
511 # define ELEM(_tag, _isContainer, _canContainSelf, _group, _canContainGroups) \
513 eHTMLTag_##_tag, _group, _canContainGroups, _isContainer, \
514 _canContainSelf \
516 #else
517 # define ELEM(_tag, _isContainer, _canContainSelf, _group, _canContainGroups) \
518 { _group, _canContainGroups, _isContainer, _canContainSelf }
519 #endif
521 static const ElementInfo kElements[eHTMLTag_userdefined] = {
522 ELEM(a, true, false, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
523 ELEM(abbr, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
524 ELEM(acronym, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
525 ELEM(address, true, true, GROUP_BLOCK, GROUP_INLINE_ELEMENT | GROUP_P),
526 // While applet is no longer a valid tag, removing it here breaks the editor
527 // (compiles, but causes many tests to fail in odd ways). This list is
528 // tracked against the main HTML Tag list, so any changes will require more
529 // than just removing entries.
530 ELEM(applet, true, true, GROUP_SPECIAL | GROUP_BLOCK,
531 GROUP_FLOW_ELEMENT | GROUP_OBJECT_CONTENT),
532 ELEM(area, false, false, GROUP_MAP_CONTENT, GROUP_NONE),
533 ELEM(article, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
534 ELEM(aside, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
535 ELEM(audio, false, false, GROUP_NONE, GROUP_NONE),
536 ELEM(b, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
537 ELEM(base, false, false, GROUP_HEAD_CONTENT, GROUP_NONE),
538 ELEM(basefont, false, false, GROUP_SPECIAL, GROUP_NONE),
539 ELEM(bdi, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
540 ELEM(bdo, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
541 ELEM(bgsound, false, false, GROUP_NONE, GROUP_NONE),
542 ELEM(big, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
543 ELEM(blockquote, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
544 ELEM(body, true, true, GROUP_TOPLEVEL, GROUP_FLOW_ELEMENT),
545 ELEM(br, false, false, GROUP_SPECIAL, GROUP_NONE),
546 ELEM(button, true, true, GROUP_FORMCONTROL | GROUP_BLOCK,
547 GROUP_FLOW_ELEMENT),
548 ELEM(canvas, false, false, GROUP_NONE, GROUP_NONE),
549 ELEM(caption, true, true, GROUP_NONE, GROUP_INLINE_ELEMENT),
550 ELEM(center, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
551 ELEM(cite, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
552 ELEM(code, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
553 ELEM(col, false, false, GROUP_TABLE_CONTENT | GROUP_COLGROUP_CONTENT,
554 GROUP_NONE),
555 ELEM(colgroup, true, false, GROUP_NONE, GROUP_COLGROUP_CONTENT),
556 ELEM(data, true, false, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
557 ELEM(datalist, true, false, GROUP_PHRASE,
558 GROUP_OPTIONS | GROUP_INLINE_ELEMENT),
559 ELEM(dd, true, false, GROUP_DL_CONTENT, GROUP_FLOW_ELEMENT),
560 ELEM(del, true, true, GROUP_PHRASE | GROUP_BLOCK, GROUP_FLOW_ELEMENT),
561 ELEM(details, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
562 ELEM(dfn, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
563 ELEM(dialog, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
564 ELEM(dir, true, false, GROUP_BLOCK, GROUP_LI),
565 ELEM(div, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
566 ELEM(dl, true, false, GROUP_BLOCK, GROUP_DL_CONTENT),
567 ELEM(dt, true, true, GROUP_DL_CONTENT, GROUP_INLINE_ELEMENT),
568 ELEM(em, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
569 ELEM(embed, false, false, GROUP_NONE, GROUP_NONE),
570 ELEM(fieldset, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
571 ELEM(figcaption, true, false, GROUP_FIGCAPTION, GROUP_FLOW_ELEMENT),
572 ELEM(figure, true, true, GROUP_BLOCK,
573 GROUP_FLOW_ELEMENT | GROUP_FIGCAPTION),
574 ELEM(font, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
575 ELEM(footer, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
576 ELEM(form, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
577 ELEM(frame, false, false, GROUP_FRAME, GROUP_NONE),
578 ELEM(frameset, true, true, GROUP_FRAME, GROUP_FRAME),
579 ELEM(h1, true, false, GROUP_BLOCK | GROUP_HEADING, GROUP_INLINE_ELEMENT),
580 ELEM(h2, true, false, GROUP_BLOCK | GROUP_HEADING, GROUP_INLINE_ELEMENT),
581 ELEM(h3, true, false, GROUP_BLOCK | GROUP_HEADING, GROUP_INLINE_ELEMENT),
582 ELEM(h4, true, false, GROUP_BLOCK | GROUP_HEADING, GROUP_INLINE_ELEMENT),
583 ELEM(h5, true, false, GROUP_BLOCK | GROUP_HEADING, GROUP_INLINE_ELEMENT),
584 ELEM(h6, true, false, GROUP_BLOCK | GROUP_HEADING, GROUP_INLINE_ELEMENT),
585 ELEM(head, true, false, GROUP_TOPLEVEL, GROUP_HEAD_CONTENT),
586 ELEM(header, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
587 ELEM(hgroup, true, false, GROUP_BLOCK, GROUP_HEADING),
588 ELEM(hr, false, false, GROUP_BLOCK, GROUP_NONE),
589 ELEM(html, true, false, GROUP_TOPLEVEL, GROUP_TOPLEVEL),
590 ELEM(i, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
591 ELEM(iframe, true, true, GROUP_SPECIAL | GROUP_BLOCK, GROUP_FLOW_ELEMENT),
592 ELEM(image, false, false, GROUP_NONE, GROUP_NONE),
593 ELEM(img, false, false, GROUP_SPECIAL | GROUP_PICTURE_CONTENT, GROUP_NONE),
594 ELEM(input, false, false, GROUP_FORMCONTROL, GROUP_NONE),
595 ELEM(ins, true, true, GROUP_PHRASE | GROUP_BLOCK, GROUP_FLOW_ELEMENT),
596 ELEM(kbd, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
597 ELEM(keygen, false, false, GROUP_NONE, GROUP_NONE),
598 ELEM(label, true, false, GROUP_FORMCONTROL, GROUP_INLINE_ELEMENT),
599 ELEM(legend, true, true, GROUP_NONE, GROUP_INLINE_ELEMENT),
600 ELEM(li, true, false, GROUP_LI, GROUP_FLOW_ELEMENT),
601 ELEM(link, false, false, GROUP_HEAD_CONTENT, GROUP_NONE),
602 ELEM(listing, true, true, GROUP_BLOCK, GROUP_INLINE_ELEMENT),
603 ELEM(main, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
604 ELEM(map, true, true, GROUP_SPECIAL, GROUP_BLOCK | GROUP_MAP_CONTENT),
605 ELEM(mark, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
606 ELEM(marquee, true, false, GROUP_NONE, GROUP_NONE),
607 ELEM(menu, true, true, GROUP_BLOCK, GROUP_LI | GROUP_FLOW_ELEMENT),
608 ELEM(menuitem, false, false, GROUP_NONE, GROUP_NONE),
609 ELEM(meta, false, false, GROUP_HEAD_CONTENT, GROUP_NONE),
610 ELEM(meter, true, false, GROUP_SPECIAL, GROUP_FLOW_ELEMENT),
611 ELEM(multicol, false, false, GROUP_NONE, GROUP_NONE),
612 ELEM(nav, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
613 ELEM(nobr, true, false, GROUP_NONE, GROUP_NONE),
614 ELEM(noembed, false, false, GROUP_NONE, GROUP_NONE),
615 ELEM(noframes, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
616 ELEM(noscript, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
617 ELEM(object, true, true, GROUP_SPECIAL | GROUP_BLOCK,
618 GROUP_FLOW_ELEMENT | GROUP_OBJECT_CONTENT),
619 // XXX Can contain self and ul because editor does sublists illegally.
620 ELEM(ol, true, true, GROUP_BLOCK | GROUP_OL_UL, GROUP_LI | GROUP_OL_UL),
621 ELEM(optgroup, true, false, GROUP_SELECT_CONTENT, GROUP_OPTIONS),
622 ELEM(option, true, false, GROUP_SELECT_CONTENT | GROUP_OPTIONS, GROUP_LEAF),
623 ELEM(output, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
624 ELEM(p, true, false, GROUP_BLOCK | GROUP_P, GROUP_INLINE_ELEMENT),
625 ELEM(param, false, false, GROUP_OBJECT_CONTENT, GROUP_NONE),
626 ELEM(picture, true, false, GROUP_SPECIAL, GROUP_PICTURE_CONTENT),
627 ELEM(plaintext, false, false, GROUP_NONE, GROUP_NONE),
628 ELEM(pre, true, true, GROUP_BLOCK, GROUP_INLINE_ELEMENT),
629 ELEM(progress, true, false, GROUP_SPECIAL, GROUP_FLOW_ELEMENT),
630 ELEM(q, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
631 ELEM(rb, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
632 ELEM(rp, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
633 ELEM(rt, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
634 ELEM(rtc, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
635 ELEM(ruby, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
636 ELEM(s, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
637 ELEM(samp, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
638 ELEM(script, true, false, GROUP_HEAD_CONTENT | GROUP_SPECIAL, GROUP_LEAF),
639 ELEM(section, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
640 ELEM(select, true, false, GROUP_FORMCONTROL, GROUP_SELECT_CONTENT),
641 ELEM(small, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
642 ELEM(slot, true, false, GROUP_NONE, GROUP_FLOW_ELEMENT),
643 ELEM(source, false, false, GROUP_PICTURE_CONTENT, GROUP_NONE),
644 ELEM(span, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
645 ELEM(strike, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
646 ELEM(strong, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
647 ELEM(style, true, false, GROUP_HEAD_CONTENT, GROUP_LEAF),
648 ELEM(sub, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
649 ELEM(summary, true, true, GROUP_BLOCK, GROUP_FLOW_ELEMENT),
650 ELEM(sup, true, true, GROUP_SPECIAL, GROUP_INLINE_ELEMENT),
651 ELEM(table, true, false, GROUP_BLOCK, GROUP_TABLE_CONTENT),
652 ELEM(tbody, true, false, GROUP_TABLE_CONTENT, GROUP_TBODY_CONTENT),
653 ELEM(td, true, false, GROUP_TR_CONTENT, GROUP_FLOW_ELEMENT),
654 ELEM(textarea, true, false, GROUP_FORMCONTROL, GROUP_LEAF),
655 ELEM(tfoot, true, false, GROUP_NONE, GROUP_TBODY_CONTENT),
656 ELEM(th, true, false, GROUP_TR_CONTENT, GROUP_FLOW_ELEMENT),
657 ELEM(thead, true, false, GROUP_NONE, GROUP_TBODY_CONTENT),
658 ELEM(template, false, false, GROUP_NONE, GROUP_NONE),
659 ELEM(time, true, false, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
660 ELEM(title, true, false, GROUP_HEAD_CONTENT, GROUP_LEAF),
661 ELEM(tr, true, false, GROUP_TBODY_CONTENT, GROUP_TR_CONTENT),
662 ELEM(track, false, false, GROUP_NONE, GROUP_NONE),
663 ELEM(tt, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
664 ELEM(u, true, true, GROUP_FONTSTYLE, GROUP_INLINE_ELEMENT),
665 // XXX Can contain self and ol because editor does sublists illegally.
666 ELEM(ul, true, true, GROUP_BLOCK | GROUP_OL_UL, GROUP_LI | GROUP_OL_UL),
667 ELEM(var, true, true, GROUP_PHRASE, GROUP_INLINE_ELEMENT),
668 ELEM(video, false, false, GROUP_NONE, GROUP_NONE),
669 ELEM(wbr, false, false, GROUP_NONE, GROUP_NONE),
670 ELEM(xmp, true, false, GROUP_BLOCK, GROUP_NONE),
672 // These aren't elements.
673 ELEM(text, false, false, GROUP_LEAF, GROUP_NONE),
674 ELEM(whitespace, false, false, GROUP_LEAF, GROUP_NONE),
675 ELEM(newline, false, false, GROUP_LEAF, GROUP_NONE),
676 ELEM(comment, false, false, GROUP_LEAF, GROUP_NONE),
677 ELEM(entity, false, false, GROUP_NONE, GROUP_NONE),
678 ELEM(doctypeDecl, false, false, GROUP_NONE, GROUP_NONE),
679 ELEM(markupDecl, false, false, GROUP_NONE, GROUP_NONE),
680 ELEM(instruction, false, false, GROUP_NONE, GROUP_NONE),
682 ELEM(userdefined, true, false, GROUP_NONE, GROUP_FLOW_ELEMENT)};
684 bool HTMLEditUtils::CanNodeContain(nsHTMLTag aParentTagId,
685 nsHTMLTag aChildTagId) {
686 NS_ASSERTION(
687 aParentTagId > eHTMLTag_unknown && aParentTagId <= eHTMLTag_userdefined,
688 "aParentTagId out of range!");
689 NS_ASSERTION(
690 aChildTagId > eHTMLTag_unknown && aChildTagId <= eHTMLTag_userdefined,
691 "aChildTagId out of range!");
693 #ifdef DEBUG
694 static bool checked = false;
695 if (!checked) {
696 checked = true;
697 int32_t i;
698 for (i = 1; i <= eHTMLTag_userdefined; ++i) {
699 NS_ASSERTION(kElements[i - 1].mTag == i,
700 "You need to update kElements (missing tags).");
703 #endif
705 // Special-case button.
706 if (aParentTagId == eHTMLTag_button) {
707 static const nsHTMLTag kButtonExcludeKids[] = {
708 eHTMLTag_a, eHTMLTag_fieldset, eHTMLTag_form, eHTMLTag_iframe,
709 eHTMLTag_input, eHTMLTag_select, eHTMLTag_textarea};
711 uint32_t j;
712 for (j = 0; j < ArrayLength(kButtonExcludeKids); ++j) {
713 if (kButtonExcludeKids[j] == aChildTagId) {
714 return false;
719 // Deprecated elements.
720 if (aChildTagId == eHTMLTag_bgsound) {
721 return false;
724 // Bug #67007, dont strip userdefined tags.
725 if (aChildTagId == eHTMLTag_userdefined) {
726 return true;
729 const ElementInfo& parent = kElements[aParentTagId - 1];
730 if (aParentTagId == aChildTagId) {
731 return parent.mCanContainSelf;
734 const ElementInfo& child = kElements[aChildTagId - 1];
735 return !!(parent.mCanContainGroups & child.mGroup);
738 bool HTMLEditUtils::IsContainerNode(nsHTMLTag aTagId) {
739 NS_ASSERTION(aTagId > eHTMLTag_unknown && aTagId <= eHTMLTag_userdefined,
740 "aTagId out of range!");
742 return kElements[aTagId - 1].mIsContainer;
745 bool HTMLEditUtils::IsNonListSingleLineContainer(nsINode& aNode) {
746 return aNode.IsAnyOfHTMLElements(
747 nsGkAtoms::address, nsGkAtoms::div, nsGkAtoms::h1, nsGkAtoms::h2,
748 nsGkAtoms::h3, nsGkAtoms::h4, nsGkAtoms::h5, nsGkAtoms::h6,
749 nsGkAtoms::listing, nsGkAtoms::p, nsGkAtoms::pre, nsGkAtoms::xmp);
752 bool HTMLEditUtils::IsSingleLineContainer(nsINode& aNode) {
753 return IsNonListSingleLineContainer(aNode) ||
754 aNode.IsAnyOfHTMLElements(nsGkAtoms::li, nsGkAtoms::dt, nsGkAtoms::dd);
757 // static
758 template <typename EditorDOMPointType>
759 EditorDOMPointType HTMLEditUtils::GetPreviousEditablePoint(
760 nsIContent& aContent, const Element* aAncestorLimiter,
761 InvisibleWhiteSpaces aInvisibleWhiteSpaces,
762 TableBoundary aHowToTreatTableBoundary) {
763 MOZ_ASSERT(HTMLEditUtils::IsSimplyEditableNode(aContent));
764 NS_ASSERTION(!HTMLEditUtils::IsAnyTableElement(&aContent) ||
765 HTMLEditUtils::IsTableCellOrCaption(aContent),
766 "HTMLEditUtils::GetPreviousEditablePoint() may return a point "
767 "between table structure elements");
769 // First, look for previous content.
770 nsIContent* previousContent = aContent.GetPreviousSibling();
771 if (!previousContent) {
772 if (!aContent.GetParentElement()) {
773 return EditorDOMPointType();
775 nsIContent* inclusiveAncestor = &aContent;
776 for (Element* parentElement : aContent.AncestorsOfType<Element>()) {
777 previousContent = parentElement->GetPreviousSibling();
778 if (!previousContent &&
779 (parentElement == aAncestorLimiter ||
780 !HTMLEditUtils::IsSimplyEditableNode(*parentElement) ||
781 !HTMLEditUtils::CanCrossContentBoundary(*parentElement,
782 aHowToTreatTableBoundary))) {
783 // If cannot cross the parent element boundary, return the point of
784 // last inclusive ancestor point.
785 return EditorDOMPointType(inclusiveAncestor);
788 // Start of the parent element is a next editable point if it's an
789 // element which is not a table structure element.
790 if (!HTMLEditUtils::IsAnyTableElement(parentElement) ||
791 HTMLEditUtils::IsTableCellOrCaption(*parentElement)) {
792 inclusiveAncestor = parentElement;
795 if (!previousContent) {
796 continue; // Keep looking for previous sibling of an ancestor.
799 // XXX Should we ignore data node like CDATA, Comment, etc?
801 // If previous content is not editable, let's return the point after it.
802 if (!HTMLEditUtils::IsSimplyEditableNode(*previousContent)) {
803 return EditorDOMPointType::After(*previousContent);
806 // If cannot cross previous content boundary, return start of last
807 // inclusive ancestor.
808 if (!HTMLEditUtils::CanCrossContentBoundary(*previousContent,
809 aHowToTreatTableBoundary)) {
810 return inclusiveAncestor == &aContent
811 ? EditorDOMPointType(inclusiveAncestor)
812 : EditorDOMPointType(inclusiveAncestor, 0);
814 break;
816 if (!previousContent) {
817 return EditorDOMPointType(inclusiveAncestor);
819 } else if (!HTMLEditUtils::IsSimplyEditableNode(*previousContent)) {
820 return EditorDOMPointType::After(*previousContent);
821 } else if (!HTMLEditUtils::CanCrossContentBoundary(
822 *previousContent, aHowToTreatTableBoundary)) {
823 return EditorDOMPointType(&aContent);
826 // Next, look for end of the previous content.
827 nsIContent* leafContent = previousContent;
828 if (previousContent->GetChildCount() &&
829 HTMLEditUtils::IsContainerNode(*previousContent)) {
830 for (nsIContent* maybeLeafContent = previousContent->GetLastChild();
831 maybeLeafContent;
832 maybeLeafContent = maybeLeafContent->GetLastChild()) {
833 // If it's not an editable content or cannot cross the boundary,
834 // return the point after the content. Note that in this case,
835 // the content must not be any table elements except `<table>`
836 // because we've climbed down the tree.
837 if (!HTMLEditUtils::IsSimplyEditableNode(*maybeLeafContent) ||
838 !HTMLEditUtils::CanCrossContentBoundary(*maybeLeafContent,
839 aHowToTreatTableBoundary)) {
840 return EditorDOMPointType::After(*maybeLeafContent);
842 leafContent = maybeLeafContent;
843 if (!HTMLEditUtils::IsContainerNode(*leafContent)) {
844 break;
849 if (leafContent->IsText()) {
850 Text* textNode = leafContent->AsText();
851 if (aInvisibleWhiteSpaces == InvisibleWhiteSpaces::Preserve) {
852 return EditorDOMPointType::AtEndOf(*textNode);
854 // There may be invisible trailing white-spaces which should be
855 // ignored. Let's scan its start.
856 return WSRunScanner::GetAfterLastVisiblePoint<EditorDOMPointType>(
857 *textNode, aAncestorLimiter);
860 // If it's a container element, return end of it. Otherwise, return
861 // the point after the non-container element.
862 return HTMLEditUtils::IsContainerNode(*leafContent)
863 ? EditorDOMPointType::AtEndOf(*leafContent)
864 : EditorDOMPointType::After(*leafContent);
867 // static
868 template <typename EditorDOMPointType>
869 EditorDOMPointType HTMLEditUtils::GetNextEditablePoint(
870 nsIContent& aContent, const Element* aAncestorLimiter,
871 InvisibleWhiteSpaces aInvisibleWhiteSpaces,
872 TableBoundary aHowToTreatTableBoundary) {
873 MOZ_ASSERT(HTMLEditUtils::IsSimplyEditableNode(aContent));
874 NS_ASSERTION(!HTMLEditUtils::IsAnyTableElement(&aContent) ||
875 HTMLEditUtils::IsTableCellOrCaption(aContent),
876 "HTMLEditUtils::GetPreviousEditablePoint() may return a point "
877 "between table structure elements");
879 // First, look for next content.
880 nsIContent* nextContent = aContent.GetNextSibling();
881 if (!nextContent) {
882 if (!aContent.GetParentElement()) {
883 return EditorDOMPointType();
885 nsIContent* inclusiveAncestor = &aContent;
886 for (Element* parentElement : aContent.AncestorsOfType<Element>()) {
887 // End of the parent element is a next editable point if it's an
888 // element which is not a table structure element.
889 if (!HTMLEditUtils::IsAnyTableElement(parentElement) ||
890 HTMLEditUtils::IsTableCellOrCaption(*parentElement)) {
891 inclusiveAncestor = parentElement;
894 nextContent = parentElement->GetNextSibling();
895 if (!nextContent &&
896 (parentElement == aAncestorLimiter ||
897 !HTMLEditUtils::IsSimplyEditableNode(*parentElement) ||
898 !HTMLEditUtils::CanCrossContentBoundary(*parentElement,
899 aHowToTreatTableBoundary))) {
900 // If cannot cross the parent element boundary, return the point of
901 // last inclusive ancestor point.
902 return EditorDOMPointType(inclusiveAncestor);
905 // End of the parent element is a next editable point if it's an
906 // element which is not a table structure element.
907 if (!HTMLEditUtils::IsAnyTableElement(parentElement) ||
908 HTMLEditUtils::IsTableCellOrCaption(*parentElement)) {
909 inclusiveAncestor = parentElement;
912 if (!nextContent) {
913 continue; // Keep looking for next sibling of an ancestor.
916 // XXX Should we ignore data node like CDATA, Comment, etc?
918 // If next content is not editable, let's return the point after
919 // the last inclusive ancestor.
920 if (!HTMLEditUtils::IsSimplyEditableNode(*nextContent)) {
921 return EditorDOMPointType::After(*parentElement);
924 // If cannot cross next content boundary, return after the last
925 // inclusive ancestor.
926 if (!HTMLEditUtils::CanCrossContentBoundary(*nextContent,
927 aHowToTreatTableBoundary)) {
928 return EditorDOMPointType::After(*inclusiveAncestor);
930 break;
932 if (!nextContent) {
933 return EditorDOMPointType::After(*inclusiveAncestor);
935 } else if (!HTMLEditUtils::IsSimplyEditableNode(*nextContent)) {
936 return EditorDOMPointType::After(aContent);
937 } else if (!HTMLEditUtils::CanCrossContentBoundary(
938 *nextContent, aHowToTreatTableBoundary)) {
939 return EditorDOMPointType::After(aContent);
942 // Next, look for start of the next content.
943 nsIContent* leafContent = nextContent;
944 if (nextContent->GetChildCount() &&
945 HTMLEditUtils::IsContainerNode(*nextContent)) {
946 for (nsIContent* maybeLeafContent = nextContent->GetFirstChild();
947 maybeLeafContent;
948 maybeLeafContent = maybeLeafContent->GetFirstChild()) {
949 // If it's not an editable content or cannot cross the boundary,
950 // return the point at the content (i.e., start of its parent). Note
951 // that in this case, the content must not be any table elements except
952 // `<table>` because we've climbed down the tree.
953 if (!HTMLEditUtils::IsSimplyEditableNode(*maybeLeafContent) ||
954 !HTMLEditUtils::CanCrossContentBoundary(*maybeLeafContent,
955 aHowToTreatTableBoundary)) {
956 return EditorDOMPointType(maybeLeafContent);
958 leafContent = maybeLeafContent;
959 if (!HTMLEditUtils::IsContainerNode(*leafContent)) {
960 break;
965 if (leafContent->IsText()) {
966 Text* textNode = leafContent->AsText();
967 if (aInvisibleWhiteSpaces == InvisibleWhiteSpaces::Preserve) {
968 return EditorDOMPointType(textNode, 0);
970 // There may be invisible leading white-spaces which should be
971 // ignored. Let's scan its start.
972 return WSRunScanner::GetFirstVisiblePoint<EditorDOMPointType>(
973 *textNode, aAncestorLimiter);
976 // If it's a container element, return start of it. Otherwise, return
977 // the point at the non-container element (i.e., start of its parent).
978 return HTMLEditUtils::IsContainerNode(*leafContent)
979 ? EditorDOMPointType(leafContent, 0)
980 : EditorDOMPointType(leafContent);
983 // static
984 Element*
985 HTMLEditUtils::GetInclusiveAncestorEditableBlockElementOrInlineEditingHost(
986 nsIContent& aContent) {
987 MOZ_ASSERT(EditorUtils::IsEditableContent(aContent, EditorType::HTML));
988 Element* maybeInlineEditingHost = nullptr;
989 for (Element* element : aContent.InclusiveAncestorsOfType<Element>()) {
990 if (!EditorUtils::IsEditableContent(*element, EditorType::HTML)) {
991 return maybeInlineEditingHost;
993 if (HTMLEditUtils::IsBlockElement(*element)) {
994 return element;
996 maybeInlineEditingHost = element;
998 return maybeInlineEditingHost;
1001 // static
1002 Element* HTMLEditUtils::GetClosestAncestorAnyListElement(
1003 const nsIContent& aContent) {
1004 for (Element* element : aContent.AncestorsOfType<Element>()) {
1005 if (HTMLEditUtils::IsAnyListElement(element)) {
1006 return element;
1010 return nullptr;
1013 EditAction HTMLEditUtils::GetEditActionForInsert(const nsAtom& aTagName) {
1014 // This method may be in a hot path. So, return only necessary
1015 // EditAction::eInsert*Element.
1016 if (&aTagName == nsGkAtoms::ul) {
1017 // For InputEvent.inputType, "insertUnorderedList".
1018 return EditAction::eInsertUnorderedListElement;
1020 if (&aTagName == nsGkAtoms::ol) {
1021 // For InputEvent.inputType, "insertOrderedList".
1022 return EditAction::eInsertOrderedListElement;
1024 if (&aTagName == nsGkAtoms::hr) {
1025 // For InputEvent.inputType, "insertHorizontalRule".
1026 return EditAction::eInsertHorizontalRuleElement;
1028 return EditAction::eInsertNode;
1031 EditAction HTMLEditUtils::GetEditActionForRemoveList(const nsAtom& aTagName) {
1032 // This method may be in a hot path. So, return only necessary
1033 // EditAction::eRemove*Element.
1034 if (&aTagName == nsGkAtoms::ul) {
1035 // For InputEvent.inputType, "insertUnorderedList".
1036 return EditAction::eRemoveUnorderedListElement;
1038 if (&aTagName == nsGkAtoms::ol) {
1039 // For InputEvent.inputType, "insertOrderedList".
1040 return EditAction::eRemoveOrderedListElement;
1042 return EditAction::eRemoveListElement;
1045 EditAction HTMLEditUtils::GetEditActionForInsert(const Element& aElement) {
1046 return GetEditActionForInsert(*aElement.NodeInfo()->NameAtom());
1049 EditAction HTMLEditUtils::GetEditActionForFormatText(const nsAtom& aProperty,
1050 const nsAtom* aAttribute,
1051 bool aToSetStyle) {
1052 // This method may be in a hot path. So, return only necessary
1053 // EditAction::eSet*Property or EditAction::eRemove*Property.
1054 if (&aProperty == nsGkAtoms::b) {
1055 return aToSetStyle ? EditAction::eSetFontWeightProperty
1056 : EditAction::eRemoveFontWeightProperty;
1058 if (&aProperty == nsGkAtoms::i) {
1059 return aToSetStyle ? EditAction::eSetTextStyleProperty
1060 : EditAction::eRemoveTextStyleProperty;
1062 if (&aProperty == nsGkAtoms::u) {
1063 return aToSetStyle ? EditAction::eSetTextDecorationPropertyUnderline
1064 : EditAction::eRemoveTextDecorationPropertyUnderline;
1066 if (&aProperty == nsGkAtoms::strike) {
1067 return aToSetStyle ? EditAction::eSetTextDecorationPropertyLineThrough
1068 : EditAction::eRemoveTextDecorationPropertyLineThrough;
1070 if (&aProperty == nsGkAtoms::sup) {
1071 return aToSetStyle ? EditAction::eSetVerticalAlignPropertySuper
1072 : EditAction::eRemoveVerticalAlignPropertySuper;
1074 if (&aProperty == nsGkAtoms::sub) {
1075 return aToSetStyle ? EditAction::eSetVerticalAlignPropertySub
1076 : EditAction::eRemoveVerticalAlignPropertySub;
1078 if (&aProperty == nsGkAtoms::font) {
1079 if (aAttribute == nsGkAtoms::face) {
1080 return aToSetStyle ? EditAction::eSetFontFamilyProperty
1081 : EditAction::eRemoveFontFamilyProperty;
1083 if (aAttribute == nsGkAtoms::color) {
1084 return aToSetStyle ? EditAction::eSetColorProperty
1085 : EditAction::eRemoveColorProperty;
1087 if (aAttribute == nsGkAtoms::bgcolor) {
1088 return aToSetStyle ? EditAction::eSetBackgroundColorPropertyInline
1089 : EditAction::eRemoveBackgroundColorPropertyInline;
1092 return aToSetStyle ? EditAction::eSetInlineStyleProperty
1093 : EditAction::eRemoveInlineStyleProperty;
1096 EditAction HTMLEditUtils::GetEditActionForAlignment(
1097 const nsAString& aAlignType) {
1098 // This method may be in a hot path. So, return only necessary
1099 // EditAction::eAlign*.
1100 if (aAlignType.EqualsLiteral("left")) {
1101 return EditAction::eAlignLeft;
1103 if (aAlignType.EqualsLiteral("right")) {
1104 return EditAction::eAlignRight;
1106 if (aAlignType.EqualsLiteral("center")) {
1107 return EditAction::eAlignCenter;
1109 if (aAlignType.EqualsLiteral("justify")) {
1110 return EditAction::eJustify;
1112 return EditAction::eSetAlignment;
1115 } // namespace mozilla