/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #ifndef HTMLEditUtils_h #define HTMLEditUtils_h /** * This header declares/defines static helper methods as members of * HTMLEditUtils. If you want to create or look for helper trivial classes for * HTMLEditor, see HTMLEditHelpers.h. */ #include "EditorBase.h" #include "EditorDOMPoint.h" #include "EditorForwards.h" #include "EditorLineBreak.h" #include "EditorUtils.h" #include "HTMLEditHelpers.h" #include "mozilla/Attributes.h" #include "mozilla/EnumSet.h" #include "mozilla/IntegerRange.h" #include "mozilla/Maybe.h" #include "mozilla/Result.h" #include "mozilla/dom/AbstractRange.h" #include "mozilla/dom/AncestorIterator.h" #include "mozilla/dom/CharacterDataBuffer.h" #include "mozilla/dom/Element.h" #include "mozilla/dom/HTMLBRElement.h" #include "mozilla/dom/Selection.h" #include "mozilla/dom/Text.h" #include "nsContentUtils.h" #include "nsCRT.h" #include "nsGkAtoms.h" #include "nsHTMLTags.h" #include "nsTArray.h" class nsAtom; class nsPresContext; namespace mozilla { enum class CollectChildrenOption { // Ignore non-editable nodes IgnoreNonEditableChildren, // Ignore invisible text nodes IgnoreInvisibleTextNodes, // Collect list children too. CollectListChildren, // Collect table children too. CollectTableChildren, }; class HTMLEditUtils final { using AbstractRange = dom::AbstractRange; using Element = dom::Element; using Selection = dom::Selection; using Text = dom::Text; using WhitespaceOption = dom::CharacterDataBuffer::WhitespaceOption; using WhitespaceOptions = dom::CharacterDataBuffer::WhitespaceOptions; public: static constexpr char16_t kNewLine = '\n'; static constexpr char16_t kCarriageReturn = '\r'; static constexpr char16_t kTab = '\t'; static constexpr char16_t kSpace = ' '; static constexpr char16_t kNBSP = 0x00A0; static constexpr char16_t kGreaterThan = '>'; /** * IsSimplyEditableNode() returns true when aNode is simply editable. * This does NOT means that aNode can be removed from current parent nor * aNode's data is editable. */ static bool IsSimplyEditableNode(const nsINode& aNode) { return aNode.IsEditable(); } /** * Return true if aNode is editable or not in a composed doc. This is useful * if the caller may modify document fragment before inserting it into a * Document. */ static bool NodeIsEditableOrNotInComposedDoc(const nsINode& aNode) { return MOZ_UNLIKELY(!aNode.IsInComposedDoc()) || aNode.IsEditable(); } /** * Return true if aElement is an editing host which is either: * - the root element * - parent is not editable * - the element of the document */ [[nodiscard]] static bool ElementIsEditableRoot(const Element& aElement); /** * Return true if inclusive flat tree ancestor has `inert` state. */ static bool ContentIsInert(const nsIContent& aContent); /** * IsNeverContentEditableElementByUser() returns true if the element's content * is never editable by user. E.g., the content is always replaced by * native anonymous node or something. */ static bool IsNeverElementContentsEditableByUser(const nsIContent& aContent) { return aContent.IsElement() && (!HTMLEditUtils::IsContainerNode(aContent) || aContent.IsAnyOfHTMLElements( nsGkAtoms::applet, nsGkAtoms::colgroup, nsGkAtoms::frameset, nsGkAtoms::head, nsGkAtoms::html, nsGkAtoms::iframe, nsGkAtoms::meter, nsGkAtoms::progress, nsGkAtoms::select, nsGkAtoms::textarea)); } /** * IsNonEditableReplacedContent() returns true when aContent is an inclusive * descendant of a replaced element whose content shouldn't be editable by * user's operation. */ static bool IsNonEditableReplacedContent(const nsIContent& aContent) { for (Element* element : aContent.InclusiveAncestorsOfType()) { if (element->IsAnyOfHTMLElements(nsGkAtoms::select, nsGkAtoms::option, nsGkAtoms::optgroup)) { return true; } } return false; } /* * IsRemovalNode() returns true when parent of aContent is editable even * if aContent isn't editable. * This is a valid method to check it if you find the content from point * of view of siblings or parents of aContent. * Note that padding `
` element for empty editor and manual native * anonymous content should be deletable even after `HTMLEditor` is destroyed * because they are owned/managed by `HTMLEditor`. */ static bool IsRemovableNode(const nsIContent& aContent) { return EditorUtils::IsPaddingBRElementForEmptyEditor(aContent) || aContent.IsRootOfNativeAnonymousSubtree() || (aContent.GetParentNode() && aContent.GetParentNode()->IsEditable() && &aContent != aContent.OwnerDoc()->GetBody() && &aContent != aContent.OwnerDoc()->GetDocumentElement()); } /** * IsRemovableFromParentNode() returns true when aContent is editable, has a * parent node and the parent node is also editable. * This is a valid method to check it if you find the content from point * of view of descendants of aContent. * Note that padding `
` element for empty editor and manual native * anonymous content should be deletable even after `HTMLEditor` is destroyed * because they are owned/managed by `HTMLEditor`. */ static bool IsRemovableFromParentNode(const nsIContent& aContent) { return EditorUtils::IsPaddingBRElementForEmptyEditor(aContent) || aContent.IsRootOfNativeAnonymousSubtree() || (aContent.IsEditable() && aContent.GetParentNode() && aContent.GetParentNode()->IsEditable() && &aContent != aContent.OwnerDoc()->GetBody() && &aContent != aContent.OwnerDoc()->GetDocumentElement()); } /** * CanContentsBeJoined() returns true if aLeftContent and aRightContent can be * joined. */ static bool CanContentsBeJoined(const nsIContent& aLeftContent, const nsIContent& aRightContent); /** * Returns true if aContent is an element and it should be treated as a block. * * @param aBlockInlineCheck * - If UseHTMLDefaultStyle, this returns true only for HTML elements which * are defined as a block by the default style. I.e., non-HTML elements are * always treated as inline. * - If UseComputedDisplayOutsideStyle, this returns true for element nodes * whose display-outside is not inline nor ruby. This is useful to get * inclusive ancestor block element. * - If UseComputedDisplayStyle, this returns true for element nodes whose * display-outside is not inline or whose display-inside is flow-root and they * do not appear as a form control. This is useful to check whether * collapsible white-spaces at the element edges are visible or invisible or * whether
element at end of the element is visible or invisible. */ [[nodiscard]] static bool IsBlockElement(const nsIContent& aContent, BlockInlineCheck aBlockInlineCheck); /** * This is designed to check elements or non-element nodes which are layed out * as inline. Therefore, inline-block etc and ruby are treated as inline. * Note that invisible non-element nodes like comment nodes are also treated * as inline. * * @param aBlockInlineCheck UseComputedDisplayOutsideStyle and * UseComputedDisplayStyle return same result for * any elements. */ [[nodiscard]] static bool IsInlineContent(const nsIContent& aContent, BlockInlineCheck aBlockInlineCheck); /** * IsVisibleElementEvenIfLeafNode() returns true if aContent is an empty block * element, a visible replaced element such as a form control. This does not * check the layout information. */ static bool IsVisibleElementEvenIfLeafNode(const nsIContent& aContent); /** * Return true if aContent is an inline element which formats the content * without giving any meanings. E.g., , , , , etc, but * not , , etc. */ [[nodiscard]] static bool IsInlineStyleElement(const nsIContent& aContent); /** * IsDisplayOutsideInline() returns true if display-outside value is * "inside". This does NOT flush the layout. */ [[nodiscard]] static bool IsDisplayOutsideInline(const Element& aElement); /** * IsDisplayInsideFlowRoot() returns true if display-inline value of aElement * is "flow-root". This does NOT flush the layout. */ [[nodiscard]] static bool IsDisplayInsideFlowRoot(const Element& aElement); /** * Return true if aElement is a flex item or a grid item. This works only * when aElement has a primary frame. */ [[nodiscard]] static bool IsFlexOrGridItem(const Element& aElement); /** * IsRemovableInlineStyleElement() returns true if aElement is an inline * element and can be removed or split to in order to modifying inline * styles. */ static bool IsRemovableInlineStyleElement(Element& aElement); /** * Return true if aTagName is one of the format element name of * Document.execCommand("formatBlock"). */ [[nodiscard]] static bool IsFormatTagForFormatBlockCommand( const nsStaticAtom& aTagName) { return // clang-format off &aTagName == nsGkAtoms::address || &aTagName == nsGkAtoms::article || &aTagName == nsGkAtoms::aside || &aTagName == nsGkAtoms::blockquote || &aTagName == nsGkAtoms::dd || &aTagName == nsGkAtoms::div || &aTagName == nsGkAtoms::dl || &aTagName == nsGkAtoms::dt || &aTagName == nsGkAtoms::footer || &aTagName == nsGkAtoms::h1 || &aTagName == nsGkAtoms::h2 || &aTagName == nsGkAtoms::h3 || &aTagName == nsGkAtoms::h4 || &aTagName == nsGkAtoms::h5 || &aTagName == nsGkAtoms::h6 || &aTagName == nsGkAtoms::header || &aTagName == nsGkAtoms::hgroup || &aTagName == nsGkAtoms::main || &aTagName == nsGkAtoms::nav || &aTagName == nsGkAtoms::p || &aTagName == nsGkAtoms::pre || &aTagName == nsGkAtoms::section; // clang-format on } /** * Return true if aContent is a format element of * Document.execCommand("formatBlock"). */ [[nodiscard]] static bool IsFormatElementForFormatBlockCommand( const nsIContent& aContent) { if (!aContent.IsHTMLElement() || !aContent.NodeInfo()->NameAtom()->IsStatic()) { return false; } const nsStaticAtom* tagName = aContent.NodeInfo()->NameAtom()->AsStatic(); return IsFormatTagForFormatBlockCommand(*tagName); } /** * Return true if aTagName is one of the format element name of * cmd_paragraphState. */ [[nodiscard]] static bool IsFormatTagForParagraphStateCommand( const nsStaticAtom& aTagName) { return // clang-format off &aTagName == nsGkAtoms::address || &aTagName == nsGkAtoms::dd || &aTagName == nsGkAtoms::dl || &aTagName == nsGkAtoms::dt || &aTagName == nsGkAtoms::h1 || &aTagName == nsGkAtoms::h2 || &aTagName == nsGkAtoms::h3 || &aTagName == nsGkAtoms::h4 || &aTagName == nsGkAtoms::h5 || &aTagName == nsGkAtoms::h6 || &aTagName == nsGkAtoms::p || &aTagName == nsGkAtoms::pre; // clang-format on } /** * Return true if aContent is a format element of cmd_paragraphState. */ [[nodiscard]] static bool IsFormatElementForParagraphStateCommand( const nsIContent& aContent) { if (!aContent.IsHTMLElement() || !aContent.NodeInfo()->NameAtom()->IsStatic()) { return false; } const nsStaticAtom* tagName = aContent.NodeInfo()->NameAtom()->AsStatic(); return IsFormatTagForParagraphStateCommand(*tagName); } /** * Return true if aContent is an element which can be outdented such as * a list element, a list-item element or a
. */ [[nodiscard]] static bool IsOutdentable(const nsIContent& aContent); /** * Return true if aContent is one of

,

,

,

,

or
. */ [[nodiscard]] static bool IsHeadingElement(const nsIContent& aContent); /** * Return true if aContent is a list item element such as
  • ,
    or
    . */ [[nodiscard]] static bool IsListItemElement(const nsIContent& aContent); [[nodiscard]] static bool IsListItemElement(const nsIContent* aContent) { return aContent && IsListItemElement(*aContent); } /** * Return true if aContent is a . */ [[nodiscard]] static bool IsTableRowElement(const nsIContent& aContent); [[nodiscard]] static bool IsTableRowElement(const nsIContent* aContent) { return aContent && IsTableRowElement(*aContent); } /** * Return true if aContent is an element which makes a table and is not a * nor a . So, , , , , nor
    ,
    , etc. */ [[nodiscard]] static bool IsAnyTableElementExceptColumnElement( const nsIContent& aContent); /** * Return true if aContent is an element which makes a table and nis not a *
    . */ [[nodiscard]] static bool IsAnyTableElementExceptTableElementAndColumElement( const nsIContent& aContent); /** * Return true if aContent is a table cell such as
    or . */ [[nodiscard]] static bool IsTableCellElement(const nsIContent& aContent); [[nodiscard]] static bool IsTableCellElement(const nsIContent* aContent) { return aContent && IsTableCellElement(*aContent); } /** * Return true if aContent is a table cell or a caption, i.e., that may * contain visible content. */ [[nodiscard]] static bool IsTableCellOrCaptionElement( const nsIContent& aContent); /** * Return true if aContent is a list element such as