From c4c3b160ebd9f20e953178296f4fa84c6c26ed63 Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Fri, 6 Sep 2019 04:32:36 +0000 Subject: [PATCH] Bug 1574852 - part 68: Make `HTMLEditRules::GetDefinitionListItemTypes()` to a stack class r=m_kato It scans children and returns whether `
` and `
` are found or not. So, we can make it a stack class and makes caller pick the necessary value with getter methods. Differential Revision: https://phabricator.services.mozilla.com/D44461 --HG-- extra : moz-landing-system : lando --- editor/libeditor/HTMLEditRules.cpp | 28 ++++--------------- editor/libeditor/HTMLEditRules.h | 2 -- editor/libeditor/HTMLEditUtils.h | 44 ++++++++++++++++++++++++++---- 3 files changed, 45 insertions(+), 29 deletions(-) diff --git a/editor/libeditor/HTMLEditRules.cpp b/editor/libeditor/HTMLEditRules.cpp index 68d6196c463e..d944f2d1c5e7 100644 --- a/editor/libeditor/HTMLEditRules.cpp +++ b/editor/libeditor/HTMLEditRules.cpp @@ -974,11 +974,13 @@ nsresult HTMLEditRules::GetListItemState(bool* aMixed, bool* aLI, bool* aDT, } else if (node->IsHTMLElement(nsGkAtoms::dd)) { *aDD = true; } else if (node->IsHTMLElement(nsGkAtoms::dl)) { + if (*aDT && *aDD) { + continue; + } // need to look inside dl and see which types of items it has - bool bDT, bDD; - GetDefinitionListItemTypes(node->AsElement(), &bDT, &bDD); - *aDT |= bDT; - *aDD |= bDD; + DefinitionListItemScanner scanner(*node->AsElement()); + *aDT |= scanner.DTElementFound(); + *aDD |= scanner.DDElementFound(); } else { bNonList = true; } @@ -7871,24 +7873,6 @@ Element* HTMLEditor::GetDeepestEditableOnlyChildDivBlockquoteOrListElement( return parentElement; } -void HTMLEditRules::GetDefinitionListItemTypes(dom::Element* aElement, - bool* aDT, bool* aDD) const { - MOZ_ASSERT(aElement); - MOZ_ASSERT(aElement->IsHTMLElement(nsGkAtoms::dl)); - MOZ_ASSERT(aDT); - MOZ_ASSERT(aDD); - - *aDT = *aDD = false; - for (nsIContent* child = aElement->GetFirstChild(); child; - child = child->GetNextSibling()) { - if (child->IsHTMLElement(nsGkAtoms::dt)) { - *aDT = true; - } else if (child->IsHTMLElement(nsGkAtoms::dd)) { - *aDD = true; - } - } -} - nsresult HTMLEditRules::GetParagraphFormatNodes( nsTArray>& outArrayOfNodes) { MOZ_ASSERT(IsEditorDataAvailable()); diff --git a/editor/libeditor/HTMLEditRules.h b/editor/libeditor/HTMLEditRules.h index e9687021941c..dca5584a3699 100644 --- a/editor/libeditor/HTMLEditRules.h +++ b/editor/libeditor/HTMLEditRules.h @@ -367,8 +367,6 @@ class HTMLEditRules : public TextEditRules { OutdentPartOfBlock(Element& aBlockElement, nsIContent& aStartOfOutdent, nsIContent& aEndOutdent, bool aIsBlockIndentedWithCSS); - void GetDefinitionListItemTypes(Element* aElement, bool* aDT, - bool* aDD) const; MOZ_CAN_RUN_SCRIPT nsresult GetParagraphFormatNodes( nsTArray>& outArrayOfNodes); diff --git a/editor/libeditor/HTMLEditUtils.h b/editor/libeditor/HTMLEditUtils.h index 2bed0f826189..7030755a4a2b 100644 --- a/editor/libeditor/HTMLEditUtils.h +++ b/editor/libeditor/HTMLEditUtils.h @@ -6,15 +6,13 @@ #ifndef HTMLEditUtils_h #define HTMLEditUtils_h -#include +#include "mozilla/Attributes.h" +#include "mozilla/dom/Element.h" +#include "nsGkAtoms.h" class nsAtom; -class nsINode; namespace mozilla { -namespace dom { -class Element; -} // namespace dom class HTMLEditUtils final { public: @@ -64,6 +62,42 @@ class HTMLEditUtils final { static EditAction GetEditActionForAlignment(const nsAString& aAlignType); }; +/** + * DefinitionListItemScanner() scans given `
` element's children. + * Then, you can check whether `
` and/or `
` elements are in it. + */ +class MOZ_STACK_CLASS DefinitionListItemScanner final { + public: + DefinitionListItemScanner() = delete; + explicit DefinitionListItemScanner(dom::Element& aDLElement) { + MOZ_ASSERT(aDLElement.IsHTMLElement(nsGkAtoms::dl)); + for (nsIContent* child = aDLElement.GetFirstChild(); child; + child = child->GetNextSibling()) { + if (child->IsHTMLElement(nsGkAtoms::dt)) { + mDTFound = true; + if (mDDFound) { + break; + } + continue; + } + if (child->IsHTMLElement(nsGkAtoms::dd)) { + mDDFound = true; + if (mDTFound) { + break; + } + continue; + } + } + } + + bool DTElementFound() const { return mDTFound; } + bool DDElementFound() const { return mDDFound; } + + private: + bool mDTFound = false; + bool mDDFound = false; +}; + } // namespace mozilla #endif // #ifndef HTMLEditUtils_h