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 "AutoRangeArray.h" // for AutoRangeArray
9 #include "CSSEditUtils.h" // for CSSEditUtils
10 #include "EditAction.h" // for EditAction
11 #include "EditorBase.h" // for EditorBase, EditorType
12 #include "EditorDOMPoint.h" // for EditorDOMPoint, etc.
13 #include "EditorForwards.h" // for CollectChildrenOptions
14 #include "EditorUtils.h" // for EditorUtils
15 #include "HTMLEditHelpers.h" // for EditorInlineStyle
16 #include "WSRunObject.h" // for WSRunScanner
18 #include "mozilla/ArrayUtils.h" // for ArrayLength
19 #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc.
20 #include "mozilla/Attributes.h"
21 #include "mozilla/StaticPrefs_editor.h" // for StaticPrefs::editor_
22 #include "mozilla/RangeUtils.h" // for RangeUtils
23 #include "mozilla/dom/DocumentInlines.h" // for GetBodyElement()
24 #include "mozilla/dom/Element.h" // for Element, nsINode
25 #include "mozilla/dom/HTMLAnchorElement.h"
26 #include "mozilla/dom/HTMLBodyElement.h"
27 #include "mozilla/dom/HTMLInputElement.h"
28 #include "mozilla/ServoCSSParser.h" // for ServoCSSParser
29 #include "mozilla/dom/StaticRange.h"
30 #include "mozilla/dom/Text.h" // for Text
32 #include "nsAString.h" // for nsAString::IsEmpty
33 #include "nsAtom.h" // for nsAtom
34 #include "nsAttrValue.h" // nsAttrValue
35 #include "nsCaseTreatment.h"
36 #include "nsCOMPtr.h" // for nsCOMPtr, operator==, etc.
37 #include "nsComputedDOMStyle.h" // for nsComputedDOMStyle
38 #include "nsDebug.h" // for NS_ASSERTION, etc.
39 #include "nsElementTable.h" // for nsHTMLElement
40 #include "nsError.h" // for NS_SUCCEEDED
41 #include "nsGkAtoms.h" // for nsGkAtoms, nsGkAtoms::a, etc.
42 #include "nsHTMLTags.h"
43 #include "nsIFrameInlines.h" // for nsIFrame::IsFlexOrGridItem()
44 #include "nsLiteralString.h" // for NS_LITERAL_STRING
45 #include "nsNameSpaceManager.h" // for kNameSpaceID_None
46 #include "nsPrintfCString.h" // nsPringfCString
47 #include "nsString.h" // for nsAutoString
48 #include "nsStyledElement.h"
49 #include "nsStyleStruct.h" // for StyleDisplay
50 #include "nsStyleUtil.h" // for nsStyleUtil
51 #include "nsTextFragment.h" // for nsTextFragment
52 #include "nsTextFrame.h" // for nsTextFrame
57 using EditorType
= EditorBase::EditorType
;
59 template nsIContent
* HTMLEditUtils::GetPreviousContent(
60 const EditorDOMPoint
& aPoint
, const WalkTreeOptions
& aOptions
,
61 BlockInlineCheck aBlockInlineCheck
, const Element
* aAncestorLimiter
);
62 template nsIContent
* HTMLEditUtils::GetPreviousContent(
63 const EditorRawDOMPoint
& aPoint
, const WalkTreeOptions
& aOptions
,
64 BlockInlineCheck aBlockInlineCheck
, const Element
* aAncestorLimiter
);
65 template nsIContent
* HTMLEditUtils::GetPreviousContent(
66 const EditorDOMPointInText
& aPoint
, const WalkTreeOptions
& aOptions
,
67 BlockInlineCheck aBlockInlineCheck
, const Element
* aAncestorLimiter
);
68 template nsIContent
* HTMLEditUtils::GetPreviousContent(
69 const EditorRawDOMPointInText
& aPoint
, const WalkTreeOptions
& aOptions
,
70 BlockInlineCheck aBlockInlineCheck
, const Element
* aAncestorLimiter
);
71 template nsIContent
* HTMLEditUtils::GetNextContent(
72 const EditorDOMPoint
& aPoint
, const WalkTreeOptions
& aOptions
,
73 BlockInlineCheck aBlockInlineCheck
, const Element
* aAncestorLimiter
);
74 template nsIContent
* HTMLEditUtils::GetNextContent(
75 const EditorRawDOMPoint
& aPoint
, const WalkTreeOptions
& aOptions
,
76 BlockInlineCheck aBlockInlineCheck
, const Element
* aAncestorLimiter
);
77 template nsIContent
* HTMLEditUtils::GetNextContent(
78 const EditorDOMPointInText
& aPoint
, const WalkTreeOptions
& aOptions
,
79 BlockInlineCheck aBlockInlineCheck
, const Element
* aAncestorLimiter
);
80 template nsIContent
* HTMLEditUtils::GetNextContent(
81 const EditorRawDOMPointInText
& aPoint
, const WalkTreeOptions
& aOptions
,
82 BlockInlineCheck aBlockInlineCheck
, const Element
* aAncestorLimiter
);
84 template EditorDOMPoint
HTMLEditUtils::GetPreviousEditablePoint(
85 nsIContent
& aContent
, const Element
* aAncestorLimiter
,
86 InvisibleWhiteSpaces aInvisibleWhiteSpaces
,
87 TableBoundary aHowToTreatTableBoundary
);
88 template EditorRawDOMPoint
HTMLEditUtils::GetPreviousEditablePoint(
89 nsIContent
& aContent
, const Element
* aAncestorLimiter
,
90 InvisibleWhiteSpaces aInvisibleWhiteSpaces
,
91 TableBoundary aHowToTreatTableBoundary
);
92 template EditorDOMPoint
HTMLEditUtils::GetNextEditablePoint(
93 nsIContent
& aContent
, const Element
* aAncestorLimiter
,
94 InvisibleWhiteSpaces aInvisibleWhiteSpaces
,
95 TableBoundary aHowToTreatTableBoundary
);
96 template EditorRawDOMPoint
HTMLEditUtils::GetNextEditablePoint(
97 nsIContent
& aContent
, const Element
* aAncestorLimiter
,
98 InvisibleWhiteSpaces aInvisibleWhiteSpaces
,
99 TableBoundary aHowToTreatTableBoundary
);
101 template nsIContent
* HTMLEditUtils::GetContentToPreserveInlineStyles(
102 const EditorDOMPoint
& aPoint
, const Element
& aEditingHost
);
103 template nsIContent
* HTMLEditUtils::GetContentToPreserveInlineStyles(
104 const EditorRawDOMPoint
& aPoint
, const Element
& aEditingHost
);
106 template EditorDOMPoint
HTMLEditUtils::GetBetterInsertionPointFor(
107 const nsIContent
& aContentToInsert
, const EditorDOMPoint
& aPointToInsert
,
108 const Element
& aEditingHost
);
109 template EditorRawDOMPoint
HTMLEditUtils::GetBetterInsertionPointFor(
110 const nsIContent
& aContentToInsert
, const EditorRawDOMPoint
& aPointToInsert
,
111 const Element
& aEditingHost
);
112 template EditorDOMPoint
HTMLEditUtils::GetBetterInsertionPointFor(
113 const nsIContent
& aContentToInsert
, const EditorRawDOMPoint
& aPointToInsert
,
114 const Element
& aEditingHost
);
115 template EditorRawDOMPoint
HTMLEditUtils::GetBetterInsertionPointFor(
116 const nsIContent
& aContentToInsert
, const EditorDOMPoint
& aPointToInsert
,
117 const Element
& aEditingHost
);
119 template EditorDOMPoint
HTMLEditUtils::GetBetterCaretPositionToInsertText(
120 const EditorDOMPoint
& aPoint
, const Element
& aEditingHost
);
121 template EditorDOMPoint
HTMLEditUtils::GetBetterCaretPositionToInsertText(
122 const EditorRawDOMPoint
& aPoint
, const Element
& aEditingHost
);
123 template EditorRawDOMPoint
HTMLEditUtils::GetBetterCaretPositionToInsertText(
124 const EditorDOMPoint
& aPoint
, const Element
& aEditingHost
);
125 template EditorRawDOMPoint
HTMLEditUtils::GetBetterCaretPositionToInsertText(
126 const EditorRawDOMPoint
& aPoint
, const Element
& aEditingHost
);
128 template Result
<EditorDOMPoint
, nsresult
>
129 HTMLEditUtils::ComputePointToPutCaretInElementIfOutside(
130 const Element
& aElement
, const EditorDOMPoint
& aCurrentPoint
);
131 template Result
<EditorRawDOMPoint
, nsresult
>
132 HTMLEditUtils::ComputePointToPutCaretInElementIfOutside(
133 const Element
& aElement
, const EditorDOMPoint
& aCurrentPoint
);
134 template Result
<EditorDOMPoint
, nsresult
>
135 HTMLEditUtils::ComputePointToPutCaretInElementIfOutside(
136 const Element
& aElement
, const EditorRawDOMPoint
& aCurrentPoint
);
137 template Result
<EditorRawDOMPoint
, nsresult
>
138 HTMLEditUtils::ComputePointToPutCaretInElementIfOutside(
139 const Element
& aElement
, const EditorRawDOMPoint
& aCurrentPoint
);
141 template bool HTMLEditUtils::IsSameCSSColorValue(const nsAString
& aColorA
,
142 const nsAString
& aColorB
);
143 template bool HTMLEditUtils::IsSameCSSColorValue(const nsACString
& aColorA
,
144 const nsACString
& aColorB
);
146 bool HTMLEditUtils::CanContentsBeJoined(const nsIContent
& aLeftContent
,
147 const nsIContent
& aRightContent
) {
148 if (aLeftContent
.NodeInfo()->NameAtom() !=
149 aRightContent
.NodeInfo()->NameAtom()) {
153 if (!aLeftContent
.IsElement()) {
154 return true; // can join text nodes, etc
156 MOZ_ASSERT(aRightContent
.IsElement());
158 if (aLeftContent
.NodeInfo()->NameAtom() == nsGkAtoms::font
) {
159 const nsAttrValue
* const leftSize
=
160 aLeftContent
.AsElement()->GetParsedAttr(nsGkAtoms::size
);
161 const nsAttrValue
* const rightSize
=
162 aRightContent
.AsElement()->GetParsedAttr(nsGkAtoms::size
);
163 if (!leftSize
^ !rightSize
|| (leftSize
&& !leftSize
->Equals(*rightSize
))) {
167 const nsAttrValue
* const leftColor
=
168 aLeftContent
.AsElement()->GetParsedAttr(nsGkAtoms::color
);
169 const nsAttrValue
* const rightColor
=
170 aRightContent
.AsElement()->GetParsedAttr(nsGkAtoms::color
);
171 if (!leftColor
^ !rightColor
||
172 (leftColor
&& !leftColor
->Equals(*rightColor
))) {
176 const nsAttrValue
* const leftFace
=
177 aLeftContent
.AsElement()->GetParsedAttr(nsGkAtoms::face
);
178 const nsAttrValue
* const rightFace
=
179 aRightContent
.AsElement()->GetParsedAttr(nsGkAtoms::face
);
180 if (!leftFace
^ !rightFace
|| (leftFace
&& !leftFace
->Equals(*rightFace
))) {
184 nsStyledElement
* leftStyledElement
=
185 nsStyledElement::FromNode(const_cast<nsIContent
*>(&aLeftContent
));
186 if (!leftStyledElement
) {
189 nsStyledElement
* rightStyledElement
=
190 nsStyledElement::FromNode(const_cast<nsIContent
*>(&aRightContent
));
191 if (!rightStyledElement
) {
194 return CSSEditUtils::DoStyledElementsHaveSameStyle(*leftStyledElement
,
195 *rightStyledElement
);
198 static bool IsHTMLBlockElementByDefault(const nsIContent
& aContent
) {
199 if (!aContent
.IsHTMLElement()) {
202 if (aContent
.IsHTMLElement(nsGkAtoms::br
)) { // shortcut for TextEditor
203 MOZ_ASSERT(!nsHTMLElement::IsBlock(
204 nsHTMLTags::CaseSensitiveAtomTagToId(nsGkAtoms::br
)));
207 // We want to treat these as block nodes even though nsHTMLElement says
209 if (aContent
.IsAnyOfHTMLElements(
210 nsGkAtoms::body
, nsGkAtoms::head
, nsGkAtoms::tbody
, nsGkAtoms::thead
,
211 nsGkAtoms::tfoot
, nsGkAtoms::tr
, nsGkAtoms::th
, nsGkAtoms::td
,
212 nsGkAtoms::dt
, nsGkAtoms::dd
)) {
216 return nsHTMLElement::IsBlock(
217 nsHTMLTags::CaseSensitiveAtomTagToId(aContent
.NodeInfo()->NameAtom()));
220 bool HTMLEditUtils::IsBlockElement(const nsIContent
& aContent
,
221 BlockInlineCheck aBlockInlineCheck
) {
222 MOZ_ASSERT(aBlockInlineCheck
!= BlockInlineCheck::Unused
);
224 if (MOZ_UNLIKELY(!aContent
.IsElement())) {
227 // If it's a <br>, we should always treat it as an inline element because
228 // its preceding collapse white-spaces and another <br> works same as usual
229 // even if you set its style to `display:block`.
230 if (aContent
.IsHTMLElement(nsGkAtoms::br
)) {
233 if (!StaticPrefs::editor_block_inline_check_use_computed_style() ||
234 aBlockInlineCheck
== BlockInlineCheck::UseHTMLDefaultStyle
) {
235 return IsHTMLBlockElementByDefault(aContent
);
237 // Let's treat the document element and the body element is a block to avoid
238 // complicated things which may be detected by fuzzing.
239 if (aContent
.OwnerDoc()->GetDocumentElement() == &aContent
||
240 (aContent
.IsHTMLElement(nsGkAtoms::body
) &&
241 aContent
.OwnerDoc()->GetBodyElement() == &aContent
)) {
244 RefPtr
<const ComputedStyle
> elementStyle
=
245 nsComputedDOMStyle::GetComputedStyleNoFlush(aContent
.AsElement());
246 if (MOZ_UNLIKELY(!elementStyle
)) { // If aContent is not in the composed tree
247 return IsHTMLBlockElementByDefault(aContent
);
249 const nsStyleDisplay
* styleDisplay
= elementStyle
->StyleDisplay();
250 if (MOZ_UNLIKELY(styleDisplay
->mDisplay
== StyleDisplay::None
)) {
251 // Typically, we should not keep handling editing in invisible nodes, but if
252 // we reach here, let's fallback to the default style for protecting the
253 // structure as far as possible.
254 return IsHTMLBlockElementByDefault(aContent
);
256 // Both Blink and WebKit treat ruby style as a block, see IsEnclosingBlock()
257 // in Chromium or isBlock() in WebKit.
258 if (styleDisplay
->IsRubyDisplayType()) {
261 // If the outside is not inline, treat it as block.
262 if (!styleDisplay
->IsInlineOutsideStyle()) {
265 // If we're checking display-inside, inline-block, etc should be a block too.
266 return aBlockInlineCheck
== BlockInlineCheck::UseComputedDisplayStyle
&&
267 styleDisplay
->DisplayInside() == StyleDisplayInside::FlowRoot
&&
268 // Treat widgets as inline since they won't hide collapsible
269 // white-spaces around them.
270 styleDisplay
->EffectiveAppearance() == StyleAppearance::None
;
273 bool HTMLEditUtils::IsInlineContent(const nsIContent
& aContent
,
274 BlockInlineCheck aBlockInlineCheck
) {
275 MOZ_ASSERT(aBlockInlineCheck
!= BlockInlineCheck::Unused
);
277 if (!aContent
.IsElement()) {
280 // If it's a <br>, we should always treat it as an inline element because
281 // its preceding collapse white-spaces and another <br> works same as usual
282 // even if you set its style to `display:block`.
283 if (aContent
.IsHTMLElement(nsGkAtoms::br
)) {
286 if (!StaticPrefs::editor_block_inline_check_use_computed_style() ||
287 aBlockInlineCheck
== BlockInlineCheck::UseHTMLDefaultStyle
) {
288 return !IsHTMLBlockElementByDefault(aContent
);
290 // Let's treat the document element and the body element is a block to avoid
291 // complicated things which may be detected by fuzzing.
292 if (aContent
.OwnerDoc()->GetDocumentElement() == &aContent
||
293 (aContent
.IsHTMLElement(nsGkAtoms::body
) &&
294 aContent
.OwnerDoc()->GetBodyElement() == &aContent
)) {
297 RefPtr
<const ComputedStyle
> elementStyle
=
298 nsComputedDOMStyle::GetComputedStyleNoFlush(aContent
.AsElement());
299 if (MOZ_UNLIKELY(!elementStyle
)) { // If aContent is not in the composed tree
300 return !IsHTMLBlockElementByDefault(aContent
);
302 const nsStyleDisplay
* styleDisplay
= elementStyle
->StyleDisplay();
303 if (MOZ_UNLIKELY(styleDisplay
->mDisplay
== StyleDisplay::None
)) {
304 // Similar to IsBlockElement, let's fallback to refer the default style.
305 // Note that if you change here, you may need to check the parent element
306 // style if aContent.
307 return !IsHTMLBlockElementByDefault(aContent
);
309 // Different block IsBlockElement, when the display-outside is inline, it's
310 // simply an inline element.
311 return styleDisplay
->IsInlineOutsideStyle() ||
312 styleDisplay
->IsRubyDisplayType();
315 bool HTMLEditUtils::IsFlexOrGridItem(const Element
& aElement
) {
316 nsIFrame
* frame
= aElement
.GetPrimaryFrame();
317 return frame
&& frame
->IsFlexOrGridItem();
320 bool HTMLEditUtils::IsInclusiveAncestorCSSDisplayNone(
321 const nsIContent
& aContent
) {
322 if (NS_WARN_IF(!aContent
.IsInComposedDoc())) {
325 for (const Element
* element
:
326 aContent
.InclusiveFlatTreeAncestorsOfType
<Element
>()) {
327 RefPtr
<const ComputedStyle
> elementStyle
=
328 nsComputedDOMStyle::GetComputedStyleNoFlush(element
);
329 if (NS_WARN_IF(!elementStyle
)) {
332 const nsStyleDisplay
* styleDisplay
= elementStyle
->StyleDisplay();
333 if (MOZ_UNLIKELY(styleDisplay
->mDisplay
== StyleDisplay::None
)) {
340 bool HTMLEditUtils::IsVisibleElementEvenIfLeafNode(const nsIContent
& aContent
) {
341 if (!aContent
.IsElement()) {
344 // Assume non-HTML element is visible.
345 if (!aContent
.IsHTMLElement()) {
348 // XXX Should we return false if the element is display:none?
349 if (HTMLEditUtils::IsBlockElement(
350 aContent
, BlockInlineCheck::UseComputedDisplayStyle
)) {
353 if (aContent
.IsAnyOfHTMLElements(nsGkAtoms::applet
, nsGkAtoms::iframe
,
354 nsGkAtoms::img
, nsGkAtoms::meter
,
355 nsGkAtoms::progress
, nsGkAtoms::select
,
356 nsGkAtoms::textarea
)) {
359 if (const HTMLInputElement
* inputElement
=
360 HTMLInputElement::FromNode(&aContent
)) {
361 return inputElement
->ControlType() != FormControlType::InputHidden
;
363 // Maybe, empty inline element such as <span>.
368 * IsInlineStyle() returns true if aNode is an inline style.
370 bool HTMLEditUtils::IsInlineStyle(nsINode
* aNode
) {
372 return aNode
->IsAnyOfHTMLElements(
373 nsGkAtoms::b
, nsGkAtoms::i
, nsGkAtoms::u
, nsGkAtoms::tt
, nsGkAtoms::s
,
374 nsGkAtoms::strike
, nsGkAtoms::big
, nsGkAtoms::small
, nsGkAtoms::sub
,
375 nsGkAtoms::sup
, nsGkAtoms::font
);
378 bool HTMLEditUtils::IsDisplayOutsideInline(const Element
& aElement
) {
379 RefPtr
<const ComputedStyle
> elementStyle
=
380 nsComputedDOMStyle::GetComputedStyleNoFlush(&aElement
);
384 return elementStyle
->StyleDisplay()->DisplayOutside() ==
385 StyleDisplayOutside::Inline
;
388 bool HTMLEditUtils::IsDisplayInsideFlowRoot(const Element
& aElement
) {
389 RefPtr
<const ComputedStyle
> elementStyle
=
390 nsComputedDOMStyle::GetComputedStyleNoFlush(&aElement
);
394 return elementStyle
->StyleDisplay()->DisplayInside() ==
395 StyleDisplayInside::FlowRoot
;
398 bool HTMLEditUtils::IsRemovableInlineStyleElement(Element
& aElement
) {
399 if (!aElement
.IsHTMLElement()) {
402 // https://w3c.github.io/editing/execCommand.html#removeformat-candidate
403 if (aElement
.IsAnyOfHTMLElements(
404 nsGkAtoms::abbr
, // Chrome ignores, but does not make sense.
405 nsGkAtoms::acronym
, nsGkAtoms::b
,
406 nsGkAtoms::bdi
, // Chrome ignores, but does not make sense.
407 nsGkAtoms::bdo
, nsGkAtoms::big
, nsGkAtoms::cite
, nsGkAtoms::code
,
408 // nsGkAtoms::del, Chrome ignores, but does not make sense but
409 // execCommand unofficial draft excludes this. Spec issue:
410 // https://github.com/w3c/editing/issues/192
411 nsGkAtoms::dfn
, nsGkAtoms::em
, nsGkAtoms::font
, nsGkAtoms::i
,
412 nsGkAtoms::ins
, nsGkAtoms::kbd
,
413 nsGkAtoms::mark
, // Chrome ignores, but does not make sense.
414 nsGkAtoms::nobr
, nsGkAtoms::q
, nsGkAtoms::s
, nsGkAtoms::samp
,
415 nsGkAtoms::small
, nsGkAtoms::span
, nsGkAtoms::strike
,
416 nsGkAtoms::strong
, nsGkAtoms::sub
, nsGkAtoms::sup
, nsGkAtoms::tt
,
417 nsGkAtoms::u
, nsGkAtoms::var
)) {
420 // If it's a <blink> element, we can remove it.
421 nsAutoString tagName
;
422 aElement
.GetTagName(tagName
);
423 return tagName
.LowerCaseEqualsASCII("blink");
427 * IsNodeThatCanOutdent() returns true if aNode is a list, list item or
430 bool HTMLEditUtils::IsNodeThatCanOutdent(nsINode
* aNode
) {
432 return aNode
->IsAnyOfHTMLElements(nsGkAtoms::ul
, nsGkAtoms::ol
, nsGkAtoms::dl
,
433 nsGkAtoms::li
, nsGkAtoms::dd
, nsGkAtoms::dt
,
434 nsGkAtoms::blockquote
);
438 * IsHeader() returns true if aNode is an html header.
440 bool HTMLEditUtils::IsHeader(nsINode
& aNode
) {
441 return aNode
.IsAnyOfHTMLElements(nsGkAtoms::h1
, nsGkAtoms::h2
, nsGkAtoms::h3
,
442 nsGkAtoms::h4
, nsGkAtoms::h5
, nsGkAtoms::h6
);
446 * IsListItem() returns true if aNode is an html list item.
448 bool HTMLEditUtils::IsListItem(const nsINode
* aNode
) {
450 return aNode
->IsAnyOfHTMLElements(nsGkAtoms::li
, nsGkAtoms::dd
,
455 * IsAnyTableElement() returns true if aNode is an html table, td, tr, ...
457 bool HTMLEditUtils::IsAnyTableElement(const nsINode
* aNode
) {
459 return aNode
->IsAnyOfHTMLElements(
460 nsGkAtoms::table
, nsGkAtoms::tr
, nsGkAtoms::td
, nsGkAtoms::th
,
461 nsGkAtoms::thead
, nsGkAtoms::tfoot
, nsGkAtoms::tbody
, nsGkAtoms::caption
);
465 * IsAnyTableElementButNotTable() returns true if aNode is an html td, tr, ...
466 * (doesn't include table)
468 bool HTMLEditUtils::IsAnyTableElementButNotTable(nsINode
* aNode
) {
470 return aNode
->IsAnyOfHTMLElements(nsGkAtoms::tr
, nsGkAtoms::td
, nsGkAtoms::th
,
471 nsGkAtoms::thead
, nsGkAtoms::tfoot
,
472 nsGkAtoms::tbody
, nsGkAtoms::caption
);
476 * IsTable() returns true if aNode is an html table.
478 bool HTMLEditUtils::IsTable(nsINode
* aNode
) {
479 return aNode
&& aNode
->IsHTMLElement(nsGkAtoms::table
);
483 * IsTableRow() returns true if aNode is an html tr.
485 bool HTMLEditUtils::IsTableRow(nsINode
* aNode
) {
486 return aNode
&& aNode
->IsHTMLElement(nsGkAtoms::tr
);
490 * IsTableCell() returns true if aNode is an html td or th.
492 bool HTMLEditUtils::IsTableCell(const nsINode
* aNode
) {
494 return aNode
->IsAnyOfHTMLElements(nsGkAtoms::td
, nsGkAtoms::th
);
498 * IsTableCellOrCaption() returns true if aNode is an html td or th or caption.
500 bool HTMLEditUtils::IsTableCellOrCaption(nsINode
& aNode
) {
501 return aNode
.IsAnyOfHTMLElements(nsGkAtoms::td
, nsGkAtoms::th
,
506 * IsAnyListElement() returns true if aNode is an html list.
508 bool HTMLEditUtils::IsAnyListElement(const nsINode
* aNode
) {
510 return aNode
->IsAnyOfHTMLElements(nsGkAtoms::ul
, nsGkAtoms::ol
,
515 * IsPre() returns true if aNode is an html pre node.
517 bool HTMLEditUtils::IsPre(const nsINode
* aNode
) {
518 return aNode
&& aNode
->IsHTMLElement(nsGkAtoms::pre
);
522 * IsImage() returns true if aNode is an html image node.
524 bool HTMLEditUtils::IsImage(nsINode
* aNode
) {
525 return aNode
&& aNode
->IsHTMLElement(nsGkAtoms::img
);
528 bool HTMLEditUtils::IsLink(const nsINode
* aNode
) {
531 if (!aNode
->IsContent()) {
535 RefPtr
<const dom::HTMLAnchorElement
> anchor
=
536 dom::HTMLAnchorElement::FromNodeOrNull(aNode
->AsContent());
541 nsAutoCString tmpText
;
542 anchor
->GetHref(tmpText
);
543 return !tmpText
.IsEmpty();
546 bool HTMLEditUtils::IsNamedAnchor(const nsINode
* aNode
) {
548 if (!aNode
->IsHTMLElement(nsGkAtoms::a
)) {
553 return aNode
->AsElement()->GetAttr(nsGkAtoms::name
, text
) && !text
.IsEmpty();
557 * IsMozDiv() returns true if aNode is an html div node with |type = _moz|.
559 bool HTMLEditUtils::IsMozDiv(nsINode
* aNode
) {
561 return aNode
->IsHTMLElement(nsGkAtoms::div
) &&
562 aNode
->AsElement()->AttrValueIs(kNameSpaceID_None
, nsGkAtoms::type
,
563 u
"_moz"_ns
, eIgnoreCase
);
567 * IsMailCite() returns true if aNode is an html blockquote with |type=cite|.
569 bool HTMLEditUtils::IsMailCite(const Element
& aElement
) {
570 // don't ask me why, but our html mailcites are id'd by "type=cite"...
571 if (aElement
.AttrValueIs(kNameSpaceID_None
, nsGkAtoms::type
, u
"cite"_ns
,
576 // ... but our plaintext mailcites by "_moz_quote=true". go figure.
577 if (aElement
.AttrValueIs(kNameSpaceID_None
, nsGkAtoms::mozquote
, u
"true"_ns
,
586 * IsFormWidget() returns true if aNode is a form widget of some kind.
588 bool HTMLEditUtils::IsFormWidget(const nsINode
* aNode
) {
590 return aNode
->IsAnyOfHTMLElements(nsGkAtoms::textarea
, nsGkAtoms::select
,
591 nsGkAtoms::button
, nsGkAtoms::output
,
592 nsGkAtoms::progress
, nsGkAtoms::meter
,
596 bool HTMLEditUtils::SupportsAlignAttr(nsINode
& aNode
) {
597 return aNode
.IsAnyOfHTMLElements(
598 nsGkAtoms::hr
, nsGkAtoms::table
, nsGkAtoms::tbody
, nsGkAtoms::tfoot
,
599 nsGkAtoms::thead
, nsGkAtoms::tr
, nsGkAtoms::td
, nsGkAtoms::th
,
600 nsGkAtoms::div
, nsGkAtoms::p
, nsGkAtoms::h1
, nsGkAtoms::h2
, nsGkAtoms::h3
,
601 nsGkAtoms::h4
, nsGkAtoms::h5
, nsGkAtoms::h6
);
604 bool HTMLEditUtils::IsVisibleTextNode(const Text
& aText
) {
605 if (!aText
.TextDataLength()) {
609 Maybe
<uint32_t> visibleCharOffset
=
610 HTMLEditUtils::GetInclusiveNextNonCollapsibleCharOffset(
611 EditorDOMPointInText(&aText
, 0));
612 if (visibleCharOffset
.isSome()) {
616 // Now, all characters in aText is collapsible white-spaces. The node is
617 // invisible if next to block boundary.
618 return !HTMLEditUtils::GetElementOfImmediateBlockBoundary(
619 aText
, WalkTreeDirection::Forward
) &&
620 !HTMLEditUtils::GetElementOfImmediateBlockBoundary(
621 aText
, WalkTreeDirection::Backward
);
624 bool HTMLEditUtils::IsInVisibleTextFrames(nsPresContext
* aPresContext
,
626 // TODO(dholbert): aPresContext is now unused; maybe we can remove it, here
627 // and in IsEmptyNode? We do use it as a signal (implicitly here,
628 // more-explicitly in IsEmptyNode) that we are in a "SafeToAskLayout" case...
629 // If/when we remove it, we should be sure we're not losing that signal of
630 // strictness, since this function here does absolutely need to query layout.
631 MOZ_ASSERT(aPresContext
);
633 if (!aText
.TextDataLength()) {
637 nsTextFrame
* textFrame
= do_QueryFrame(aText
.GetPrimaryFrame());
642 return textFrame
->HasVisibleText();
645 Element
* HTMLEditUtils::GetElementOfImmediateBlockBoundary(
646 const nsIContent
& aContent
, const WalkTreeDirection aDirection
) {
647 MOZ_ASSERT(aContent
.IsHTMLElement(nsGkAtoms::br
) || aContent
.IsText());
649 // First, we get a block container. This is not designed for reaching
650 // no block boundaries in the tree.
651 Element
* maybeNonEditableAncestorBlock
= HTMLEditUtils::GetAncestorElement(
652 aContent
, HTMLEditUtils::ClosestBlockElement
,
653 BlockInlineCheck::UseComputedDisplayStyle
);
654 if (NS_WARN_IF(!maybeNonEditableAncestorBlock
)) {
658 auto getNextContent
= [&aDirection
, &maybeNonEditableAncestorBlock
](
659 const nsIContent
& aContent
) -> nsIContent
* {
660 return aDirection
== WalkTreeDirection::Forward
661 ? HTMLEditUtils::GetNextContent(
663 {WalkTreeOption::IgnoreDataNodeExceptText
,
664 WalkTreeOption::StopAtBlockBoundary
},
665 BlockInlineCheck::UseComputedDisplayStyle
,
666 maybeNonEditableAncestorBlock
)
667 : HTMLEditUtils::GetPreviousContent(
669 {WalkTreeOption::IgnoreDataNodeExceptText
,
670 WalkTreeOption::StopAtBlockBoundary
},
671 BlockInlineCheck::UseComputedDisplayStyle
,
672 maybeNonEditableAncestorBlock
);
675 // Then, scan block element boundary while we don't see visible things.
676 const bool isBRElement
= aContent
.IsHTMLElement(nsGkAtoms::br
);
677 for (nsIContent
* nextContent
= getNextContent(aContent
); nextContent
;
678 nextContent
= getNextContent(*nextContent
)) {
679 if (nextContent
->IsElement()) {
680 // Break is right before a child block, it's not visible
681 if (HTMLEditUtils::IsBlockElement(
682 *nextContent
, BlockInlineCheck::UseComputedDisplayStyle
)) {
683 return nextContent
->AsElement();
686 // XXX How about other non-HTML elements? Assume they are styled as
688 if (!nextContent
->IsHTMLElement()) {
689 return nextContent
->AsElement();
692 // If there is a visible content which generates something visible,
694 if (HTMLEditUtils::IsVisibleElementEvenIfLeafNode(*nextContent
)) {
698 if (nextContent
->IsHTMLElement(nsGkAtoms::br
)) {
699 // If aContent is a <br> element, another <br> element prevents the
700 // block boundary special handling.
705 MOZ_ASSERT(aContent
.IsText());
706 // Following <br> element always hides its following block boundary.
707 // I.e., white-spaces is at end of the text node is visible.
708 if (aDirection
== WalkTreeDirection::Forward
) {
711 // Otherwise, if text node follows <br> element, its white-spaces at
712 // start of the text node are invisible. In this case, we return
713 // the found <br> element.
714 return nextContent
->AsElement();
720 switch (nextContent
->NodeType()) {
721 case nsINode::TEXT_NODE
:
722 case nsINode::CDATA_SECTION_NODE
:
728 Text
* textNode
= Text::FromNode(nextContent
);
729 MOZ_ASSERT(textNode
);
730 if (!textNode
->TextLength()) {
731 continue; // empty invisible text node, keep scanning next one.
733 if (HTMLEditUtils::IsInclusiveAncestorCSSDisplayNone(*textNode
)) {
734 continue; // Styled as invisible.
736 if (!textNode
->TextIsOnlyWhitespace()) {
737 return nullptr; // found a visible text node.
739 const nsTextFragment
& textFragment
= textNode
->TextFragment();
740 const bool isWhiteSpacePreformatted
=
741 EditorUtils::IsWhiteSpacePreformatted(*textNode
);
742 const bool isNewLinePreformatted
=
743 EditorUtils::IsNewLinePreformatted(*textNode
);
744 if (!isWhiteSpacePreformatted
&& !isNewLinePreformatted
) {
745 // if the white-space only text node is not preformatted, ignore it.
748 for (uint32_t i
= 0; i
< textFragment
.GetLength(); i
++) {
749 if (textFragment
.CharAt(i
) == HTMLEditUtils::kNewLine
) {
750 if (isNewLinePreformatted
) {
751 return nullptr; // found a visible text node.
755 if (isWhiteSpacePreformatted
) {
756 return nullptr; // found a visible text node.
759 // All white-spaces in the text node is invisible, keep scanning next one.
762 // There is no visible content and reached current block boundary. Then,
763 // the <br> element is the last content in the block and invisible.
764 // XXX Should we treat it visible if it's the only child of a block?
765 return maybeNonEditableAncestorBlock
;
768 nsIContent
* HTMLEditUtils::GetUnnecessaryLineBreakContent(
769 const Element
& aBlockElement
, ScanLineBreak aScanLineBreak
) {
770 auto* lastLineBreakContent
= [&]() -> nsIContent
* {
771 const LeafNodeTypes leafNodeOrNonEditableNode
{
772 LeafNodeType::LeafNodeOrNonEditableNode
};
773 const WalkTreeOptions onlyPrecedingLine
{
774 WalkTreeOption::StopAtBlockBoundary
};
775 for (nsIContent
* content
=
776 aScanLineBreak
== ScanLineBreak::AtEndOfBlock
777 ? HTMLEditUtils::GetLastLeafContent(aBlockElement
,
778 leafNodeOrNonEditableNode
)
779 : HTMLEditUtils::GetPreviousContent(
780 aBlockElement
, onlyPrecedingLine
,
781 BlockInlineCheck::UseComputedDisplayStyle
,
782 aBlockElement
.GetParentElement());
785 aScanLineBreak
== ScanLineBreak::AtEndOfBlock
786 ? HTMLEditUtils::GetPreviousLeafContentOrPreviousBlockElement(
787 *content
, aBlockElement
, leafNodeOrNonEditableNode
,
788 BlockInlineCheck::UseComputedDisplayStyle
)
789 : HTMLEditUtils::GetPreviousContent(
790 *content
, onlyPrecedingLine
,
791 BlockInlineCheck::UseComputedDisplayStyle
,
792 aBlockElement
.GetParentElement())) {
793 // If we're scanning preceding <br> element of aBlockElement, we don't
794 // need to look for a line break in another block because the caller
795 // needs to handle only preceding <br> element of aBlockElement.
796 if (aScanLineBreak
== ScanLineBreak::BeforeBlock
&&
797 HTMLEditUtils::IsBlockElement(
798 *content
, BlockInlineCheck::UseComputedDisplayStyle
)) {
801 if (Text
* textNode
= Text::FromNode(content
)) {
802 if (!textNode
->TextLength()) {
803 continue; // ignore empty text node
805 const nsTextFragment
& textFragment
= textNode
->TextFragment();
806 if (EditorUtils::IsNewLinePreformatted(*textNode
) &&
807 textFragment
.CharAt(textFragment
.GetLength() - 1u) ==
808 HTMLEditUtils::kNewLine
) {
809 // If the text node ends with a preserved line break, it's unnecessary
810 // unless it follows another preformatted line break.
811 if (textFragment
.GetLength() == 1u) {
812 return textNode
; // Need to scan previous leaf.
814 return textFragment
.CharAt(textFragment
.GetLength() - 2u) ==
815 HTMLEditUtils::kNewLine
819 if (HTMLEditUtils::IsVisibleTextNode(*textNode
)) {
824 if (content
->IsCharacterData()) {
825 continue; // ignore hidden character data nodes like comment
827 if (content
->IsHTMLElement(nsGkAtoms::br
)) {
830 // If found element is empty block or visible element, there is no
831 // unnecessary line break.
832 if (HTMLEditUtils::IsVisibleElementEvenIfLeafNode(*content
)) {
835 // Otherwise, e.g., empty <b>, we should keep scanning.
839 if (!lastLineBreakContent
) {
843 // If the found node is a text node and contains only one preformatted new
844 // line break, we need to keep scanning previous one, but if it has 2 or more
845 // characters, we know it has redundant line break.
846 Text
* lastLineBreakText
= Text::FromNode(lastLineBreakContent
);
847 if (lastLineBreakText
&& lastLineBreakText
->TextDataLength() != 1u) {
848 return lastLineBreakText
;
851 // Scan previous leaf content, but now, we can stop at child block boundary.
852 const LeafNodeTypes leafNodeOrNonEditableNodeOrChildBlock
{
853 LeafNodeType::LeafNodeOrNonEditableNode
,
854 LeafNodeType::LeafNodeOrChildBlock
};
855 const Element
* blockElement
= HTMLEditUtils::GetAncestorElement(
856 *lastLineBreakContent
, HTMLEditUtils::ClosestBlockElement
,
857 BlockInlineCheck::UseComputedDisplayStyle
);
858 for (nsIContent
* content
=
859 HTMLEditUtils::GetPreviousLeafContentOrPreviousBlockElement(
860 *lastLineBreakContent
, *blockElement
,
861 leafNodeOrNonEditableNodeOrChildBlock
,
862 BlockInlineCheck::UseComputedDisplayStyle
);
864 content
= HTMLEditUtils::GetPreviousLeafContentOrPreviousBlockElement(
865 *content
, *blockElement
, leafNodeOrNonEditableNodeOrChildBlock
,
866 BlockInlineCheck::UseComputedDisplayStyle
)) {
867 if (HTMLEditUtils::IsBlockElement(
868 *content
, BlockInlineCheck::UseComputedDisplayStyle
) ||
869 (content
->IsElement() && !content
->IsHTMLElement())) {
870 // Now, must found <div>...<div>...</div><br></div>
872 // In this case, the <br> element is necessary to make a following empty
873 // line of the inner <div> visible.
876 if (Text
* textNode
= Text::FromNode(content
)) {
877 if (!textNode
->TextLength()) {
878 continue; // ignore empty text node
880 const nsTextFragment
& textFragment
= textNode
->TextFragment();
881 if (EditorUtils::IsNewLinePreformatted(*textNode
) &&
882 textFragment
.CharAt(textFragment
.GetLength() - 1u) ==
883 HTMLEditUtils::kNewLine
) {
884 // So, we are here because the preformatted line break is followed by
885 // lastLineBreakContent which is <br> or a text node containing only
886 // one. In this case, even if their parents are different,
887 // lastLineBreakContent is necessary to make the last line visible.
890 if (!HTMLEditUtils::IsVisibleTextNode(*textNode
)) {
893 if (EditorUtils::IsWhiteSpacePreformatted(*textNode
)) {
894 // If the white-space is preserved, neither following <br> nor a
895 // preformatted line break is not necessary.
896 return lastLineBreakContent
;
898 // Otherwise, only if the last character is a collapsible white-space,
899 // we need lastLineBreakContent to make the trailing white-space visible.
900 switch (textFragment
.CharAt(textFragment
.GetLength() - 1u)) {
901 case HTMLEditUtils::kSpace
:
902 case HTMLEditUtils::kCarriageReturn
:
903 case HTMLEditUtils::kTab
:
904 case HTMLEditUtils::kNBSP
:
907 return lastLineBreakContent
;
910 if (content
->IsCharacterData()) {
911 continue; // ignore hidden character data nodes like comment
913 // If lastLineBreakContent follows a <br> element in same block, it's
914 // necessary to make the empty last line visible.
915 if (content
->IsHTMLElement(nsGkAtoms::br
)) {
918 // If it follows a visible element, it's unnecessary line break.
919 if (HTMLEditUtils::IsVisibleElementEvenIfLeafNode(*content
)) {
920 return lastLineBreakContent
;
922 // Otherwise, ignore empty inline elements such as <b>.
924 // If the block is empty except invisible data nodes and lastLineBreakContent,
925 // lastLineBreakContent is necessary to make the block visible.
929 bool HTMLEditUtils::IsEmptyNode(nsPresContext
* aPresContext
,
930 const nsINode
& aNode
,
931 const EmptyCheckOptions
& aOptions
/* = {} */,
932 bool* aSeenBR
/* = nullptr */) {
933 MOZ_ASSERT_IF(aOptions
.contains(EmptyCheckOption::SafeToAskLayout
),
940 if (const Text
* text
= Text::FromNode(&aNode
)) {
941 return aOptions
.contains(EmptyCheckOption::SafeToAskLayout
)
942 ? !IsInVisibleTextFrames(aPresContext
, *text
)
943 : !IsVisibleTextNode(*text
);
946 if (!aNode
.IsElement()) {
951 // If it's not a container such as an <hr> or <br>, etc, it should be
952 // treated as not empty.
953 !IsContainerNode(*aNode
.AsContent()) ||
954 // If it's a named anchor, we shouldn't treat it as empty because it
955 // has special meaning even if invisible.
956 IsNamedAnchor(&aNode
) ||
957 // Form widgets should be treated as not empty because they have special
958 // meaning even if invisible.
959 IsFormWidget(&aNode
)) {
963 const auto [isListItem
, isTableCell
, hasAppearance
] =
964 [&]() MOZ_NEVER_INLINE_DEBUG
-> std::tuple
<bool, bool, bool> {
965 if (!StaticPrefs::editor_block_inline_check_use_computed_style()) {
966 return {IsListItem(&aNode
), IsTableCell(&aNode
), false};
968 // Let's stop treating the document element and the <body> as a list item
969 // nor a table cell to avoid tricky cases.
970 if (aNode
.OwnerDoc()->GetDocumentElement() == &aNode
||
971 (aNode
.IsHTMLElement(nsGkAtoms::body
) &&
972 aNode
.OwnerDoc()->GetBodyElement() == &aNode
)) {
973 return {false, false, false};
976 RefPtr
<const ComputedStyle
> elementStyle
=
977 nsComputedDOMStyle::GetComputedStyleNoFlush(aNode
.AsElement());
978 // If there is no style information like in a document fragment, let's refer
979 // the default style.
980 if (MOZ_UNLIKELY(!elementStyle
)) {
981 return {IsListItem(&aNode
), IsTableCell(&aNode
), false};
983 const nsStyleDisplay
* styleDisplay
= elementStyle
->StyleDisplay();
984 if (NS_WARN_IF(!styleDisplay
)) {
985 return {IsListItem(&aNode
), IsTableCell(&aNode
), false};
987 if (styleDisplay
->mDisplay
!= StyleDisplay::None
&&
988 styleDisplay
->HasAppearance()) {
989 return {false, false, true};
991 if (styleDisplay
->IsListItem()) {
992 return {true, false, false};
994 if (styleDisplay
->mDisplay
== StyleDisplay::TableCell
) {
995 return {false, true, false};
997 // The default display of <dt> and <dd> is block. Therefore, we need
998 // special handling for them.
999 return {styleDisplay
->mDisplay
== StyleDisplay::Block
&&
1000 aNode
.IsAnyOfHTMLElements(nsGkAtoms::dd
, nsGkAtoms::dt
),
1004 // The web author created native widget without form control elements. Let's
1005 // treat it as visible.
1006 if (hasAppearance
) {
1011 aOptions
.contains(EmptyCheckOption::TreatListItemAsVisible
)) {
1015 aOptions
.contains(EmptyCheckOption::TreatTableCellAsVisible
)) {
1019 bool seenBR
= aSeenBR
&& *aSeenBR
;
1020 for (nsIContent
* childContent
= aNode
.GetFirstChild(); childContent
;
1021 childContent
= childContent
->GetNextSibling()) {
1022 // Is the child editable and non-empty? if so, return false
1023 if (aOptions
.contains(
1024 EmptyCheckOption::TreatNonEditableContentAsInvisible
) &&
1025 !EditorUtils::IsEditableContent(*childContent
, EditorType::HTML
)) {
1029 if (Text
* text
= Text::FromNode(childContent
)) {
1030 // break out if we find we aren't empty
1031 if (aOptions
.contains(EmptyCheckOption::SafeToAskLayout
)
1032 ? IsInVisibleTextFrames(aPresContext
, *text
)
1033 : IsVisibleTextNode(*text
)) {
1039 MOZ_ASSERT(childContent
!= &aNode
);
1041 if (!aOptions
.contains(EmptyCheckOption::TreatSingleBRElementAsVisible
) &&
1042 !seenBR
&& childContent
->IsHTMLElement(nsGkAtoms::br
)) {
1043 // Ignore first <br> element in it if caller wants so because it's
1044 // typically a padding <br> element of for a parent block.
1052 // Note: list items or table cells are not considered empty
1053 // if they contain other lists or tables
1054 EmptyCheckOptions
options(aOptions
);
1055 if (childContent
->IsElement() && (isListItem
|| isTableCell
)) {
1056 options
+= {EmptyCheckOption::TreatListItemAsVisible
,
1057 EmptyCheckOption::TreatTableCellAsVisible
};
1059 if (!IsEmptyNode(aPresContext
, *childContent
, options
, &seenBR
)) {
1073 bool HTMLEditUtils::ShouldInsertLinefeedCharacter(
1074 const EditorDOMPoint
& aPointToInsert
, const Element
& aEditingHost
) {
1075 MOZ_ASSERT(aPointToInsert
.IsSetAndValid());
1077 if (!aPointToInsert
.IsInContentNode()) {
1081 // closestEditableBlockElement can be nullptr if aEditingHost is an inline
1083 Element
* closestEditableBlockElement
=
1084 HTMLEditUtils::GetInclusiveAncestorElement(
1085 *aPointToInsert
.ContainerAs
<nsIContent
>(),
1086 HTMLEditUtils::ClosestEditableBlockElement
,
1087 BlockInlineCheck::UseComputedDisplayOutsideStyle
);
1089 // If and only if the nearest block is the editing host or its parent,
1090 // and new line character is preformatted, we should insert a linefeed.
1091 return (!closestEditableBlockElement
||
1092 closestEditableBlockElement
== &aEditingHost
) &&
1093 EditorUtils::IsNewLinePreformatted(
1094 *aPointToInsert
.ContainerAs
<nsIContent
>());
1097 // We use bitmasks to test containment of elements. Elements are marked to be
1098 // in certain groups by setting the mGroup member of the `ElementInfo` struct
1099 // to the corresponding GROUP_ values (OR'ed together). Similarly, elements are
1100 // marked to allow containment of certain groups by setting the
1101 // mCanContainGroups member of the `ElementInfo` struct to the corresponding
1102 // GROUP_ values (OR'ed together).
1103 // Testing containment then simply consists of checking whether the
1104 // mCanContainGroups bitmask of an element and the mGroup bitmask of a
1105 // potential child overlap.
1107 #define GROUP_NONE 0
1110 #define GROUP_TOPLEVEL (1 << 1)
1112 // base, link, meta, script, style, title
1113 #define GROUP_HEAD_CONTENT (1 << 2)
1115 // b, big, i, s, small, strike, tt, u
1116 #define GROUP_FONTSTYLE (1 << 3)
1118 // abbr, acronym, cite, code, datalist, del, dfn, em, ins, kbd, mark, rb, rp
1119 // rt, rtc, ruby, samp, strong, var
1120 #define GROUP_PHRASE (1 << 4)
1122 // a, applet, basefont, bdi, bdo, br, font, iframe, img, map, meter, object,
1123 // output, picture, progress, q, script, span, sub, sup
1124 #define GROUP_SPECIAL (1 << 5)
1126 // button, form, input, label, select, textarea
1127 #define GROUP_FORMCONTROL (1 << 6)
1129 // address, applet, article, aside, blockquote, button, center, del, details,
1130 // dialog, dir, div, dl, fieldset, figure, footer, form, h1, h2, h3, h4, h5,
1131 // h6, header, hgroup, hr, iframe, ins, main, map, menu, nav, noframes,
1132 // noscript, object, ol, p, pre, table, search, section, summary, ul
1133 #define GROUP_BLOCK (1 << 7)
1136 #define GROUP_FRAME (1 << 8)
1139 #define GROUP_TABLE_CONTENT (1 << 9)
1142 #define GROUP_TBODY_CONTENT (1 << 10)
1145 #define GROUP_TR_CONTENT (1 << 11)
1148 #define GROUP_COLGROUP_CONTENT (1 << 12)
1151 #define GROUP_OBJECT_CONTENT (1 << 13)
1154 #define GROUP_LI (1 << 14)
1157 #define GROUP_MAP_CONTENT (1 << 15)
1160 #define GROUP_SELECT_CONTENT (1 << 16)
1163 #define GROUP_OPTIONS (1 << 17)
1166 #define GROUP_DL_CONTENT (1 << 18)
1169 #define GROUP_P (1 << 19)
1171 // text, white-space, newline, comment
1172 #define GROUP_LEAF (1 << 20)
1174 // XXX This is because the editor does sublists illegally.
1176 #define GROUP_OL_UL (1 << 21)
1178 // h1, h2, h3, h4, h5, h6
1179 #define GROUP_HEADING (1 << 22)
1182 #define GROUP_FIGCAPTION (1 << 23)
1184 // picture members (img, source)
1185 #define GROUP_PICTURE_CONTENT (1 << 24)
1187 #define GROUP_INLINE_ELEMENT \
1188 (GROUP_FONTSTYLE | GROUP_PHRASE | GROUP_SPECIAL | GROUP_FORMCONTROL | \
1191 #define GROUP_FLOW_ELEMENT (GROUP_INLINE_ELEMENT | GROUP_BLOCK)
1193 struct ElementInfo final
{
1197 // See `GROUP_NONE`'s comment.
1199 // See `GROUP_NONE`'s comment.
1200 uint32_t mCanContainGroups
;
1202 bool mCanContainSelf
;
1206 # define ELEM(_tag, _isContainer, _canContainSelf, _group, _canContainGroups) \
1207 {eHTMLTag_##_tag, _group, _canContainGroups, _isContainer, _canContainSelf}
1209 # define ELEM(_tag, _isContainer, _canContainSelf, _group, _canContainGroups) \
1210 {_group, _canContainGroups, _isContainer, _canContainSelf}
1213 static const ElementInfo kElements
[eHTMLTag_userdefined
] = {
1214 ELEM(a
, true, false, GROUP_SPECIAL
, GROUP_INLINE_ELEMENT
),
1215 ELEM(abbr
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1216 ELEM(acronym
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1217 ELEM(address
, true, true, GROUP_BLOCK
, GROUP_INLINE_ELEMENT
| GROUP_P
),
1218 // While applet is no longer a valid tag, removing it here breaks the editor
1219 // (compiles, but causes many tests to fail in odd ways). This list is
1220 // tracked against the main HTML Tag list, so any changes will require more
1221 // than just removing entries.
1222 ELEM(applet
, true, true, GROUP_SPECIAL
| GROUP_BLOCK
,
1223 GROUP_FLOW_ELEMENT
| GROUP_OBJECT_CONTENT
),
1224 ELEM(area
, false, false, GROUP_MAP_CONTENT
, GROUP_NONE
),
1225 ELEM(article
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1226 ELEM(aside
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1227 ELEM(audio
, false, false, GROUP_NONE
, GROUP_NONE
),
1228 ELEM(b
, true, true, GROUP_FONTSTYLE
, GROUP_INLINE_ELEMENT
),
1229 ELEM(base
, false, false, GROUP_HEAD_CONTENT
, GROUP_NONE
),
1230 ELEM(basefont
, false, false, GROUP_SPECIAL
, GROUP_NONE
),
1231 ELEM(bdi
, true, true, GROUP_SPECIAL
, GROUP_INLINE_ELEMENT
),
1232 ELEM(bdo
, true, true, GROUP_SPECIAL
, GROUP_INLINE_ELEMENT
),
1233 ELEM(bgsound
, false, false, GROUP_NONE
, GROUP_NONE
),
1234 ELEM(big
, true, true, GROUP_FONTSTYLE
, GROUP_INLINE_ELEMENT
),
1235 ELEM(blockquote
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1236 ELEM(body
, true, true, GROUP_TOPLEVEL
, GROUP_FLOW_ELEMENT
),
1237 ELEM(br
, false, false, GROUP_SPECIAL
, GROUP_NONE
),
1238 ELEM(button
, true, true, GROUP_FORMCONTROL
| GROUP_BLOCK
,
1239 GROUP_FLOW_ELEMENT
),
1240 ELEM(canvas
, false, false, GROUP_NONE
, GROUP_NONE
),
1241 ELEM(caption
, true, true, GROUP_NONE
, GROUP_INLINE_ELEMENT
),
1242 ELEM(center
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1243 ELEM(cite
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1244 ELEM(code
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1245 ELEM(col
, false, false, GROUP_TABLE_CONTENT
| GROUP_COLGROUP_CONTENT
,
1247 ELEM(colgroup
, true, false, GROUP_NONE
, GROUP_COLGROUP_CONTENT
),
1248 ELEM(data
, true, false, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1249 ELEM(datalist
, true, false, GROUP_PHRASE
,
1250 GROUP_OPTIONS
| GROUP_INLINE_ELEMENT
),
1251 ELEM(dd
, true, false, GROUP_DL_CONTENT
, GROUP_FLOW_ELEMENT
),
1252 ELEM(del
, true, true, GROUP_PHRASE
| GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1253 ELEM(details
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1254 ELEM(dfn
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1255 ELEM(dialog
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1256 ELEM(dir
, true, false, GROUP_BLOCK
, GROUP_LI
),
1257 ELEM(div
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1258 ELEM(dl
, true, false, GROUP_BLOCK
, GROUP_DL_CONTENT
),
1259 ELEM(dt
, true, true, GROUP_DL_CONTENT
, GROUP_INLINE_ELEMENT
),
1260 ELEM(em
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1261 ELEM(embed
, false, false, GROUP_NONE
, GROUP_NONE
),
1262 ELEM(fieldset
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1263 ELEM(figcaption
, true, false, GROUP_FIGCAPTION
, GROUP_FLOW_ELEMENT
),
1264 ELEM(figure
, true, true, GROUP_BLOCK
,
1265 GROUP_FLOW_ELEMENT
| GROUP_FIGCAPTION
),
1266 ELEM(font
, true, true, GROUP_SPECIAL
, GROUP_INLINE_ELEMENT
),
1267 ELEM(footer
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1268 ELEM(form
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1269 ELEM(frame
, false, false, GROUP_FRAME
, GROUP_NONE
),
1270 ELEM(frameset
, true, true, GROUP_FRAME
, GROUP_FRAME
),
1271 ELEM(h1
, true, false, GROUP_BLOCK
| GROUP_HEADING
, GROUP_INLINE_ELEMENT
),
1272 ELEM(h2
, true, false, GROUP_BLOCK
| GROUP_HEADING
, GROUP_INLINE_ELEMENT
),
1273 ELEM(h3
, true, false, GROUP_BLOCK
| GROUP_HEADING
, GROUP_INLINE_ELEMENT
),
1274 ELEM(h4
, true, false, GROUP_BLOCK
| GROUP_HEADING
, GROUP_INLINE_ELEMENT
),
1275 ELEM(h5
, true, false, GROUP_BLOCK
| GROUP_HEADING
, GROUP_INLINE_ELEMENT
),
1276 ELEM(h6
, true, false, GROUP_BLOCK
| GROUP_HEADING
, GROUP_INLINE_ELEMENT
),
1277 ELEM(head
, true, false, GROUP_TOPLEVEL
, GROUP_HEAD_CONTENT
),
1278 ELEM(header
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1279 ELEM(hgroup
, true, false, GROUP_BLOCK
, GROUP_HEADING
),
1280 ELEM(hr
, false, false, GROUP_BLOCK
, GROUP_NONE
),
1281 ELEM(html
, true, false, GROUP_TOPLEVEL
, GROUP_TOPLEVEL
),
1282 ELEM(i
, true, true, GROUP_FONTSTYLE
, GROUP_INLINE_ELEMENT
),
1283 ELEM(iframe
, true, true, GROUP_SPECIAL
| GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1284 ELEM(image
, false, false, GROUP_NONE
, GROUP_NONE
),
1285 ELEM(img
, false, false, GROUP_SPECIAL
| GROUP_PICTURE_CONTENT
, GROUP_NONE
),
1286 ELEM(input
, false, false, GROUP_FORMCONTROL
, GROUP_NONE
),
1287 ELEM(ins
, true, true, GROUP_PHRASE
| GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1288 ELEM(kbd
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1289 ELEM(keygen
, false, false, GROUP_NONE
, GROUP_NONE
),
1290 ELEM(label
, true, false, GROUP_FORMCONTROL
, GROUP_INLINE_ELEMENT
),
1291 ELEM(legend
, true, true, GROUP_NONE
, GROUP_INLINE_ELEMENT
),
1292 ELEM(li
, true, false, GROUP_LI
, GROUP_FLOW_ELEMENT
),
1293 ELEM(link
, false, false, GROUP_HEAD_CONTENT
, GROUP_NONE
),
1294 ELEM(listing
, true, true, GROUP_BLOCK
, GROUP_INLINE_ELEMENT
),
1295 ELEM(main
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1296 ELEM(map
, true, true, GROUP_SPECIAL
, GROUP_BLOCK
| GROUP_MAP_CONTENT
),
1297 ELEM(mark
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1298 ELEM(marquee
, true, false, GROUP_NONE
, GROUP_NONE
),
1299 ELEM(menu
, true, true, GROUP_BLOCK
, GROUP_LI
| GROUP_FLOW_ELEMENT
),
1300 ELEM(meta
, false, false, GROUP_HEAD_CONTENT
, GROUP_NONE
),
1301 ELEM(meter
, true, false, GROUP_SPECIAL
, GROUP_FLOW_ELEMENT
),
1302 ELEM(multicol
, false, false, GROUP_NONE
, GROUP_NONE
),
1303 ELEM(nav
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1304 ELEM(nobr
, true, false, GROUP_NONE
, GROUP_NONE
),
1305 ELEM(noembed
, false, false, GROUP_NONE
, GROUP_NONE
),
1306 ELEM(noframes
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1307 ELEM(noscript
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1308 ELEM(object
, true, true, GROUP_SPECIAL
| GROUP_BLOCK
,
1309 GROUP_FLOW_ELEMENT
| GROUP_OBJECT_CONTENT
),
1310 // XXX Can contain self and ul because editor does sublists illegally.
1311 ELEM(ol
, true, true, GROUP_BLOCK
| GROUP_OL_UL
, GROUP_LI
| GROUP_OL_UL
),
1312 ELEM(optgroup
, true, false, GROUP_SELECT_CONTENT
, GROUP_OPTIONS
),
1313 ELEM(option
, true, false, GROUP_SELECT_CONTENT
| GROUP_OPTIONS
, GROUP_LEAF
),
1314 ELEM(output
, true, true, GROUP_SPECIAL
, GROUP_INLINE_ELEMENT
),
1315 ELEM(p
, true, false, GROUP_BLOCK
| GROUP_P
, GROUP_INLINE_ELEMENT
),
1316 ELEM(param
, false, false, GROUP_OBJECT_CONTENT
, GROUP_NONE
),
1317 ELEM(picture
, true, false, GROUP_SPECIAL
, GROUP_PICTURE_CONTENT
),
1318 ELEM(plaintext
, false, false, GROUP_NONE
, GROUP_NONE
),
1319 ELEM(pre
, true, true, GROUP_BLOCK
, GROUP_INLINE_ELEMENT
),
1320 ELEM(progress
, true, false, GROUP_SPECIAL
, GROUP_FLOW_ELEMENT
),
1321 ELEM(q
, true, true, GROUP_SPECIAL
, GROUP_INLINE_ELEMENT
),
1322 ELEM(rb
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1323 ELEM(rp
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1324 ELEM(rt
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1325 ELEM(rtc
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1326 ELEM(ruby
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1327 ELEM(s
, true, true, GROUP_FONTSTYLE
, GROUP_INLINE_ELEMENT
),
1328 ELEM(samp
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1329 ELEM(script
, true, false, GROUP_HEAD_CONTENT
| GROUP_SPECIAL
, GROUP_LEAF
),
1330 ELEM(search
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1331 ELEM(section
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1332 ELEM(select
, true, false, GROUP_FORMCONTROL
, GROUP_SELECT_CONTENT
),
1333 ELEM(small
, true, true, GROUP_FONTSTYLE
, GROUP_INLINE_ELEMENT
),
1334 ELEM(slot
, true, false, GROUP_NONE
, GROUP_FLOW_ELEMENT
),
1335 ELEM(source
, false, false, GROUP_PICTURE_CONTENT
, GROUP_NONE
),
1336 ELEM(span
, true, true, GROUP_SPECIAL
, GROUP_INLINE_ELEMENT
),
1337 ELEM(strike
, true, true, GROUP_FONTSTYLE
, GROUP_INLINE_ELEMENT
),
1338 ELEM(strong
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1339 ELEM(style
, true, false, GROUP_HEAD_CONTENT
, GROUP_LEAF
),
1340 ELEM(sub
, true, true, GROUP_SPECIAL
, GROUP_INLINE_ELEMENT
),
1341 ELEM(summary
, true, true, GROUP_BLOCK
, GROUP_FLOW_ELEMENT
),
1342 ELEM(sup
, true, true, GROUP_SPECIAL
, GROUP_INLINE_ELEMENT
),
1343 ELEM(table
, true, false, GROUP_BLOCK
, GROUP_TABLE_CONTENT
),
1344 ELEM(tbody
, true, false, GROUP_TABLE_CONTENT
, GROUP_TBODY_CONTENT
),
1345 ELEM(td
, true, false, GROUP_TR_CONTENT
, GROUP_FLOW_ELEMENT
),
1346 ELEM(textarea
, true, false, GROUP_FORMCONTROL
, GROUP_LEAF
),
1347 ELEM(tfoot
, true, false, GROUP_NONE
, GROUP_TBODY_CONTENT
),
1348 ELEM(th
, true, false, GROUP_TR_CONTENT
, GROUP_FLOW_ELEMENT
),
1349 ELEM(thead
, true, false, GROUP_NONE
, GROUP_TBODY_CONTENT
),
1350 ELEM(template, false, false, GROUP_NONE
, GROUP_NONE
),
1351 ELEM(time
, true, false, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1352 ELEM(title
, true, false, GROUP_HEAD_CONTENT
, GROUP_LEAF
),
1353 ELEM(tr
, true, false, GROUP_TBODY_CONTENT
, GROUP_TR_CONTENT
),
1354 ELEM(track
, false, false, GROUP_NONE
, GROUP_NONE
),
1355 ELEM(tt
, true, true, GROUP_FONTSTYLE
, GROUP_INLINE_ELEMENT
),
1356 ELEM(u
, true, true, GROUP_FONTSTYLE
, GROUP_INLINE_ELEMENT
),
1357 // XXX Can contain self and ol because editor does sublists illegally.
1358 ELEM(ul
, true, true, GROUP_BLOCK
| GROUP_OL_UL
, GROUP_LI
| GROUP_OL_UL
),
1359 ELEM(var
, true, true, GROUP_PHRASE
, GROUP_INLINE_ELEMENT
),
1360 ELEM(video
, false, false, GROUP_NONE
, GROUP_NONE
),
1361 ELEM(wbr
, false, false, GROUP_NONE
, GROUP_NONE
),
1362 ELEM(xmp
, true, false, GROUP_BLOCK
, GROUP_NONE
),
1364 // These aren't elements.
1365 ELEM(text
, false, false, GROUP_LEAF
, GROUP_NONE
),
1366 ELEM(whitespace
, false, false, GROUP_LEAF
, GROUP_NONE
),
1367 ELEM(newline
, false, false, GROUP_LEAF
, GROUP_NONE
),
1368 ELEM(comment
, false, false, GROUP_LEAF
, GROUP_NONE
),
1369 ELEM(entity
, false, false, GROUP_NONE
, GROUP_NONE
),
1370 ELEM(doctypeDecl
, false, false, GROUP_NONE
, GROUP_NONE
),
1371 ELEM(markupDecl
, false, false, GROUP_NONE
, GROUP_NONE
),
1372 ELEM(instruction
, false, false, GROUP_NONE
, GROUP_NONE
),
1374 ELEM(userdefined
, true, false, GROUP_NONE
, GROUP_FLOW_ELEMENT
)};
1376 bool HTMLEditUtils::CanNodeContain(nsHTMLTag aParentTagId
,
1377 nsHTMLTag aChildTagId
) {
1379 aParentTagId
> eHTMLTag_unknown
&& aParentTagId
<= eHTMLTag_userdefined
,
1380 "aParentTagId out of range!");
1382 aChildTagId
> eHTMLTag_unknown
&& aChildTagId
<= eHTMLTag_userdefined
,
1383 "aChildTagId out of range!");
1386 static bool checked
= false;
1390 for (i
= 1; i
<= eHTMLTag_userdefined
; ++i
) {
1391 NS_ASSERTION(kElements
[i
- 1].mTag
== i
,
1392 "You need to update kElements (missing tags).");
1397 // Special-case button.
1398 if (aParentTagId
== eHTMLTag_button
) {
1399 static const nsHTMLTag kButtonExcludeKids
[] = {
1400 eHTMLTag_a
, eHTMLTag_fieldset
, eHTMLTag_form
, eHTMLTag_iframe
,
1401 eHTMLTag_input
, eHTMLTag_select
, eHTMLTag_textarea
};
1404 for (j
= 0; j
< ArrayLength(kButtonExcludeKids
); ++j
) {
1405 if (kButtonExcludeKids
[j
] == aChildTagId
) {
1411 // Deprecated elements.
1412 if (aChildTagId
== eHTMLTag_bgsound
) {
1416 // Bug #67007, dont strip userdefined tags.
1417 if (aChildTagId
== eHTMLTag_userdefined
) {
1421 const ElementInfo
& parent
= kElements
[aParentTagId
- 1];
1422 if (aParentTagId
== aChildTagId
) {
1423 return parent
.mCanContainSelf
;
1426 const ElementInfo
& child
= kElements
[aChildTagId
- 1];
1427 return !!(parent
.mCanContainGroups
& child
.mGroup
);
1430 bool HTMLEditUtils::ContentIsInert(const nsIContent
& aContent
) {
1431 for (nsIContent
* content
:
1432 aContent
.InclusiveFlatTreeAncestorsOfType
<nsIContent
>()) {
1433 if (nsIFrame
* frame
= content
->GetPrimaryFrame()) {
1434 return frame
->StyleUI()->IsInert();
1436 // If it doesn't have primary frame, we need to check its ancestors.
1437 // This may occur if it's an invisible text node or element nodes whose
1438 // display is an invisible value.
1439 if (!content
->IsElement()) {
1442 if (content
->AsElement()->State().HasState(dom::ElementState::INERT
)) {
1449 bool HTMLEditUtils::IsContainerNode(nsHTMLTag aTagId
) {
1450 NS_ASSERTION(aTagId
> eHTMLTag_unknown
&& aTagId
<= eHTMLTag_userdefined
,
1451 "aTagId out of range!");
1453 return kElements
[aTagId
- 1].mIsContainer
;
1456 bool HTMLEditUtils::IsNonListSingleLineContainer(const nsINode
& aNode
) {
1457 return aNode
.IsAnyOfHTMLElements(
1458 nsGkAtoms::address
, nsGkAtoms::div
, nsGkAtoms::h1
, nsGkAtoms::h2
,
1459 nsGkAtoms::h3
, nsGkAtoms::h4
, nsGkAtoms::h5
, nsGkAtoms::h6
,
1460 nsGkAtoms::listing
, nsGkAtoms::p
, nsGkAtoms::pre
, nsGkAtoms::xmp
);
1463 bool HTMLEditUtils::IsSingleLineContainer(const nsINode
& aNode
) {
1464 return IsNonListSingleLineContainer(aNode
) ||
1465 aNode
.IsAnyOfHTMLElements(nsGkAtoms::li
, nsGkAtoms::dt
, nsGkAtoms::dd
);
1469 template <typename PT
, typename CT
>
1470 nsIContent
* HTMLEditUtils::GetPreviousContent(
1471 const EditorDOMPointBase
<PT
, CT
>& aPoint
, const WalkTreeOptions
& aOptions
,
1472 BlockInlineCheck aBlockInlineCheck
,
1473 const Element
* aAncestorLimiter
/* = nullptr */) {
1474 MOZ_ASSERT(aPoint
.IsSetAndValid());
1475 NS_WARNING_ASSERTION(
1476 !aPoint
.IsInDataNode() || aPoint
.IsInTextNode(),
1477 "GetPreviousContent() doesn't assume that the start point is a "
1478 "data node except text node");
1480 // If we are at the beginning of the node, or it is a text node, then just
1482 if (aPoint
.IsStartOfContainer() || aPoint
.IsInTextNode()) {
1483 if (aOptions
.contains(WalkTreeOption::StopAtBlockBoundary
) &&
1484 aPoint
.IsInContentNode() &&
1485 HTMLEditUtils::IsBlockElement(
1486 *aPoint
.template ContainerAs
<nsIContent
>(), aBlockInlineCheck
)) {
1487 // If we aren't allowed to cross blocks, don't look before this block.
1490 return HTMLEditUtils::GetPreviousContent(
1491 *aPoint
.GetContainer(), aOptions
, aBlockInlineCheck
, aAncestorLimiter
);
1494 // else look before the child at 'aOffset'
1495 if (aPoint
.GetChild()) {
1496 return HTMLEditUtils::GetPreviousContent(
1497 *aPoint
.GetChild(), aOptions
, aBlockInlineCheck
, aAncestorLimiter
);
1500 // unless there isn't one, in which case we are at the end of the node
1501 // and want the deep-right child.
1502 nsIContent
* lastLeafContent
= HTMLEditUtils::GetLastLeafContent(
1503 *aPoint
.GetContainer(),
1504 {aOptions
.contains(WalkTreeOption::StopAtBlockBoundary
)
1505 ? LeafNodeType::LeafNodeOrChildBlock
1506 : LeafNodeType::OnlyLeafNode
},
1508 if (!lastLeafContent
) {
1512 if (!HTMLEditUtils::IsContentIgnored(*lastLeafContent
, aOptions
)) {
1513 return lastLeafContent
;
1516 // restart the search from the non-editable node we just found
1517 return HTMLEditUtils::GetPreviousContent(*lastLeafContent
, aOptions
,
1518 aBlockInlineCheck
, aAncestorLimiter
);
1522 template <typename PT
, typename CT
>
1523 nsIContent
* HTMLEditUtils::GetNextContent(
1524 const EditorDOMPointBase
<PT
, CT
>& aPoint
, const WalkTreeOptions
& aOptions
,
1525 BlockInlineCheck aBlockInlineCheck
,
1526 const Element
* aAncestorLimiter
/* = nullptr */) {
1527 MOZ_ASSERT(aPoint
.IsSetAndValid());
1528 NS_WARNING_ASSERTION(
1529 !aPoint
.IsInDataNode() || aPoint
.IsInTextNode(),
1530 "GetNextContent() doesn't assume that the start point is a "
1531 "data node except text node");
1533 auto point
= aPoint
.template To
<EditorRawDOMPoint
>();
1535 // if the container is a text node, use its location instead
1536 if (point
.IsInTextNode()) {
1537 point
.SetAfter(point
.GetContainer());
1538 if (NS_WARN_IF(!point
.IsSet())) {
1543 if (point
.GetChild()) {
1544 if (aOptions
.contains(WalkTreeOption::StopAtBlockBoundary
) &&
1545 HTMLEditUtils::IsBlockElement(*point
.GetChild(), aBlockInlineCheck
)) {
1546 return point
.GetChild();
1549 nsIContent
* firstLeafContent
= HTMLEditUtils::GetFirstLeafContent(
1551 {aOptions
.contains(WalkTreeOption::StopAtBlockBoundary
)
1552 ? LeafNodeType::LeafNodeOrChildBlock
1553 : LeafNodeType::OnlyLeafNode
},
1555 if (!firstLeafContent
) {
1556 return point
.GetChild();
1559 // XXX Why do we need to do this check? The leaf node must be a descendant
1560 // of `point.GetChild()`.
1561 if (aAncestorLimiter
&&
1562 (firstLeafContent
== aAncestorLimiter
||
1563 !firstLeafContent
->IsInclusiveDescendantOf(aAncestorLimiter
))) {
1567 if (!HTMLEditUtils::IsContentIgnored(*firstLeafContent
, aOptions
)) {
1568 return firstLeafContent
;
1571 // restart the search from the non-editable node we just found
1572 return HTMLEditUtils::GetNextContent(*firstLeafContent
, aOptions
,
1573 aBlockInlineCheck
, aAncestorLimiter
);
1576 // unless there isn't one, in which case we are at the end of the node
1577 // and want the next one.
1578 if (aOptions
.contains(WalkTreeOption::StopAtBlockBoundary
) &&
1579 point
.IsInContentNode() &&
1580 HTMLEditUtils::IsBlockElement(*point
.template ContainerAs
<nsIContent
>(),
1581 aBlockInlineCheck
)) {
1582 // don't cross out of parent block
1586 return HTMLEditUtils::GetNextContent(*point
.GetContainer(), aOptions
,
1587 aBlockInlineCheck
, aAncestorLimiter
);
1591 nsIContent
* HTMLEditUtils::GetAdjacentLeafContent(
1592 const nsINode
& aNode
, WalkTreeDirection aWalkTreeDirection
,
1593 const WalkTreeOptions
& aOptions
, BlockInlineCheck aBlockInlineCheck
,
1594 const Element
* aAncestorLimiter
/* = nullptr */) {
1595 // called only by GetPriorNode so we don't need to check params.
1596 MOZ_ASSERT(&aNode
!= aAncestorLimiter
);
1597 MOZ_ASSERT_IF(aAncestorLimiter
,
1598 aAncestorLimiter
->IsInclusiveDescendantOf(aAncestorLimiter
));
1600 const nsINode
* node
= &aNode
;
1602 // if aNode has a sibling in the right direction, return
1603 // that sibling's closest child (or itself if it has no children)
1604 nsIContent
* sibling
= aWalkTreeDirection
== WalkTreeDirection::Forward
1605 ? node
->GetNextSibling()
1606 : node
->GetPreviousSibling();
1608 // XXX If `sibling` belongs to siblings of inclusive ancestors of aNode,
1609 // perhaps, we need to use
1610 // IgnoreInsideBlockBoundary(aBlockInlineCheck) here.
1611 if (aOptions
.contains(WalkTreeOption::StopAtBlockBoundary
) &&
1612 HTMLEditUtils::IsBlockElement(*sibling
, aBlockInlineCheck
)) {
1613 // don't look inside previous sibling, since it is a block
1616 const LeafNodeTypes leafNodeTypes
= {
1617 aOptions
.contains(WalkTreeOption::StopAtBlockBoundary
)
1618 ? LeafNodeType::LeafNodeOrChildBlock
1619 : LeafNodeType::OnlyLeafNode
};
1620 nsIContent
* leafContent
=
1621 aWalkTreeDirection
== WalkTreeDirection::Forward
1622 ? HTMLEditUtils::GetFirstLeafContent(*sibling
, leafNodeTypes
,
1624 : HTMLEditUtils::GetLastLeafContent(*sibling
, leafNodeTypes
,
1626 return leafContent
? leafContent
: sibling
;
1629 nsIContent
* parent
= node
->GetParent();
1634 if (parent
== aAncestorLimiter
||
1635 (aOptions
.contains(WalkTreeOption::StopAtBlockBoundary
) &&
1636 HTMLEditUtils::IsBlockElement(*parent
, aBlockInlineCheck
))) {
1643 MOZ_ASSERT_UNREACHABLE("What part of for(;;) do you not understand?");
1648 nsIContent
* HTMLEditUtils::GetAdjacentContent(
1649 const nsINode
& aNode
, WalkTreeDirection aWalkTreeDirection
,
1650 const WalkTreeOptions
& aOptions
, BlockInlineCheck aBlockInlineCheck
,
1651 const Element
* aAncestorLimiter
/* = nullptr */) {
1652 if (&aNode
== aAncestorLimiter
) {
1653 // Don't allow traversal above the root node! This helps
1654 // prevent us from accidentally editing browser content
1655 // when the editor is in a text widget.
1659 nsIContent
* leafContent
= HTMLEditUtils::GetAdjacentLeafContent(
1660 aNode
, aWalkTreeDirection
, aOptions
, aBlockInlineCheck
, aAncestorLimiter
);
1665 if (!HTMLEditUtils::IsContentIgnored(*leafContent
, aOptions
)) {
1669 return HTMLEditUtils::GetAdjacentContent(*leafContent
, aWalkTreeDirection
,
1670 aOptions
, aBlockInlineCheck
,
1675 template <typename EditorDOMPointType
>
1676 EditorDOMPointType
HTMLEditUtils::GetPreviousEditablePoint(
1677 nsIContent
& aContent
, const Element
* aAncestorLimiter
,
1678 InvisibleWhiteSpaces aInvisibleWhiteSpaces
,
1679 TableBoundary aHowToTreatTableBoundary
) {
1680 MOZ_ASSERT(HTMLEditUtils::IsSimplyEditableNode(aContent
));
1681 NS_ASSERTION(!HTMLEditUtils::IsAnyTableElement(&aContent
) ||
1682 HTMLEditUtils::IsTableCellOrCaption(aContent
),
1683 "HTMLEditUtils::GetPreviousEditablePoint() may return a point "
1684 "between table structure elements");
1686 if (&aContent
== aAncestorLimiter
) {
1687 return EditorDOMPointType();
1690 // First, look for previous content.
1691 nsIContent
* previousContent
= aContent
.GetPreviousSibling();
1692 if (!previousContent
) {
1693 if (!aContent
.GetParentElement()) {
1694 return EditorDOMPointType();
1696 nsIContent
* inclusiveAncestor
= &aContent
;
1697 for (Element
* parentElement
: aContent
.AncestorsOfType
<Element
>()) {
1698 if (parentElement
== aAncestorLimiter
||
1699 !HTMLEditUtils::IsSimplyEditableNode(*parentElement
) ||
1700 !HTMLEditUtils::CanCrossContentBoundary(*parentElement
,
1701 aHowToTreatTableBoundary
)) {
1702 // If cannot cross the parent element boundary, return the point of
1703 // last inclusive ancestor point.
1704 return EditorDOMPointType(inclusiveAncestor
);
1707 // Start of the parent element is a next editable point if it's an
1708 // element which is not a table structure element.
1709 if (!HTMLEditUtils::IsAnyTableElement(parentElement
) ||
1710 HTMLEditUtils::IsTableCellOrCaption(*parentElement
)) {
1711 inclusiveAncestor
= parentElement
;
1714 previousContent
= parentElement
->GetPreviousSibling();
1715 if (!previousContent
) {
1716 continue; // Keep looking for previous sibling of an ancestor.
1719 // XXX Should we ignore data node like CDATA, Comment, etc?
1721 // If previous content is not editable, let's return the point after it.
1722 if (!HTMLEditUtils::IsSimplyEditableNode(*previousContent
)) {
1723 return EditorDOMPointType::After(*previousContent
);
1726 // If cannot cross previous content boundary, return start of last
1727 // inclusive ancestor.
1728 if (!HTMLEditUtils::CanCrossContentBoundary(*previousContent
,
1729 aHowToTreatTableBoundary
)) {
1730 return inclusiveAncestor
== &aContent
1731 ? EditorDOMPointType(inclusiveAncestor
)
1732 : EditorDOMPointType(inclusiveAncestor
, 0);
1736 if (!previousContent
) {
1737 return EditorDOMPointType(inclusiveAncestor
);
1739 } else if (!HTMLEditUtils::IsSimplyEditableNode(*previousContent
)) {
1740 return EditorDOMPointType::After(*previousContent
);
1741 } else if (!HTMLEditUtils::CanCrossContentBoundary(
1742 *previousContent
, aHowToTreatTableBoundary
)) {
1743 return EditorDOMPointType(&aContent
);
1746 // Next, look for end of the previous content.
1747 nsIContent
* leafContent
= previousContent
;
1748 if (previousContent
->GetChildCount() &&
1749 HTMLEditUtils::IsContainerNode(*previousContent
)) {
1750 for (nsIContent
* maybeLeafContent
= previousContent
->GetLastChild();
1752 maybeLeafContent
= maybeLeafContent
->GetLastChild()) {
1753 // If it's not an editable content or cannot cross the boundary,
1754 // return the point after the content. Note that in this case,
1755 // the content must not be any table elements except `<table>`
1756 // because we've climbed down the tree.
1757 if (!HTMLEditUtils::IsSimplyEditableNode(*maybeLeafContent
) ||
1758 !HTMLEditUtils::CanCrossContentBoundary(*maybeLeafContent
,
1759 aHowToTreatTableBoundary
)) {
1760 return EditorDOMPointType::After(*maybeLeafContent
);
1762 leafContent
= maybeLeafContent
;
1763 if (!HTMLEditUtils::IsContainerNode(*leafContent
)) {
1769 if (leafContent
->IsText()) {
1770 Text
* textNode
= leafContent
->AsText();
1771 if (aInvisibleWhiteSpaces
== InvisibleWhiteSpaces::Preserve
) {
1772 return EditorDOMPointType::AtEndOf(*textNode
);
1774 // There may be invisible trailing white-spaces which should be
1775 // ignored. Let's scan its start.
1776 return WSRunScanner::GetAfterLastVisiblePoint
<EditorDOMPointType
>(
1777 *textNode
, aAncestorLimiter
);
1780 // If it's a container element, return end of it. Otherwise, return
1781 // the point after the non-container element.
1782 return HTMLEditUtils::IsContainerNode(*leafContent
)
1783 ? EditorDOMPointType::AtEndOf(*leafContent
)
1784 : EditorDOMPointType::After(*leafContent
);
1788 template <typename EditorDOMPointType
>
1789 EditorDOMPointType
HTMLEditUtils::GetNextEditablePoint(
1790 nsIContent
& aContent
, const Element
* aAncestorLimiter
,
1791 InvisibleWhiteSpaces aInvisibleWhiteSpaces
,
1792 TableBoundary aHowToTreatTableBoundary
) {
1793 MOZ_ASSERT(HTMLEditUtils::IsSimplyEditableNode(aContent
));
1794 NS_ASSERTION(!HTMLEditUtils::IsAnyTableElement(&aContent
) ||
1795 HTMLEditUtils::IsTableCellOrCaption(aContent
),
1796 "HTMLEditUtils::GetPreviousEditablePoint() may return a point "
1797 "between table structure elements");
1799 if (&aContent
== aAncestorLimiter
) {
1800 return EditorDOMPointType();
1803 // First, look for next content.
1804 nsIContent
* nextContent
= aContent
.GetNextSibling();
1806 if (!aContent
.GetParentElement()) {
1807 return EditorDOMPointType();
1809 nsIContent
* inclusiveAncestor
= &aContent
;
1810 for (Element
* parentElement
: aContent
.AncestorsOfType
<Element
>()) {
1811 if (parentElement
== aAncestorLimiter
||
1812 !HTMLEditUtils::IsSimplyEditableNode(*parentElement
) ||
1813 !HTMLEditUtils::CanCrossContentBoundary(*parentElement
,
1814 aHowToTreatTableBoundary
)) {
1815 // If cannot cross the parent element boundary, return the point of
1816 // last inclusive ancestor point.
1817 return EditorDOMPointType(inclusiveAncestor
);
1820 // End of the parent element is a next editable point if it's an
1821 // element which is not a table structure element.
1822 if (!HTMLEditUtils::IsAnyTableElement(parentElement
) ||
1823 HTMLEditUtils::IsTableCellOrCaption(*parentElement
)) {
1824 inclusiveAncestor
= parentElement
;
1827 nextContent
= parentElement
->GetNextSibling();
1829 continue; // Keep looking for next sibling of an ancestor.
1832 // XXX Should we ignore data node like CDATA, Comment, etc?
1834 // If next content is not editable, let's return the point after
1835 // the last inclusive ancestor.
1836 if (!HTMLEditUtils::IsSimplyEditableNode(*nextContent
)) {
1837 return EditorDOMPointType::After(*parentElement
);
1840 // If cannot cross next content boundary, return after the last
1841 // inclusive ancestor.
1842 if (!HTMLEditUtils::CanCrossContentBoundary(*nextContent
,
1843 aHowToTreatTableBoundary
)) {
1844 return EditorDOMPointType::After(*inclusiveAncestor
);
1849 return EditorDOMPointType::After(*inclusiveAncestor
);
1851 } else if (!HTMLEditUtils::IsSimplyEditableNode(*nextContent
)) {
1852 return EditorDOMPointType::After(aContent
);
1853 } else if (!HTMLEditUtils::CanCrossContentBoundary(
1854 *nextContent
, aHowToTreatTableBoundary
)) {
1855 return EditorDOMPointType::After(aContent
);
1858 // Next, look for start of the next content.
1859 nsIContent
* leafContent
= nextContent
;
1860 if (nextContent
->GetChildCount() &&
1861 HTMLEditUtils::IsContainerNode(*nextContent
)) {
1862 for (nsIContent
* maybeLeafContent
= nextContent
->GetFirstChild();
1864 maybeLeafContent
= maybeLeafContent
->GetFirstChild()) {
1865 // If it's not an editable content or cannot cross the boundary,
1866 // return the point at the content (i.e., start of its parent). Note
1867 // that in this case, the content must not be any table elements except
1868 // `<table>` because we've climbed down the tree.
1869 if (!HTMLEditUtils::IsSimplyEditableNode(*maybeLeafContent
) ||
1870 !HTMLEditUtils::CanCrossContentBoundary(*maybeLeafContent
,
1871 aHowToTreatTableBoundary
)) {
1872 return EditorDOMPointType(maybeLeafContent
);
1874 leafContent
= maybeLeafContent
;
1875 if (!HTMLEditUtils::IsContainerNode(*leafContent
)) {
1881 if (leafContent
->IsText()) {
1882 Text
* textNode
= leafContent
->AsText();
1883 if (aInvisibleWhiteSpaces
== InvisibleWhiteSpaces::Preserve
) {
1884 return EditorDOMPointType(textNode
, 0);
1886 // There may be invisible leading white-spaces which should be
1887 // ignored. Let's scan its start.
1888 return WSRunScanner::GetFirstVisiblePoint
<EditorDOMPointType
>(
1889 *textNode
, aAncestorLimiter
);
1892 // If it's a container element, return start of it. Otherwise, return
1893 // the point at the non-container element (i.e., start of its parent).
1894 return HTMLEditUtils::IsContainerNode(*leafContent
)
1895 ? EditorDOMPointType(leafContent
, 0)
1896 : EditorDOMPointType(leafContent
);
1900 Element
* HTMLEditUtils::GetAncestorElement(
1901 const nsIContent
& aContent
, const AncestorTypes
& aAncestorTypes
,
1902 BlockInlineCheck aBlockInlineCheck
,
1903 const Element
* aAncestorLimiter
/* = nullptr */) {
1905 aAncestorTypes
.contains(AncestorType::ClosestBlockElement
) ||
1906 aAncestorTypes
.contains(AncestorType::MostDistantInlineElementInBlock
) ||
1907 aAncestorTypes
.contains(AncestorType::ButtonElement
));
1909 if (&aContent
== aAncestorLimiter
) {
1913 const Element
* theBodyElement
= aContent
.OwnerDoc()->GetBody();
1914 const Element
* theDocumentElement
= aContent
.OwnerDoc()->GetDocumentElement();
1915 Element
* lastAncestorElement
= nullptr;
1916 const bool editableElementOnly
=
1917 aAncestorTypes
.contains(AncestorType::EditableElement
);
1918 const bool lookingForClosestBlockElement
=
1919 aAncestorTypes
.contains(AncestorType::ClosestBlockElement
);
1920 const bool lookingForMostDistantInlineElementInBlock
=
1921 aAncestorTypes
.contains(AncestorType::MostDistantInlineElementInBlock
);
1922 const bool ignoreHRElement
=
1923 aAncestorTypes
.contains(AncestorType::IgnoreHRElement
);
1924 const bool lookingForButtonElement
=
1925 aAncestorTypes
.contains(AncestorType::ButtonElement
);
1926 auto IsSearchingElementType
= [&](const nsIContent
& aContent
) -> bool {
1927 if (!aContent
.IsElement() ||
1928 (ignoreHRElement
&& aContent
.IsHTMLElement(nsGkAtoms::hr
))) {
1931 if (editableElementOnly
&&
1932 !EditorUtils::IsEditableContent(aContent
, EditorType::HTML
)) {
1935 return (lookingForClosestBlockElement
&&
1936 HTMLEditUtils::IsBlockElement(aContent
, aBlockInlineCheck
)) ||
1937 (lookingForMostDistantInlineElementInBlock
&&
1938 HTMLEditUtils::IsInlineContent(aContent
, aBlockInlineCheck
)) ||
1939 (lookingForButtonElement
&&
1940 aContent
.IsHTMLElement(nsGkAtoms::button
));
1942 for (Element
* element
: aContent
.AncestorsOfType
<Element
>()) {
1943 if (editableElementOnly
&&
1944 !EditorUtils::IsEditableContent(*element
, EditorType::HTML
)) {
1945 return lastAncestorElement
&& IsSearchingElementType(*lastAncestorElement
)
1946 ? lastAncestorElement
// editing host (can be inline element)
1949 if (ignoreHRElement
&& element
->IsHTMLElement(nsGkAtoms::hr
)) {
1950 if (element
== aAncestorLimiter
) {
1955 if (lookingForButtonElement
&& element
->IsHTMLElement(nsGkAtoms::button
)) {
1956 return element
; // closest button element
1958 if (HTMLEditUtils::IsBlockElement(*element
, aBlockInlineCheck
)) {
1959 if (lookingForClosestBlockElement
) {
1960 return element
; // closest block element
1962 MOZ_ASSERT_IF(lastAncestorElement
,
1963 HTMLEditUtils::IsInlineContent(*lastAncestorElement
,
1964 aBlockInlineCheck
));
1965 return lastAncestorElement
; // the last inline element which we found
1967 if (element
== aAncestorLimiter
|| element
== theBodyElement
||
1968 element
== theDocumentElement
) {
1971 lastAncestorElement
= element
;
1973 return lastAncestorElement
&& IsSearchingElementType(*lastAncestorElement
)
1974 ? lastAncestorElement
1979 Element
* HTMLEditUtils::GetInclusiveAncestorElement(
1980 const nsIContent
& aContent
, const AncestorTypes
& aAncestorTypes
,
1981 BlockInlineCheck aBlockInlineCheck
,
1982 const Element
* aAncestorLimiter
/* = nullptr */) {
1984 aAncestorTypes
.contains(AncestorType::ClosestBlockElement
) ||
1985 aAncestorTypes
.contains(AncestorType::MostDistantInlineElementInBlock
) ||
1986 aAncestorTypes
.contains(AncestorType::ButtonElement
));
1988 const Element
* theBodyElement
= aContent
.OwnerDoc()->GetBody();
1989 const Element
* theDocumentElement
= aContent
.OwnerDoc()->GetDocumentElement();
1990 const bool editableElementOnly
=
1991 aAncestorTypes
.contains(AncestorType::EditableElement
);
1992 const bool lookingForClosestBlockElement
=
1993 aAncestorTypes
.contains(AncestorType::ClosestBlockElement
);
1994 const bool lookingForMostDistantInlineElementInBlock
=
1995 aAncestorTypes
.contains(AncestorType::MostDistantInlineElementInBlock
);
1996 const bool lookingForButtonElement
=
1997 aAncestorTypes
.contains(AncestorType::ButtonElement
);
1998 const bool ignoreHRElement
=
1999 aAncestorTypes
.contains(AncestorType::IgnoreHRElement
);
2000 auto IsSearchingElementType
= [&](const nsIContent
& aContent
) -> bool {
2001 if (!aContent
.IsElement() ||
2002 (ignoreHRElement
&& aContent
.IsHTMLElement(nsGkAtoms::hr
))) {
2005 if (editableElementOnly
&&
2006 !EditorUtils::IsEditableContent(aContent
, EditorType::HTML
)) {
2009 return (lookingForClosestBlockElement
&&
2010 HTMLEditUtils::IsBlockElement(aContent
, aBlockInlineCheck
)) ||
2011 (lookingForMostDistantInlineElementInBlock
&&
2012 HTMLEditUtils::IsInlineContent(aContent
, aBlockInlineCheck
)) ||
2013 (lookingForButtonElement
&&
2014 aContent
.IsHTMLElement(nsGkAtoms::button
));
2017 // If aContent is the body element or the document element, we shouldn't climb
2018 // up to its parent.
2019 if (editableElementOnly
&&
2020 (&aContent
== theBodyElement
|| &aContent
== theDocumentElement
)) {
2021 return IsSearchingElementType(aContent
)
2022 ? const_cast<Element
*>(aContent
.AsElement())
2026 if (lookingForButtonElement
&& aContent
.IsHTMLElement(nsGkAtoms::button
)) {
2027 return const_cast<Element
*>(aContent
.AsElement());
2030 // If aContent is a block element, we don't need to climb up the tree.
2031 // Consider the result right now.
2032 if ((lookingForClosestBlockElement
||
2033 lookingForMostDistantInlineElementInBlock
) &&
2034 HTMLEditUtils::IsBlockElement(aContent
, aBlockInlineCheck
) &&
2035 !(ignoreHRElement
&& aContent
.IsHTMLElement(nsGkAtoms::hr
))) {
2036 return IsSearchingElementType(aContent
)
2037 ? const_cast<Element
*>(aContent
.AsElement())
2041 // If aContent is the last element to search range because of the parent
2042 // element type, consider the result before calling GetAncestorElement()
2043 // because it won't return aContent.
2044 if (!aContent
.GetParent() ||
2045 (editableElementOnly
&& !EditorUtils::IsEditableContent(
2046 *aContent
.GetParent(), EditorType::HTML
)) ||
2047 (!lookingForClosestBlockElement
&&
2048 HTMLEditUtils::IsBlockElement(*aContent
.GetParent(),
2049 aBlockInlineCheck
) &&
2050 !(ignoreHRElement
&&
2051 aContent
.GetParent()->IsHTMLElement(nsGkAtoms::hr
)))) {
2052 return IsSearchingElementType(aContent
)
2053 ? const_cast<Element
*>(aContent
.AsElement())
2057 if (&aContent
== aAncestorLimiter
) {
2061 return HTMLEditUtils::GetAncestorElement(aContent
, aAncestorTypes
,
2062 aBlockInlineCheck
, aAncestorLimiter
);
2066 Element
* HTMLEditUtils::GetClosestAncestorAnyListElement(
2067 const nsIContent
& aContent
) {
2068 for (Element
* element
: aContent
.AncestorsOfType
<Element
>()) {
2069 if (HTMLEditUtils::IsAnyListElement(element
)) {
2077 Element
* HTMLEditUtils::GetClosestInclusiveAncestorAnyListElement(
2078 const nsIContent
& aContent
) {
2079 for (Element
* element
: aContent
.InclusiveAncestorsOfType
<Element
>()) {
2080 if (HTMLEditUtils::IsAnyListElement(element
)) {
2087 EditAction
HTMLEditUtils::GetEditActionForInsert(const nsAtom
& aTagName
) {
2088 // This method may be in a hot path. So, return only necessary
2089 // EditAction::eInsert*Element.
2090 if (&aTagName
== nsGkAtoms::ul
) {
2091 // For InputEvent.inputType, "insertUnorderedList".
2092 return EditAction::eInsertUnorderedListElement
;
2094 if (&aTagName
== nsGkAtoms::ol
) {
2095 // For InputEvent.inputType, "insertOrderedList".
2096 return EditAction::eInsertOrderedListElement
;
2098 if (&aTagName
== nsGkAtoms::hr
) {
2099 // For InputEvent.inputType, "insertHorizontalRule".
2100 return EditAction::eInsertHorizontalRuleElement
;
2102 return EditAction::eInsertNode
;
2105 EditAction
HTMLEditUtils::GetEditActionForRemoveList(const nsAtom
& aTagName
) {
2106 // This method may be in a hot path. So, return only necessary
2107 // EditAction::eRemove*Element.
2108 if (&aTagName
== nsGkAtoms::ul
) {
2109 // For InputEvent.inputType, "insertUnorderedList".
2110 return EditAction::eRemoveUnorderedListElement
;
2112 if (&aTagName
== nsGkAtoms::ol
) {
2113 // For InputEvent.inputType, "insertOrderedList".
2114 return EditAction::eRemoveOrderedListElement
;
2116 return EditAction::eRemoveListElement
;
2119 EditAction
HTMLEditUtils::GetEditActionForInsert(const Element
& aElement
) {
2120 return GetEditActionForInsert(*aElement
.NodeInfo()->NameAtom());
2123 EditAction
HTMLEditUtils::GetEditActionForFormatText(const nsAtom
& aProperty
,
2124 const nsAtom
* aAttribute
,
2126 // This method may be in a hot path. So, return only necessary
2127 // EditAction::eSet*Property or EditAction::eRemove*Property.
2128 if (&aProperty
== nsGkAtoms::b
) {
2129 return aToSetStyle
? EditAction::eSetFontWeightProperty
2130 : EditAction::eRemoveFontWeightProperty
;
2132 if (&aProperty
== nsGkAtoms::i
) {
2133 return aToSetStyle
? EditAction::eSetTextStyleProperty
2134 : EditAction::eRemoveTextStyleProperty
;
2136 if (&aProperty
== nsGkAtoms::u
) {
2137 return aToSetStyle
? EditAction::eSetTextDecorationPropertyUnderline
2138 : EditAction::eRemoveTextDecorationPropertyUnderline
;
2140 if (&aProperty
== nsGkAtoms::strike
) {
2141 return aToSetStyle
? EditAction::eSetTextDecorationPropertyLineThrough
2142 : EditAction::eRemoveTextDecorationPropertyLineThrough
;
2144 if (&aProperty
== nsGkAtoms::sup
) {
2145 return aToSetStyle
? EditAction::eSetVerticalAlignPropertySuper
2146 : EditAction::eRemoveVerticalAlignPropertySuper
;
2148 if (&aProperty
== nsGkAtoms::sub
) {
2149 return aToSetStyle
? EditAction::eSetVerticalAlignPropertySub
2150 : EditAction::eRemoveVerticalAlignPropertySub
;
2152 if (&aProperty
== nsGkAtoms::font
) {
2153 if (aAttribute
== nsGkAtoms::face
) {
2154 return aToSetStyle
? EditAction::eSetFontFamilyProperty
2155 : EditAction::eRemoveFontFamilyProperty
;
2157 if (aAttribute
== nsGkAtoms::color
) {
2158 return aToSetStyle
? EditAction::eSetColorProperty
2159 : EditAction::eRemoveColorProperty
;
2161 if (aAttribute
== nsGkAtoms::bgcolor
) {
2162 return aToSetStyle
? EditAction::eSetBackgroundColorPropertyInline
2163 : EditAction::eRemoveBackgroundColorPropertyInline
;
2166 return aToSetStyle
? EditAction::eSetInlineStyleProperty
2167 : EditAction::eRemoveInlineStyleProperty
;
2170 EditAction
HTMLEditUtils::GetEditActionForAlignment(
2171 const nsAString
& aAlignType
) {
2172 // This method may be in a hot path. So, return only necessary
2173 // EditAction::eAlign*.
2174 if (aAlignType
.EqualsLiteral("left")) {
2175 return EditAction::eAlignLeft
;
2177 if (aAlignType
.EqualsLiteral("right")) {
2178 return EditAction::eAlignRight
;
2180 if (aAlignType
.EqualsLiteral("center")) {
2181 return EditAction::eAlignCenter
;
2183 if (aAlignType
.EqualsLiteral("justify")) {
2184 return EditAction::eJustify
;
2186 return EditAction::eSetAlignment
;
2190 template <typename EditorDOMPointType
>
2191 nsIContent
* HTMLEditUtils::GetContentToPreserveInlineStyles(
2192 const EditorDOMPointType
& aPoint
, const Element
& aEditingHost
) {
2193 MOZ_ASSERT(aPoint
.IsSetAndValid());
2194 if (MOZ_UNLIKELY(!aPoint
.IsInContentNode())) {
2197 // If it points middle of a text node, use it. Otherwise, scan next visible
2198 // thing and use the style of following text node if there is.
2199 if (aPoint
.IsInTextNode() && !aPoint
.IsEndOfContainer()) {
2200 return aPoint
.template ContainerAs
<nsIContent
>();
2202 for (auto point
= aPoint
.template To
<EditorRawDOMPoint
>(); point
.IsSet();) {
2203 const WSScanResult nextVisibleThing
=
2204 WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary(
2205 &aEditingHost
, point
,
2206 BlockInlineCheck::UseComputedDisplayOutsideStyle
);
2207 if (nextVisibleThing
.InVisibleOrCollapsibleCharacters()) {
2208 return nextVisibleThing
.TextPtr();
2210 // Ignore empty inline container elements because it's not visible for
2211 // users so that using the style will appear suddenly from point of
2213 if (nextVisibleThing
.ReachedSpecialContent() &&
2214 nextVisibleThing
.IsContentEditable() &&
2215 nextVisibleThing
.ContentIsElement() &&
2216 !nextVisibleThing
.ElementPtr()->HasChildNodes() &&
2217 HTMLEditUtils::IsContainerNode(*nextVisibleThing
.ElementPtr())) {
2218 point
.SetAfter(nextVisibleThing
.ElementPtr());
2221 // Otherwise, we should use style of the container of the start point.
2224 return aPoint
.template ContainerAs
<nsIContent
>();
2227 template <typename EditorDOMPointType
, typename EditorDOMPointTypeInput
>
2228 EditorDOMPointType
HTMLEditUtils::GetBetterInsertionPointFor(
2229 const nsIContent
& aContentToInsert
,
2230 const EditorDOMPointTypeInput
& aPointToInsert
,
2231 const Element
& aEditingHost
) {
2232 if (NS_WARN_IF(!aPointToInsert
.IsSet())) {
2233 return EditorDOMPointType();
2236 auto pointToInsert
=
2237 aPointToInsert
.template GetNonAnonymousSubtreePoint
<EditorDOMPointType
>();
2239 NS_WARN_IF(!pointToInsert
.IsSet()) ||
2240 NS_WARN_IF(!pointToInsert
.GetContainer()->IsInclusiveDescendantOf(
2242 // Cannot insert aContentToInsert into this DOM tree.
2243 return EditorDOMPointType();
2246 // If the node to insert is not a block level element, we can insert it
2248 if (!HTMLEditUtils::IsBlockElement(
2249 aContentToInsert
, BlockInlineCheck::UseComputedDisplayStyle
)) {
2250 return pointToInsert
;
2253 WSRunScanner
wsScannerForPointToInsert(
2254 const_cast<Element
*>(&aEditingHost
), pointToInsert
,
2255 BlockInlineCheck::UseComputedDisplayStyle
);
2257 // If the insertion position is after the last visible item in a line,
2258 // i.e., the insertion position is just before a visible line break <br>,
2259 // we want to skip to the position just after the line break (see bug 68767).
2260 const WSScanResult forwardScanFromPointToInsertResult
=
2261 wsScannerForPointToInsert
.ScanInclusiveNextVisibleNodeOrBlockBoundaryFrom(
2263 // So, if the next visible node isn't a <br> element, we can insert the block
2264 // level element to the point.
2265 if (!forwardScanFromPointToInsertResult
.ReachedBRElement()) {
2266 return pointToInsert
;
2269 // However, we must not skip next <br> element when the caret appears to be
2270 // positioned at the beginning of a block, in that case skipping the <br>
2271 // would not insert the <br> at the caret position, but after the current
2273 const WSScanResult backwardScanFromPointToInsertResult
=
2274 wsScannerForPointToInsert
.ScanPreviousVisibleNodeOrBlockBoundaryFrom(
2276 // So, if there is no previous visible node,
2277 // or, if both nodes of the insertion point is <br> elements,
2278 // or, if the previous visible node is different block,
2279 // we need to skip the following <br>. So, otherwise, we can insert the
2280 // block at the insertion point.
2281 if (NS_WARN_IF(backwardScanFromPointToInsertResult
.Failed()) ||
2282 backwardScanFromPointToInsertResult
.ReachedInlineEditingHostBoundary() ||
2283 backwardScanFromPointToInsertResult
.ReachedBRElement() ||
2284 backwardScanFromPointToInsertResult
.ReachedCurrentBlockBoundary()) {
2285 return pointToInsert
;
2288 return forwardScanFromPointToInsertResult
2289 .template PointAfterReachedContent
<EditorDOMPointType
>();
2293 template <typename EditorDOMPointType
, typename EditorDOMPointTypeInput
>
2294 EditorDOMPointType
HTMLEditUtils::GetBetterCaretPositionToInsertText(
2295 const EditorDOMPointTypeInput
& aPoint
, const Element
& aEditingHost
) {
2296 MOZ_ASSERT(aPoint
.IsSetAndValid());
2298 aPoint
.GetContainer()->IsInclusiveFlatTreeDescendantOf(&aEditingHost
));
2300 if (aPoint
.IsInTextNode()) {
2301 return aPoint
.template To
<EditorDOMPointType
>();
2303 if (!aPoint
.IsEndOfContainer() && aPoint
.GetChild() &&
2304 aPoint
.GetChild()->IsText()) {
2305 return EditorDOMPointType(aPoint
.GetChild(), 0u);
2307 if (aPoint
.IsEndOfContainer()) {
2308 WSRunScanner
scanner(&aEditingHost
, aPoint
,
2309 BlockInlineCheck::UseComputedDisplayStyle
);
2310 const WSScanResult previousThing
=
2311 scanner
.ScanPreviousVisibleNodeOrBlockBoundaryFrom(aPoint
);
2312 if (previousThing
.InVisibleOrCollapsibleCharacters()) {
2313 return EditorDOMPointType::AtEndOf(*previousThing
.TextPtr());
2316 if (HTMLEditUtils::CanNodeContain(*aPoint
.GetContainer(),
2317 *nsGkAtoms::textTagName
)) {
2318 return aPoint
.template To
<EditorDOMPointType
>();
2320 if (MOZ_UNLIKELY(aPoint
.GetContainer() == &aEditingHost
||
2321 !aPoint
.template GetContainerParentAs
<nsIContent
>() ||
2322 !HTMLEditUtils::CanNodeContain(
2323 *aPoint
.template ContainerParentAs
<nsIContent
>(),
2324 *nsGkAtoms::textTagName
))) {
2325 return EditorDOMPointType();
2327 return aPoint
.ParentPoint().template To
<EditorDOMPointType
>();
2331 template <typename EditorDOMPointType
, typename EditorDOMPointTypeInput
>
2332 Result
<EditorDOMPointType
, nsresult
>
2333 HTMLEditUtils::ComputePointToPutCaretInElementIfOutside(
2334 const Element
& aElement
, const EditorDOMPointTypeInput
& aCurrentPoint
) {
2335 MOZ_ASSERT(aCurrentPoint
.IsSet());
2337 // FYI: This was moved from
2338 // https://searchfox.org/mozilla-central/rev/d3c2f51d89c3ca008ff0cb5a057e77ccd973443e/editor/libeditor/HTMLEditSubActionHandler.cpp#9193
2340 // Use range boundaries and RangeUtils::CompareNodeToRange() to compare
2341 // selection start to new block.
2342 bool nodeBefore
, nodeAfter
;
2343 nsresult rv
= RangeUtils::CompareNodeToRangeBoundaries(
2344 const_cast<Element
*>(&aElement
), aCurrentPoint
.ToRawRangeBoundary(),
2345 aCurrentPoint
.ToRawRangeBoundary(), &nodeBefore
, &nodeAfter
);
2346 if (NS_FAILED(rv
)) {
2347 NS_WARNING("RangeUtils::CompareNodeToRange() failed");
2351 if (nodeBefore
&& nodeAfter
) {
2352 return EditorDOMPointType(); // aCurrentPoint is in aElement
2356 // selection is after block. put at end of block.
2357 const nsIContent
* lastEditableContent
= HTMLEditUtils::GetLastChild(
2358 aElement
, {WalkTreeOption::IgnoreNonEditableNode
});
2359 if (!lastEditableContent
) {
2360 lastEditableContent
= &aElement
;
2362 if (lastEditableContent
->IsText() ||
2363 HTMLEditUtils::IsContainerNode(*lastEditableContent
)) {
2364 return EditorDOMPointType::AtEndOf(*lastEditableContent
);
2366 MOZ_ASSERT(lastEditableContent
->GetParentNode());
2367 return EditorDOMPointType::After(*lastEditableContent
);
2370 // selection is before block. put at start of block.
2371 const nsIContent
* firstEditableContent
= HTMLEditUtils::GetFirstChild(
2372 aElement
, {WalkTreeOption::IgnoreNonEditableNode
});
2373 if (!firstEditableContent
) {
2374 firstEditableContent
= &aElement
;
2376 if (firstEditableContent
->IsText() ||
2377 HTMLEditUtils::IsContainerNode(*firstEditableContent
)) {
2378 MOZ_ASSERT(firstEditableContent
->GetParentNode());
2379 // XXX Shouldn't this be EditorDOMPointType(firstEditableContent, 0u)?
2380 return EditorDOMPointType(firstEditableContent
);
2382 // XXX And shouldn't this be EditorDOMPointType(firstEditableContent)?
2383 return EditorDOMPointType(firstEditableContent
, 0u);
2387 bool HTMLEditUtils::IsInlineStyleSetByElement(
2388 const nsIContent
& aContent
, const EditorInlineStyle
& aStyle
,
2389 const nsAString
* aValue
, nsAString
* aOutValue
/* = nullptr */) {
2390 for (Element
* element
: aContent
.InclusiveAncestorsOfType
<Element
>()) {
2391 if (aStyle
.mHTMLProperty
!= element
->NodeInfo()->NameAtom()) {
2394 if (!aStyle
.mAttribute
) {
2398 element
->GetAttr(aStyle
.mAttribute
, value
);
2402 if (!value
.IsEmpty()) {
2406 if (aValue
->Equals(value
, nsCaseInsensitiveStringComparator
)) {
2409 // We found the prop with the attribute, but the value doesn't match.
2417 size_t HTMLEditUtils::CollectChildren(
2418 const nsINode
& aNode
,
2419 nsTArray
<OwningNonNull
<nsIContent
>>& aOutArrayOfContents
,
2420 size_t aIndexToInsertChildren
, const CollectChildrenOptions
& aOptions
) {
2421 // FYI: This was moved from
2422 // https://searchfox.org/mozilla-central/rev/4bce7d85ba4796dd03c5dcc7cfe8eee0e4c07b3b/editor/libeditor/HTMLEditSubActionHandler.cpp#6261
2424 size_t numberOfFoundChildren
= 0;
2425 for (nsIContent
* content
=
2426 GetFirstChild(aNode
, {WalkTreeOption::IgnoreNonEditableNode
});
2427 content
; content
= content
->GetNextSibling()) {
2428 if ((aOptions
.contains(CollectChildrenOption::CollectListChildren
) &&
2429 (HTMLEditUtils::IsAnyListElement(content
) ||
2430 HTMLEditUtils::IsListItem(content
))) ||
2431 (aOptions
.contains(CollectChildrenOption::CollectTableChildren
) &&
2432 HTMLEditUtils::IsAnyTableElement(content
))) {
2433 numberOfFoundChildren
+= HTMLEditUtils::CollectChildren(
2434 *content
, aOutArrayOfContents
,
2435 aIndexToInsertChildren
+ numberOfFoundChildren
, aOptions
);
2439 if (aOptions
.contains(CollectChildrenOption::IgnoreNonEditableChildren
) &&
2440 !EditorUtils::IsEditableContent(*content
, EditorType::HTML
)) {
2443 if (aOptions
.contains(CollectChildrenOption::IgnoreInvisibleTextNodes
) &&
2444 content
->IsText() &&
2445 !HTMLEditUtils::IsVisibleTextNode(*content
->AsText())) {
2448 aOutArrayOfContents
.InsertElementAt(
2449 aIndexToInsertChildren
+ numberOfFoundChildren
++, *content
);
2451 return numberOfFoundChildren
;
2455 size_t HTMLEditUtils::CollectEmptyInlineContainerDescendants(
2456 const nsINode
& aNode
,
2457 nsTArray
<OwningNonNull
<nsIContent
>>& aOutArrayOfContents
,
2458 const EmptyCheckOptions
& aOptions
, BlockInlineCheck aBlockInlineCheck
) {
2459 size_t numberOfFoundElements
= 0;
2460 for (Element
* element
= aNode
.GetFirstElementChild(); element
;) {
2461 if (HTMLEditUtils::IsEmptyInlineContainer(*element
, aOptions
,
2462 aBlockInlineCheck
)) {
2463 aOutArrayOfContents
.AppendElement(*element
);
2464 numberOfFoundElements
++;
2465 nsIContent
* nextContent
= element
->GetNextNonChildNode(&aNode
);
2467 for (; nextContent
; nextContent
= nextContent
->GetNextNode(&aNode
)) {
2468 if (nextContent
->IsElement()) {
2469 element
= nextContent
->AsElement();
2476 nsIContent
* nextContent
= element
->GetNextNode(&aNode
);
2478 for (; nextContent
; nextContent
= nextContent
->GetNextNode(&aNode
)) {
2479 if (nextContent
->IsElement()) {
2480 element
= nextContent
->AsElement();
2485 return numberOfFoundElements
;
2489 bool HTMLEditUtils::ElementHasAttributeExcept(const Element
& aElement
,
2490 const nsAtom
& aAttribute1
,
2491 const nsAtom
& aAttribute2
,
2492 const nsAtom
& aAttribute3
) {
2493 // FYI: This was moved from
2494 // https://searchfox.org/mozilla-central/rev/0b1543e85d13c30a13c57e959ce9815a3f0fa1d3/editor/libeditor/HTMLStyleEditor.cpp#1626
2495 for (auto i
: IntegerRange
<uint32_t>(aElement
.GetAttrCount())) {
2496 const nsAttrName
* name
= aElement
.GetAttrNameAt(i
);
2497 if (!name
->NamespaceEquals(kNameSpaceID_None
)) {
2501 if (name
->LocalName() == &aAttribute1
||
2502 name
->LocalName() == &aAttribute2
||
2503 name
->LocalName() == &aAttribute3
) {
2504 continue; // Ignore the given attribute
2507 // Ignore empty style, class and id attributes because those attributes are
2508 // not meaningful with empty value.
2509 if (name
->LocalName() == nsGkAtoms::style
||
2510 name
->LocalName() == nsGkAtoms::_class
||
2511 name
->LocalName() == nsGkAtoms::id
) {
2512 if (aElement
.HasNonEmptyAttr(name
->LocalName())) {
2518 // Ignore special _moz attributes
2519 nsAutoString attrString
;
2520 name
->LocalName()->ToString(attrString
);
2521 if (!StringBeginsWith(attrString
, u
"_moz"_ns
)) {
2525 // if we made it through all of them without finding a real attribute
2526 // other than aAttribute, then return true
2530 bool HTMLEditUtils::GetNormalizedHTMLColorValue(const nsAString
& aColorValue
,
2531 nsAString
& aNormalizedValue
) {
2533 if (!value
.ParseColor(aColorValue
)) {
2534 aNormalizedValue
= aColorValue
;
2537 nscolor color
= NS_RGB(0, 0, 0);
2538 MOZ_ALWAYS_TRUE(value
.GetColorValue(color
));
2539 aNormalizedValue
= NS_ConvertASCIItoUTF16(nsPrintfCString(
2540 "#%02x%02x%02x", NS_GET_R(color
), NS_GET_G(color
), NS_GET_B(color
)));
2544 bool HTMLEditUtils::IsSameHTMLColorValue(
2545 const nsAString
& aColorA
, const nsAString
& aColorB
,
2546 TransparentKeyword aTransparentKeyword
) {
2547 if (aTransparentKeyword
== TransparentKeyword::Allowed
) {
2548 const bool isATransparent
= aColorA
.LowerCaseEqualsLiteral("transparent");
2549 const bool isBTransparent
= aColorB
.LowerCaseEqualsLiteral("transparent");
2550 if (isATransparent
|| isBTransparent
) {
2551 return isATransparent
&& isBTransparent
;
2554 nsAttrValue valueA
, valueB
;
2555 if (!valueA
.ParseColor(aColorA
) || !valueB
.ParseColor(aColorB
)) {
2558 nscolor colorA
= NS_RGB(0, 0, 0), colorB
= NS_RGB(0, 0, 0);
2559 MOZ_ALWAYS_TRUE(valueA
.GetColorValue(colorA
));
2560 MOZ_ALWAYS_TRUE(valueB
.GetColorValue(colorB
));
2561 return colorA
== colorB
;
2564 bool HTMLEditUtils::MaybeCSSSpecificColorValue(const nsAString
& aColorValue
) {
2565 if (aColorValue
.IsEmpty() || aColorValue
.First() == '#') {
2566 return false; // Quick return for the most cases.
2569 nsAutoString
colorValue(aColorValue
);
2570 colorValue
.CompressWhitespace(true, true);
2571 if (colorValue
.LowerCaseEqualsASCII("transparent")) {
2574 nscolor color
= NS_RGB(0, 0, 0);
2575 if (colorValue
.IsEmpty() || colorValue
.First() == '#') {
2578 const NS_ConvertUTF16toUTF8
colorU8(colorValue
);
2579 if (Servo_ColorNameToRgb(&colorU8
, &color
)) {
2582 if (colorValue
.LowerCaseEqualsASCII("initial") ||
2583 colorValue
.LowerCaseEqualsASCII("inherit") ||
2584 colorValue
.LowerCaseEqualsASCII("unset") ||
2585 colorValue
.LowerCaseEqualsASCII("revert") ||
2586 colorValue
.LowerCaseEqualsASCII("currentcolor")) {
2589 return ServoCSSParser::IsValidCSSColor(colorU8
);
2592 static bool ComputeColor(const nsAString
& aColorValue
, nscolor
* aColor
,
2593 bool* aIsCurrentColor
) {
2594 return ServoCSSParser::ComputeColor(nullptr, NS_RGB(0, 0, 0),
2595 NS_ConvertUTF16toUTF8(aColorValue
),
2596 aColor
, aIsCurrentColor
);
2599 static bool ComputeColor(const nsACString
& aColorValue
, nscolor
* aColor
,
2600 bool* aIsCurrentColor
) {
2601 return ServoCSSParser::ComputeColor(nullptr, NS_RGB(0, 0, 0), aColorValue
,
2602 aColor
, aIsCurrentColor
);
2605 bool HTMLEditUtils::CanConvertToHTMLColorValue(const nsAString
& aColorValue
) {
2606 bool isCurrentColor
= false;
2607 nscolor color
= NS_RGB(0, 0, 0);
2608 return ComputeColor(aColorValue
, &color
, &isCurrentColor
) &&
2609 !isCurrentColor
&& NS_GET_A(color
) == 0xFF;
2612 bool HTMLEditUtils::ConvertToNormalizedHTMLColorValue(
2613 const nsAString
& aColorValue
, nsAString
& aNormalizedValue
) {
2614 bool isCurrentColor
= false;
2615 nscolor color
= NS_RGB(0, 0, 0);
2616 if (!ComputeColor(aColorValue
, &color
, &isCurrentColor
) || isCurrentColor
||
2617 NS_GET_A(color
) != 0xFF) {
2618 aNormalizedValue
= aColorValue
;
2621 aNormalizedValue
.Truncate();
2622 aNormalizedValue
.AppendPrintf("#%02x%02x%02x", NS_GET_R(color
),
2623 NS_GET_G(color
), NS_GET_B(color
));
2627 bool HTMLEditUtils::GetNormalizedCSSColorValue(const nsAString
& aColorValue
,
2628 ZeroAlphaColor aZeroAlphaColor
,
2629 nsAString
& aNormalizedValue
) {
2630 bool isCurrentColor
= false;
2631 nscolor color
= NS_RGB(0, 0, 0);
2632 if (!ComputeColor(aColorValue
, &color
, &isCurrentColor
)) {
2633 aNormalizedValue
= aColorValue
;
2637 // If it's currentcolor, let's return it as-is since we cannot resolve it
2638 // without ancestors.
2639 if (isCurrentColor
) {
2640 aNormalizedValue
= aColorValue
;
2644 if (aZeroAlphaColor
== ZeroAlphaColor::TransparentKeyword
&&
2645 NS_GET_A(color
) == 0) {
2646 aNormalizedValue
.AssignLiteral("transparent");
2650 // Get serialized color value (i.e., "rgb()" or "rgba()").
2651 aNormalizedValue
.Truncate();
2652 nsStyleUtil::GetSerializedColorValue(color
, aNormalizedValue
);
2656 template <typename CharType
>
2657 bool HTMLEditUtils::IsSameCSSColorValue(const nsTSubstring
<CharType
>& aColorA
,
2658 const nsTSubstring
<CharType
>& aColorB
) {
2659 bool isACurrentColor
= false;
2660 nscolor colorA
= NS_RGB(0, 0, 0);
2661 if (!ComputeColor(aColorA
, &colorA
, &isACurrentColor
)) {
2664 bool isBCurrentColor
= false;
2665 nscolor colorB
= NS_RGB(0, 0, 0);
2666 if (!ComputeColor(aColorB
, &colorB
, &isBCurrentColor
)) {
2669 if (isACurrentColor
|| isBCurrentColor
) {
2670 return isACurrentColor
&& isBCurrentColor
;
2672 return colorA
== colorB
;
2675 /******************************************************************************
2676 * SelectedTableCellScanner
2677 ******************************************************************************/
2679 SelectedTableCellScanner::SelectedTableCellScanner(
2680 const AutoRangeArray
& aRanges
) {
2681 if (aRanges
.Ranges().IsEmpty()) {
2684 Element
* firstSelectedCellElement
=
2685 HTMLEditUtils::GetTableCellElementIfOnlyOneSelected(
2686 aRanges
.FirstRangeRef());
2687 if (!firstSelectedCellElement
) {
2688 return; // We're not in table cell selection mode.
2690 mSelectedCellElements
.SetCapacity(aRanges
.Ranges().Length());
2691 mSelectedCellElements
.AppendElement(*firstSelectedCellElement
);
2692 for (uint32_t i
= 1; i
< aRanges
.Ranges().Length(); i
++) {
2693 nsRange
* range
= aRanges
.Ranges()[i
];
2694 if (NS_WARN_IF(!range
) || NS_WARN_IF(!range
->IsPositioned())) {
2695 continue; // Shouldn't occur in normal conditions.
2697 // Just ignore selection ranges which do not select only one table
2698 // cell element. This is possible case if web apps sets multiple
2699 // selections and first range selects a table cell element.
2700 if (Element
* selectedCellElement
=
2701 HTMLEditUtils::GetTableCellElementIfOnlyOneSelected(*range
)) {
2702 mSelectedCellElements
.AppendElement(*selectedCellElement
);
2707 } // namespace mozilla