Commit d550a723 authored by Masayuki Nakano's avatar Masayuki Nakano
Browse files

Bug 1658472 - part 6: Make some utility methods for...

Bug 1658472 - part 6: Make some utility methods for `AutoInclusiveAncestorBlockElementsJoiner` r=m_kato

This patch changes the behavior in the following 2 points:
* When both content is in same block element, won't return error
* When `<hr>` element has block children accidentally, this solves its container

The former case must not change actual behavior because
`AutoBlockElementsJoiner` is used with content nodes which are in different
blocks.

Differential Revision: https://phabricator.services.mozilla.com/D86886
parent 909288b9
Loading
Loading
Loading
Loading
+10 −29
Original line number Original line Diff line number Diff line
@@ -4624,16 +4624,14 @@ HTMLEditor::AutoBlockElementsJoiner::JoinNodesDeepWithTransaction(


EditActionResult HTMLEditor::AutoBlockElementsJoiner::
EditActionResult HTMLEditor::AutoBlockElementsJoiner::
    AutoInclusiveAncestorBlockElementsJoiner::Prepare() {
    AutoInclusiveAncestorBlockElementsJoiner::Prepare() {
  mLeftBlockElement = HTMLEditUtils::GetInclusiveAncestorBlockElement(
  mLeftBlockElement =
      HTMLEditUtils::GetInclusiveAncestorBlockElementExceptHRElement(
          mInclusiveDescendantOfLeftBlockElement);
          mInclusiveDescendantOfLeftBlockElement);
  mRightBlockElement = HTMLEditUtils::GetInclusiveAncestorBlockElement(
  mRightBlockElement =
      HTMLEditUtils::GetInclusiveAncestorBlockElementExceptHRElement(
          mInclusiveDescendantOfRightBlockElement);
          mInclusiveDescendantOfRightBlockElement);


  // Sanity checks
  if (NS_WARN_IF(!IsSet())) {
  if (NS_WARN_IF(!mLeftBlockElement) || NS_WARN_IF(!mRightBlockElement)) {
    return EditActionIgnored(NS_ERROR_NULL_POINTER);
  }
  if (NS_WARN_IF(mLeftBlockElement == mRightBlockElement)) {
    return EditActionIgnored(NS_ERROR_UNEXPECTED);
    return EditActionIgnored(NS_ERROR_UNEXPECTED);
  }
  }


@@ -4643,25 +4641,8 @@ EditActionResult HTMLEditor::AutoBlockElementsJoiner::
    return EditActionCanceled();
    return EditActionCanceled();
  }
  }


  // Make sure we don't try to move things into HR's, which look like blocks
  // but aren't containers
  if (mLeftBlockElement->IsHTMLElement(nsGkAtoms::hr)) {
    mLeftBlockElement =
        HTMLEditUtils::GetAncestorBlockElement(*mLeftBlockElement);
    if (NS_WARN_IF(!mLeftBlockElement)) {
      return EditActionIgnored(NS_ERROR_UNEXPECTED);
    }
  }
  if (mRightBlockElement->IsHTMLElement(nsGkAtoms::hr)) {
    mRightBlockElement =
        HTMLEditUtils::GetAncestorBlockElement(*mRightBlockElement);
    if (NS_WARN_IF(!mRightBlockElement)) {
      return EditActionIgnored(NS_ERROR_UNEXPECTED);
    }
  }

  // Bail if both blocks the same
  // Bail if both blocks the same
  if (mLeftBlockElement == mRightBlockElement) {
  if (IsSameBlockElement()) {
    return EditActionIgnored();
    return EditActionIgnored();
  }
  }


@@ -4707,7 +4688,7 @@ EditActionResult HTMLEditor::AutoBlockElementsJoiner::
  MOZ_ASSERT(mLeftBlockElement);
  MOZ_ASSERT(mLeftBlockElement);
  MOZ_ASSERT(mRightBlockElement);
  MOZ_ASSERT(mRightBlockElement);


  if (mLeftBlockElement == mRightBlockElement) {
  if (IsSameBlockElement()) {
    return EditActionIgnored();
    return EditActionIgnored();
  }
  }


+18 −0
Original line number Original line Diff line number Diff line
@@ -528,6 +528,24 @@ class HTMLEditUtils final {
    return GetAncestorBlockElement(aContent, aAncestorLimiter);
    return GetAncestorBlockElement(aContent, aAncestorLimiter);
  }
  }


  /**
   * GetInclusiveAncestorBlockElementExceptHRElement() returns inclusive
   * ancestor block element except `<hr>` element.
   */
  static Element* GetInclusiveAncestorBlockElementExceptHRElement(
      const nsIContent& aContent, const nsINode* aAncestorLimiter = nullptr) {
    Element* blockElement =
        GetInclusiveAncestorBlockElement(aContent, aAncestorLimiter);
    if (!blockElement || !blockElement->IsHTMLElement(nsGkAtoms::hr)) {
      return blockElement;
    }
    if (!blockElement->GetParentElement()) {
      return nullptr;
    }
    return GetInclusiveAncestorBlockElementExceptHRElement(
        *blockElement->GetParentElement(), aAncestorLimiter);
  }

  /**
  /**
   * GetInclusiveAncestorEditableBlockElementOrInlineEditingHost() returns
   * GetInclusiveAncestorEditableBlockElementOrInlineEditingHost() returns
   * inclusive block ancestor element of aContent.  If aContent is in inline
   * inclusive block ancestor element of aContent.  If aContent is in inline
+5 −0
Original line number Original line Diff line number Diff line
@@ -2947,6 +2947,11 @@ class HTMLEditor final : public TextEditor,
            mInclusiveDescendantOfRightBlockElement(
            mInclusiveDescendantOfRightBlockElement(
                aInclusiveDescendantOfRightBlockElement) {}
                aInclusiveDescendantOfRightBlockElement) {}


      bool IsSet() const { return mLeftBlockElement && mRightBlockElement; }
      bool IsSameBlockElement() const {
        return mLeftBlockElement && mLeftBlockElement == mRightBlockElement;
      }

      /**
      /**
       * Prepare for joining inclusive ancestor block elements.  When this
       * Prepare for joining inclusive ancestor block elements.  When this
       * returns error or "canceled" or "handled" state, don't call Run().
       * returns error or "canceled" or "handled" state, don't call Run().