Skip to content

Commit

Permalink
closes #472: Element.insertAdjacentText Element.insertAdjacentElement
Browse files Browse the repository at this point in the history
  • Loading branch information
classilla committed Feb 11, 2018
1 parent 91b9713 commit 46b6faa
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
49 changes: 49 additions & 0 deletions dom/base/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3556,6 +3556,55 @@ Element::InsertAdjacentHTML(const nsAString& aPosition, const nsAString& aText,
}
}

nsINode*
Element::InsertAdjacent(const nsAString& aWhere,
nsINode* aNode,
ErrorResult& aError)
{
if (aWhere.LowerCaseEqualsLiteral("beforebegin")) {
nsCOMPtr<nsINode> parent = GetParentNode();
if (!parent) {
return nullptr;
}
parent->InsertBefore(*aNode, this, aError);
} else if (aWhere.LowerCaseEqualsLiteral("afterbegin")) {
nsCOMPtr<nsINode> refChild = GetFirstChild();
static_cast<nsINode*>(this)->InsertBefore(*aNode, refChild, aError);
} else if (aWhere.LowerCaseEqualsLiteral("beforeend")) {
static_cast<nsINode*>(this)->AppendChild(*aNode, aError);
} else if (aWhere.LowerCaseEqualsLiteral("afterend")) {
nsCOMPtr<nsINode> parent = GetParentNode();
if (!parent) {
return nullptr;
}
nsCOMPtr<nsINode> refChild = GetNextSibling();
parent->InsertBefore(*aNode, refChild, aError);
} else {
aError.Throw(NS_ERROR_DOM_SYNTAX_ERR);
return nullptr;
}

return aError.Failed() ? nullptr : aNode;
}

Element*
Element::InsertAdjacentElement(const nsAString& aWhere,
Element& aElement,
ErrorResult& aError) {
nsINode* newNode = InsertAdjacent(aWhere, &aElement, aError);
MOZ_ASSERT(!newNode || newNode->IsElement());

return newNode ? newNode->AsElement() : nullptr;
}

void
Element::InsertAdjacentText(
const nsAString& aWhere, const nsAString& aData, ErrorResult& aError)
{
RefPtr<nsTextNode> textNode = OwnerDoc()->CreateTextNode(aData);
InsertAdjacent(aWhere, textNode, aError);
}

nsIEditor*
Element::GetEditorInternal()
{
Expand Down
20 changes: 20 additions & 0 deletions dom/base/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,26 @@ class Element : public FragmentOrElement
ErrorResult& aError);
already_AddRefed<nsIHTMLCollection>
GetElementsByClassName(const nsAString& aClassNames);

private:
/**
* Implement the algorithm specified at
* https://dom.spec.whatwg.org/#insert-adjacent for both
* |insertAdjacentElement()| and |insertAdjacentText()| APIs.
*/
nsINode* InsertAdjacent(const nsAString& aWhere,
nsINode* aNode,
ErrorResult& aError);

public:
Element* InsertAdjacentElement(const nsAString& aWhere,
Element& aElement,
ErrorResult& aError);

void InsertAdjacentText(const nsAString& aWhere,
const nsAString& aData,
ErrorResult& aError);

void SetPointerCapture(int32_t aPointerId, ErrorResult& aError)
{
bool activeState = false;
Expand Down
6 changes: 6 additions & 0 deletions dom/webidl/Element.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ interface Element : Node {
[Pure]
HTMLCollection getElementsByClassName(DOMString classNames);

[Throws, Pure]
Element? insertAdjacentElement(DOMString where, Element element); // historical

[Throws]
void insertAdjacentText(DOMString where, DOMString data); // historical

/**
* The ratio of font-size-inflated text font size to computed font
* size for this element. This will query the element for its primary frame,
Expand Down

0 comments on commit 46b6faa

Please sign in to comment.