From 8dd16259287f58f9273002717ec4d27e97127719 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 12 Jun 2024 07:43:14 +0200 Subject: Merging upstream version 127.0. Signed-off-by: Daniel Baumann --- editor/libeditor/HTMLEditor.cpp | 105 +++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 55 deletions(-) (limited to 'editor/libeditor/HTMLEditor.cpp') diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp index fc88c79477..11d021e70b 100644 --- a/editor/libeditor/HTMLEditor.cpp +++ b/editor/libeditor/HTMLEditor.cpp @@ -46,6 +46,7 @@ #include "mozilla/css/Loader.h" #include "mozilla/dom/AncestorIterator.h" #include "mozilla/dom/Attr.h" +#include "mozilla/dom/BorrowedAttrInfo.h" #include "mozilla/dom/DocumentFragment.h" #include "mozilla/dom/DocumentInlines.h" #include "mozilla/dom/Element.h" @@ -63,7 +64,6 @@ #include "nsContentUtils.h" #include "nsCRT.h" #include "nsDebug.h" -#include "nsDOMAttributeMap.h" #include "nsElementTable.h" #include "nsFocusManager.h" #include "nsGenericHTMLElement.h" @@ -215,27 +215,23 @@ HTMLEditor::AppendNewElementWithBRToInsertingElement( } HTMLEditor::AttributeFilter HTMLEditor::CopyAllAttributes = - [](HTMLEditor&, const Element&, const Element&, const Attr&, nsString&) { - return true; - }; + [](HTMLEditor&, const Element&, const Element&, int32_t, const nsAtom&, + nsString&) { return true; }; HTMLEditor::AttributeFilter HTMLEditor::CopyAllAttributesExceptId = - [](HTMLEditor&, const Element&, const Element&, const Attr& aAttr, - nsString&) { - return aAttr.NodeInfo()->NamespaceID() != kNameSpaceID_None || - aAttr.NodeInfo()->NameAtom() != nsGkAtoms::id; + [](HTMLEditor&, const Element&, const Element&, int32_t aNamespaceID, + const nsAtom& aAttrName, nsString&) { + return aNamespaceID != kNameSpaceID_None || &aAttrName != nsGkAtoms::id; }; HTMLEditor::AttributeFilter HTMLEditor::CopyAllAttributesExceptDir = - [](HTMLEditor&, const Element&, const Element&, const Attr& aAttr, - nsString&) { - return aAttr.NodeInfo()->NamespaceID() != kNameSpaceID_None || - aAttr.NodeInfo()->NameAtom() != nsGkAtoms::dir; + [](HTMLEditor&, const Element&, const Element&, int32_t aNamespaceID, + const nsAtom& aAttrName, nsString&) { + return aNamespaceID != kNameSpaceID_None || &aAttrName != nsGkAtoms::dir; }; HTMLEditor::AttributeFilter HTMLEditor::CopyAllAttributesExceptIdAndDir = - [](HTMLEditor&, const Element&, const Element&, const Attr& aAttr, - nsString&) { - return !(aAttr.NodeInfo()->NamespaceID() == kNameSpaceID_None && - (aAttr.NodeInfo()->NameAtom() == nsGkAtoms::id || - aAttr.NodeInfo()->NameAtom() == nsGkAtoms::dir)); + [](HTMLEditor&, const Element&, const Element&, int32_t aNamespaceID, + const nsAtom& aAttrName, nsString&) { + return !(aNamespaceID == kNameSpaceID_None && + (&aAttrName == nsGkAtoms::id || &aAttrName == nsGkAtoms::dir)); }; HTMLEditor::HTMLEditor(const Document& aDocument) @@ -1150,15 +1146,15 @@ nsresult HTMLEditor::MaybeCollapseSelectionAtFirstEditableNode( if (Text* text = leafContent->GetAsText()) { // If there is editable and visible text node, move caret at first of // the visible character. - WSScanResult scanResultInTextNode = - WSRunScanner::ScanNextVisibleNodeOrBlockBoundary( + const WSScanResult scanResultInTextNode = + WSRunScanner::ScanInclusiveNextVisibleNodeOrBlockBoundary( editingHost, EditorRawDOMPoint(text, 0), BlockInlineCheck::UseComputedDisplayStyle); if ((scanResultInTextNode.InVisibleOrCollapsibleCharacters() || scanResultInTextNode.ReachedPreformattedLineBreak()) && scanResultInTextNode.TextPtr() == text) { nsresult rv = CollapseSelectionTo( - scanResultInTextNode.Point()); + scanResultInTextNode.PointAtReachedContent()); NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "EditorBase::CollapseSelectionTo() failed"); return rv; @@ -3603,29 +3599,35 @@ Result HTMLEditor::CreateAndInsertElement( nsresult HTMLEditor::CopyAttributes(WithTransaction aWithTransaction, Element& aDestElement, Element& aSrcElement, const AttributeFilter& aFilterFunc) { - RefPtr srcAttributes = aSrcElement.Attributes(); - if (!srcAttributes->Length()) { + if (!aSrcElement.GetAttrCount()) { return NS_OK; } - AutoTArray, 16> srcAttrs; - srcAttrs.SetCapacity(srcAttributes->Length()); - for (uint32_t i = 0; i < srcAttributes->Length(); i++) { - RefPtr attr = srcAttributes->Item(i); - if (!attr) { - break; + struct MOZ_STACK_CLASS AttrCache { + int32_t mNamespaceID; + OwningNonNull mName; + nsString mValue; + }; + AutoTArray srcAttrs; + srcAttrs.SetCapacity(aSrcElement.GetAttrCount()); + for (const uint32_t i : IntegerRange(aSrcElement.GetAttrCount())) { + const BorrowedAttrInfo attrInfo = aSrcElement.GetAttrInfoAt(i); + if (const nsAttrName* attrName = attrInfo.mName) { + MOZ_ASSERT(attrName->LocalName()); + MOZ_ASSERT(attrInfo.mValue); + nsString attrValue; + attrInfo.mValue->ToString(attrValue); + srcAttrs.AppendElement(AttrCache{attrInfo.mName->NamespaceID(), + *attrName->LocalName(), attrValue}); } - srcAttrs.AppendElement(std::move(attr)); } if (aWithTransaction == WithTransaction::No) { - for (const OwningNonNull& attr : srcAttrs) { - nsString value; - attr->GetValue(value); - if (!aFilterFunc(*this, aSrcElement, aDestElement, attr, value)) { + for (auto& attr : srcAttrs) { + if (!aFilterFunc(*this, aSrcElement, aDestElement, attr.mNamespaceID, + attr.mName, attr.mValue)) { continue; } - DebugOnly rvIgnored = - aDestElement.SetAttr(attr->NodeInfo()->NamespaceID(), - attr->NodeInfo()->NameAtom(), value, false); + DebugOnly rvIgnored = aDestElement.SetAttr( + attr.mNamespaceID, attr.mName, attr.mValue, false); NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored), "Element::SetAttr() failed, but ignored"); } @@ -3787,31 +3789,24 @@ nsresult HTMLEditor::InsertLinkAroundSelectionAsAction( *this, ScrollSelectionIntoView::Yes, __FUNCTION__); // Set all attributes found on the supplied anchor element - RefPtr attributeMap = anchor->Attributes(); - if (NS_WARN_IF(!attributeMap)) { - return NS_ERROR_FAILURE; - } - // TODO: We should stop using this loop for adding attributes to newly created // `` elements. Then, we can avoid to increate the ref- // counter of attribute names since we can use nsStaticAtom if we don't // need to support unknown attributes. AutoTArray stylesToSet; - stylesToSet.SetCapacity(attributeMap->Length()); - nsString value; - for (uint32_t i : IntegerRange(attributeMap->Length())) { - RefPtr attribute = attributeMap->Item(i); - if (!attribute) { - continue; + if (const uint32_t attrCount = anchor->GetAttrCount()) { + stylesToSet.SetCapacity(attrCount); + for (const uint32_t i : IntegerRange(attrCount)) { + const BorrowedAttrInfo attrInfo = anchor->GetAttrInfoAt(i); + if (const nsAttrName* attrName = attrInfo.mName) { + RefPtr attributeName = attrName->LocalName(); + MOZ_ASSERT(attrInfo.mValue); + nsString attrValue; + attrInfo.mValue->ToString(attrValue); + stylesToSet.AppendElement(EditorInlineStyleAndValue( + *nsGkAtoms::a, std::move(attributeName), std::move(attrValue))); + } } - - RefPtr attributeName = attribute->NodeInfo()->NameAtom(); - - MOZ_ASSERT(value.IsEmpty()); - attribute->GetValue(value); - - stylesToSet.AppendElement(EditorInlineStyleAndValue( - *nsGkAtoms::a, std::move(attributeName), std::move(value))); } rv = SetInlinePropertiesAsSubAction(stylesToSet); NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), -- cgit v1.2.3