[Qt] QWebElement::encloseWith doesn't work at all

https://bugs.webkit.org/show_bug.cgi?id=62464

Enclosing an element means wrapping something around an element,
not inserting something into an element. Therefore we need to check
if the parent of the element allows insertion rather than checking
the element itself.

Patch by Zeno Albisser <zeno.albisser@nokia.com> on 2011-09-09
Reviewed by Andreas Kling.

* Api/qwebelement.cpp:
(QWebElement::prependOutside):
(QWebElement::appendOutside):
(QWebElement::encloseWith):
* tests/qwebelement/tst_qwebelement.cpp:
(tst_QWebElement::appendAndPrepend):
(tst_QWebElement::encloseWith):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@94852 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 02fc51bf
......@@ -1111,16 +1111,17 @@ void QWebElement::prependOutside(const QString &markup)
if (!m_element)
return;
if (!m_element->parentNode())
Node* parent = m_element->parentNode();
if (!parent)
return;
if (!m_element->isHTMLElement())
if (!parent->isHTMLElement())
return;
RefPtr<DocumentFragment> fragment = Range::createDocumentFragmentForElement(markup, toHTMLElement(m_element));
RefPtr<DocumentFragment> fragment = Range::createDocumentFragmentForElement(markup, toHTMLElement(parent));
ExceptionCode exception = 0;
m_element->parentNode()->insertBefore(fragment, m_element, exception);
parent->insertBefore(fragment, m_element, exception);
}
/*!
......@@ -1160,19 +1161,20 @@ void QWebElement::appendOutside(const QString &markup)
if (!m_element)
return;
if (!m_element->parentNode())
Node* parent = m_element->parentNode();
if (!parent)
return;
if (!m_element->isHTMLElement())
if (!parent->isHTMLElement())
return;
RefPtr<DocumentFragment> fragment = Range::createDocumentFragmentForElement(markup, toHTMLElement(m_element));
RefPtr<DocumentFragment> fragment = Range::createDocumentFragmentForElement(markup, toHTMLElement(parent));
ExceptionCode exception = 0;
if (!m_element->nextSibling())
m_element->parentNode()->appendChild(fragment, exception);
parent->appendChild(fragment, exception);
else
m_element->parentNode()->insertBefore(fragment, m_element->nextSibling(), exception);
parent->insertBefore(fragment, m_element->nextSibling(), exception);
}
/*!
......@@ -1380,13 +1382,14 @@ void QWebElement::encloseWith(const QString &markup)
if (!m_element)
return;
if (!m_element->parentNode())
Node* parent = m_element->parentNode();
if (!parent)
return;
if (!m_element->isHTMLElement())
if (!parent->isHTMLElement())
return;
RefPtr<DocumentFragment> fragment = Range::createDocumentFragmentForElement(markup, toHTMLElement(m_element));
RefPtr<DocumentFragment> fragment = Range::createDocumentFragmentForElement(markup, toHTMLElement(parent));
if (!fragment || !fragment->firstChild())
return;
......@@ -1396,11 +1399,10 @@ void QWebElement::encloseWith(const QString &markup)
if (!insertionPoint)
return;
// Keep reference to these two nodes before pulling out this element and
// Keep reference to parent & siblingNode before pulling out this element and
// wrapping it in the fragment. The reason for doing it in this order is
// that once the fragment has been added to the document it is empty, so
// we no longer have access to the nodes it contained.
Node* parent = m_element->parentNode();
Node* siblingNode = m_element->nextSibling();
ExceptionCode exception = 0;
......
2011-09-09 Zeno Albisser <zeno.albisser@nokia.com>
[Qt] QWebElement::encloseWith doesn't work at all
https://bugs.webkit.org/show_bug.cgi?id=62464
Enclosing an element means wrapping something around an element,
not inserting something into an element. Therefore we need to check
if the parent of the element allows insertion rather than checking
the element itself.
Reviewed by Andreas Kling.
* Api/qwebelement.cpp:
(QWebElement::prependOutside):
(QWebElement::appendOutside):
(QWebElement::encloseWith):
* tests/qwebelement/tst_qwebelement.cpp:
(tst_QWebElement::appendAndPrepend):
(tst_QWebElement::encloseWith):
2011-09-08 Sam Weinig <sam@webkit.org>
Remove the Completion object from JSC, I have never liked it
......
......@@ -646,6 +646,26 @@ void tst_QWebElement::appendAndPrepend()
body.findFirst("div").prependInside("<code>yepp</code>");
QCOMPARE(body.findAll("p div code").count(), 1);
QCOMPARE(body.findFirst("p div code").toPlainText(), QString("yepp"));
// Inserting HTML into an img tag is not allowed, but appending/prepending outside is.
body.findFirst("div").appendInside("<img src=\"test.png\">");
QCOMPARE(body.findAll("p div img").count(), 1);
QWebElement img = body.findFirst("img");
QVERIFY(!img.isNull());
img.appendInside("<p id=\"fail1\"></p>");
QCOMPARE(body.findAll("p#fail1").count(), 0);
img.appendOutside("<p id=\"success1\"></p>");
QCOMPARE(body.findAll("p#success1").count(), 1);
img.prependInside("<p id=\"fail2\"></p>");
QCOMPARE(body.findAll("p#fail2").count(), 0);
img.prependOutside("<p id=\"success2\"></p>");
QCOMPARE(body.findAll("p#success2").count(), 1);
}
void tst_QWebElement::insertBeforeAndAfter()
......@@ -881,6 +901,19 @@ void tst_QWebElement::encloseWith()
body.findFirst("em").encloseWith(snippet);
QCOMPARE(body.findFirst("table tbody tr td em").toPlainText(), QString("hey"));
// Enclosing the contents of an img tag is not allowed, but enclosing the img tag itself is.
body.findFirst("td").appendInside("<img src=\"test.png\">");
QCOMPARE(body.findAll("img").count(), 1);
QWebElement img = body.findFirst("img");
QVERIFY(!img.isNull());
img.encloseWith("<p id=\"success\"></p>");
QCOMPARE(body.findAll("p#success").count(), 1);
img.encloseContentsWith("<p id=\"fail\"></p>");
QCOMPARE(body.findAll("p#fail").count(), 0);
}
void tst_QWebElement::nullSelect()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment