Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Backed out 2 changesets (bug 1484092) for build bustages in builds/wo…
Browse files Browse the repository at this point in the history
…rker/workspace/build/src/editor/libeditor/HTMLEditor.cpp:100:53 on a CLOSED TREE

Backed out changeset 10fdd041f1b5 (bug 1484092)
Backed out changeset d0b14e8711df (bug 1484092)
  • Loading branch information
nerli1 committed Aug 17, 2018
1 parent 7e5e216 commit 5a456dc
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 388 deletions.
7 changes: 2 additions & 5 deletions editor/libeditor/HTMLAnonymousNodeEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,7 @@ HTMLEditor::DeleteRefToAnonymousNode(ManualNACPtr aContent,
NS_IMETHODIMP
HTMLEditor::CheckSelectionStateForAnonymousButtons(Selection* aSelection)
{
if (NS_WARN_IF(!aSelection)) {
return NS_ERROR_INVALID_ARG;
}
NS_ENSURE_ARG_POINTER(aSelection);

// early way out if all contextual UI extensions are disabled
NS_ENSURE_TRUE(mIsObjectResizingEnabled ||
Expand Down Expand Up @@ -328,8 +326,7 @@ HTMLEditor::CheckSelectionStateForAnonymousButtons(Selection* aSelection)
if (mIsObjectResizingEnabled || mIsInlineTableEditingEnabled) {
// Resizing or Inline Table Editing is enabled, we need to check if the
// selection is contained in a table cell
cellElement =
GetElementOrParentByTagNameAtSelection(*aSelection, *nsGkAtoms::td);
cellElement = GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nullptr);
}

if (mIsObjectResizingEnabled && cellElement) {
Expand Down
193 changes: 89 additions & 104 deletions editor/libeditor/HTMLEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,29 @@ GetLowerCaseNameAtom(const nsAString& aTagName)
}

// Some utilities to handle overloading of "A" tag for link and named anchor.
static bool
IsLinkTag(const nsString& s)
{
return s.EqualsIgnoreCase("href");
}

static bool
IsLinkTag(const nsAtom& aTagName)
{
return aTagName.Equals(NS_LITERAL_STRING("href"));
}

static bool
IsNamedAnchorTag(const nsString& s)
{
return s.EqualsIgnoreCase("anchor") || s.EqualsIgnoreCase("namedanchor");
}

static bool
IsNamedAnchorTag(const nsAtom& aTagName)
{
return aTagName.Equals(NS_LITERAL_STRING("anchor");
return aTagName.Equals(NS_LITERAL_STRING("anchor")) ||
aTagName.Equals(NS_LITERAL_STRING("namedanchor"));
}

template EditorDOMPoint
Expand Down Expand Up @@ -1093,38 +1106,24 @@ HTMLEditor::TabInTable(bool inIsShift,
NS_ENSURE_TRUE(outHandled, NS_ERROR_NULL_POINTER);
*outHandled = false;

RefPtr<Selection> selection = GetSelection();
if (NS_WARN_IF(!selection)) {
// Do nothing if we didn't find a table cell.
return NS_OK;
}

// Find enclosing table cell from selection (cell may be selected element)
Element* cellElement =
GetElementOrParentByTagNameAtSelection(*selection, *nsGkAtoms::td);
if (NS_WARN_IF(!cellElement)) {
// Do nothing if we didn't find a table cell.
return NS_OK;
}
nsCOMPtr<Element> cellElement =
GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nullptr);
// Do nothing -- we didn't find a table cell
NS_ENSURE_TRUE(cellElement, NS_OK);

// find enclosing table
RefPtr<Element> table = GetEnclosingTable(cellElement);
if (NS_WARN_IF(!table)) {
return NS_OK;
}
nsCOMPtr<Element> table = GetEnclosingTable(cellElement);
NS_ENSURE_TRUE(table, NS_OK);

// advance to next cell
// first create an iterator over the table
nsCOMPtr<nsIContentIterator> iter = NS_NewContentIterator();
nsresult rv = iter->Init(table);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
NS_ENSURE_SUCCESS(rv, rv);
// position iter at block
rv = iter->PositionAt(cellElement);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
NS_ENSURE_SUCCESS(rv, rv);

nsCOMPtr<nsINode> node;
do {
Expand Down Expand Up @@ -2528,93 +2527,84 @@ HTMLEditor::Align(const nsAString& aAlignType)
return rules->DidDoAction(selection, subActionInfo, rv);
}

Element*
HTMLEditor::GetElementOrParentByTagName(const nsAtom& aTagName,
already_AddRefed<Element>
HTMLEditor::GetElementOrParentByTagName(const nsAString& aTagName,
nsINode* aNode)
{
MOZ_ASSERT(&aTagName != nsGkAtoms::_empty);
MOZ_ASSERT(!aTagName.IsEmpty());

if (aNode) {
return GetElementOrParentByTagNameInternal(aTagName, *aNode);
}
RefPtr<Selection> selection = GetSelection();
if (NS_WARN_IF(!selection)) {
return nullptr;
}
return GetElementOrParentByTagNameAtSelection(*selection, aTagName);
}

Element*
HTMLEditor::GetElementOrParentByTagNameAtSelection(Selection& aSelection,
const nsAtom& aTagName)
{
MOZ_ASSERT(&aTagName != nsGkAtoms::_empty);

// If no node supplied, get it from anchor node of current selection
const EditorRawDOMPoint atAnchor(aSelection.AnchorRef());
if (NS_WARN_IF(!atAnchor.IsSet())) {
return nullptr;
}

// Try to get the actual selected node
nsCOMPtr<nsINode> node;
if (atAnchor.GetContainer()->HasChildNodes() &&
atAnchor.GetContainerAsContent()) {
node = atAnchor.GetChild();
}
// Anchor node is probably a text node - just use that
nsCOMPtr<nsINode> node = aNode;
if (!node) {
node = atAnchor.GetContainer();
if (NS_WARN_IF(!node)) {
// If no node supplied, get it from anchor node of current selection
RefPtr<Selection> selection = GetSelection();
if (NS_WARN_IF(!selection)) {
return nullptr;
}
}
return GetElementOrParentByTagNameInternal(aTagName, *node);
}

Element*
HTMLEditor::GetElementOrParentByTagNameInternal(const nsAtom& aTagName,
nsINode& aNode)
{
MOZ_ASSERT(&aTagName != nsGkAtoms::_empty);
const EditorDOMPoint atAnchor(selection->AnchorRef());
if (NS_WARN_IF(!atAnchor.IsSet())) {
return nullptr;
}

// Try to get the actual selected node
if (atAnchor.GetContainer()->HasChildNodes() &&
atAnchor.GetContainerAsContent()) {
node = atAnchor.GetChild();
}
// Anchor node is probably a text node - just use that
if (!node) {
node = atAnchor.GetContainer();
}
}

Element* currentElement =
aNode.IsElement() ? aNode.AsElement() : aNode.GetParentElement();
if (NS_WARN_IF(!currentElement)) {
nsCOMPtr<Element> current;
if (node->IsElement()) {
current = node->AsElement();
} else if (node->GetParentElement()) {
current = node->GetParentElement();
} else {
// Neither aNode nor its parent is an element, so no ancestor is
MOZ_ASSERT(!aNode.GetParentNode() ||
!aNode.GetParentNode()->GetParentNode());
MOZ_ASSERT(!node->GetParentNode() ||
!node->GetParentNode()->GetParentNode());
return nullptr;
}

bool getLink = IsLinkTag(aTagName);
bool getNamedAnchor = IsNamedAnchorTag(aTagName);
const nsAtom& tagName = getLink || getNamedAnchor ? *nsGkAtoms::a : aTagName;
for (; currentElement; currentElement = currentElement->GetParentElement()) {
nsAutoString tagName(aTagName);
ToLowerCase(tagName);
bool getLink = IsLinkTag(tagName);
bool getNamedAnchor = IsNamedAnchorTag(tagName);
if (getLink || getNamedAnchor) {
tagName.Assign('a');
}
bool findTableCell = tagName.EqualsLiteral("td");
bool findList = tagName.EqualsLiteral("list");

for (; current; current = current->GetParentElement()) {
// Test if we have a link (an anchor with href set)
if ((getLink && HTMLEditUtils::IsLink(currentElement)) ||
(getNamedAnchor && HTMLEditUtils::IsNamedAnchor(currentElement))) {
return currentElement;
if ((getLink && HTMLEditUtils::IsLink(current)) ||
(getNamedAnchor && HTMLEditUtils::IsNamedAnchor(current))) {
return current.forget();
}
if (&tagName == nsGkAtoms::list_) {
if (findList) {
// Match "ol", "ul", or "dl" for lists
if (HTMLEditUtils::IsList(currentElement)) {
return currentElement;
if (HTMLEditUtils::IsList(current)) {
return current.forget();
}
} else if (&tagName == nsGkAtoms::td) {
} else if (findTableCell) {
// Table cells are another special case: match either "td" or "th"
if (HTMLEditUtils::IsTableCell(currentElement)) {
return currentElement;
if (HTMLEditUtils::IsTableCell(current)) {
return current.forget();
}
} else if (&tagName == currentElement->NodeInfo()->NameAtom()) {
return currentElement;
} else if (current->NodeName().Equals(tagName,
nsCaseInsensitiveStringComparator())) {
return current.forget();
}

// Stop searching if parent is a body tag. Note: Originally used IsRoot to
// stop at table cells, but that's too messy when you are trying to find
// the parent table
if (currentElement->GetParentElement() &&
currentElement->GetParentElement()->IsHTMLElement(nsGkAtoms::body)) {
if (current->GetParentElement() &&
current->GetParentElement()->IsHTMLElement(nsGkAtoms::body)) {
break;
}
}
Expand All @@ -2627,20 +2617,15 @@ HTMLEditor::GetElementOrParentByTagName(const nsAString& aTagName,
nsINode* aNode,
Element** aReturn)
{
if (NS_WARN_IF(aTagName.IsEmpty()) ||
NS_WARN_IF(!aReturn)) {
return NS_ERROR_INVALID_ARG;
}
NS_ENSURE_TRUE(!aTagName.IsEmpty(), NS_ERROR_NULL_POINTER);
NS_ENSURE_TRUE(aReturn, NS_ERROR_NULL_POINTER);

RefPtr<nsAtom> tagName = GetLowerCaseNameAtom(aTagName);
if (NS_WARN_IF(!tagName) || NS_WARN_IF(tagName == nsGkAtoms::_empty)) {
return NS_ERROR_INVALID_ARG;
}
RefPtr<Element> parent = GetElementOrParentByTagName(aTagName, aNode);

RefPtr<Element> parent = GetElementOrParentByTagName(*tagName, aNode);
if (!parent) {
return NS_SUCCESS_EDITOR_ELEMENT_NOT_FOUND;
}

parent.forget(aReturn);
return NS_OK;
}
Expand Down Expand Up @@ -2722,22 +2707,22 @@ HTMLEditor::GetSelectedElement(Selection& aSelection,
const RangeBoundary& focus = aSelection.FocusRef();
// Link node must be the same for both ends of selection
if (anchor.IsSet()) {
Element* parentLinkOfAnchor =
GetElementOrParentByTagNameInternal(*nsGkAtoms::href,
*anchor.Container());
RefPtr<Element> parentLinkOfAnchor =
GetElementOrParentByTagName(NS_LITERAL_STRING("href"),
anchor.Container());
// XXX: ERROR_HANDLING can parentLinkOfAnchor be null?
if (parentLinkOfAnchor) {
if (aSelection.IsCollapsed()) {
// We have just a caret in the link.
return do_AddRef(parentLinkOfAnchor);
return parentLinkOfAnchor.forget();
}
if (focus.IsSet()) {
// Link node must be the same for both ends of selection.
Element* parentLinkOfFocus =
GetElementOrParentByTagNameInternal(*nsGkAtoms::href,
*focus.Container());
RefPtr<Element> parentLinkOfFocus =
GetElementOrParentByTagName(NS_LITERAL_STRING("href"),
focus.Container());
if (parentLinkOfFocus == parentLinkOfAnchor) {
return do_AddRef(parentLinkOfAnchor);
return parentLinkOfAnchor.forget();
}
}
} else if (anchor.GetChildAtOffset() && focus.GetChildAtOffset()) {
Expand Down
65 changes: 2 additions & 63 deletions editor/libeditor/HTMLEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,27 +322,8 @@ class HTMLEditor final : public TextEditor
*/
nsresult DoInlineTableEditingAction(const Element& aUIAnonymousElement);

/**
* GetElementOrParentByTagName() looks for an element node whose name matches
* aTagName from aNode or anchor node of Selection to <body> element.
*
* @param aTagName The tag name which you want to look for.
* Must not be nsGkAtoms::_empty.
* If nsGkAtoms::list, the result may be <ul>, <ol> or
* <dl> element.
* If nsGkAtoms::td, the result may be <td> or <th>.
* If nsGkAtoms::href, the result may be <a> element
* which has "href" attribute with non-empty value.
* If nsGkAtoms::anchor, the result may be <a> which
* has "name" attribute with non-empty value.
* @param aNode If non-nullptr, this starts to look for the result
* from it. Otherwise, i.e., nullptr, starts from
* anchor node of Selection.
* @return If an element which matches aTagName, returns
* an Element. Otherwise, nullptr.
*/
Element*
GetElementOrParentByTagName(const nsAtom& aTagName, nsINode* aNode);
already_AddRefed<Element>
GetElementOrParentByTagName(const nsAString& aTagName, nsINode* aNode);

/**
* Get an active editor's editing host in DOM window. If this editor isn't
Expand Down Expand Up @@ -833,48 +814,6 @@ class HTMLEditor final : public TextEditor
nsresult CollapseSelectionAfter(Selection& aSelection,
Element& aElement);

/**
* GetElementOrParentByTagNameAtSelection() looks for an element node whose
* name matches aTagName from anchor node of Selection to <body> element.
*
* @param aSelection The Selection for this editor.
* @param aTagName The tag name which you want to look for.
* Must not be nsGkAtoms::_empty.
* If nsGkAtoms::list, the result may be <ul>, <ol> or
* <dl> element.
* If nsGkAtoms::td, the result may be <td> or <th>.
* If nsGkAtoms::href, the result may be <a> element
* which has "href" attribute with non-empty value.
* If nsGkAtoms::anchor, the result may be <a> which
* has "name" attribute with non-empty value.
* @return If an element which matches aTagName, returns
* an Element. Otherwise, nullptr.
*/
Element*
GetElementOrParentByTagNameAtSelection(Selection& aSelection,
const nsAtom& aTagName);

/**
* GetElementOrParentByTagNameInternal() looks for an element node whose
* name matches aTagName from aNode to <body> element.
*
* @param aTagName The tag name which you want to look for.
* Must not be nsGkAtoms::_empty.
* If nsGkAtoms::list, the result may be <ul>, <ol> or
* <dl> element.
* If nsGkAtoms::td, the result may be <td> or <th>.
* If nsGkAtoms::href, the result may be <a> element
* which has "href" attribute with non-empty value.
* If nsGkAtoms::anchor, the result may be <a> which
* has "name" attribute with non-empty value.
* @param aNode Start node to look for the element.
* @return If an element which matches aTagName, returns
* an Element. Otherwise, nullptr.
*/
Element*
GetElementOrParentByTagNameInternal(const nsAtom& aTagName,
nsINode& aNode);

/**
* GetSelectedElement() returns an element node which is in first range of
* aSelection. The rule is a little bit complicated and the rules do not
Expand Down
5 changes: 3 additions & 2 deletions editor/libeditor/HTMLEditorEventListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ HTMLEditorEventListener::MouseDown(MouseEvent* aMouseEvent)
selection->Collapse(parent, offset);
} else {
// Get enclosing link if in text so we can select the link
Element* linkElement =
htmlEditor->GetElementOrParentByTagName(*nsGkAtoms::href, node);
RefPtr<Element> linkElement =
htmlEditor->GetElementOrParentByTagName(NS_LITERAL_STRING("href"),
node);
if (linkElement) {
element = linkElement;
}
Expand Down
Loading

0 comments on commit 5a456dc

Please sign in to comment.