closes #472: Element.insertAdjacentText Element.insertAdjacentElement

This commit is contained in:
Cameron Kaiser 2018-02-11 13:21:45 -08:00
parent 91b9713028
commit 46b6faa6b2
3 changed files with 75 additions and 0 deletions

View File

@ -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()
{

View File

@ -708,6 +708,26 @@ public:
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;

View File

@ -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,