From fbead1ebb52e4b3d917820dfdbf0840c2fdb0f83 Mon Sep 17 00:00:00 2001 From: Matthew Horan Date: Wed, 21 Nov 2012 23:20:47 -0500 Subject: [PATCH] Return unsupported content as plain text --- spec/driver_spec.rb | 25 +++++++++++++++++++++++++ src/UnsupportedContentHandler.cpp | 1 + src/WebPage.cpp | 10 ++++++++++ src/WebPage.h | 3 +++ src/body.cpp | 7 ++++++- 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb index 84d7bff..d86c679 100644 --- a/spec/driver_spec.rb +++ b/spec/driver_spec.rb @@ -257,6 +257,31 @@ describe Capybara::Webkit::Driver do it "sets the response headers with respect to the unsupported request" do driver.response_headers["Content-Type"].should == "text/css" end + + it "does not wrap the content in HTML tags" do + driver.html.should_not =~ // + end + end + + context "html app" do + let(:driver) do + driver_for_html(<<-HTML) + + + Hello HTML + + +

This Is HTML!

+ + + HTML + end + + before { visit("/") } + + it "does not strip HTML tags" do + driver.html.should =~ // + end end context "hello app" do diff --git a/src/UnsupportedContentHandler.cpp b/src/UnsupportedContentHandler.cpp index b6249a2..d34bf52 100644 --- a/src/UnsupportedContentHandler.cpp +++ b/src/UnsupportedContentHandler.cpp @@ -10,6 +10,7 @@ UnsupportedContentHandler::UnsupportedContentHandler(WebPage *page, QNetworkRepl void UnsupportedContentHandler::renderNonHtmlContent() { QByteArray text = m_reply->readAll(); m_page->mainFrame()->setContent(text, QString("text/plain"), m_reply->url()); + m_page->setUnsupportedContentLoaded(); m_page->networkAccessManagerFinishedReply(m_reply); m_page->loadFinished(true); this->deleteLater(); diff --git a/src/WebPage.cpp b/src/WebPage.cpp index 05c3f7d..efbbe55 100644 --- a/src/WebPage.cpp +++ b/src/WebPage.cpp @@ -14,6 +14,7 @@ WebPage::WebPage(WebPageManager *manager, QObject *parent) : QWebPage(parent) { m_failed = false; m_manager = manager; m_uuid = QUuid::createUuid().toString(); + m_unsupportedContentLoaded = false; setForwardUnsupportedContent(true); loadJavascript(); @@ -169,6 +170,7 @@ bool WebPage::javaScriptPrompt(QWebFrame *frame, const QString &message, const Q void WebPage::loadStarted() { m_loading = true; m_errorPageMessage = QString(); + m_unsupportedContentLoaded = false; } void WebPage::loadFinished(bool success) { @@ -275,6 +277,14 @@ void WebPage::handleUnsupportedContent(QNetworkReply *reply) { } } +void WebPage::setUnsupportedContentLoaded() { + m_unsupportedContentLoaded = true; +} + +bool WebPage::unsupportedContentLoaded() { + return m_unsupportedContentLoaded; +} + bool WebPage::supportsExtension(Extension extension) const { if (extension == ErrorPageExtension) return true; diff --git a/src/WebPage.h b/src/WebPage.h index 839a051..0719a6a 100644 --- a/src/WebPage.h +++ b/src/WebPage.h @@ -34,6 +34,8 @@ class WebPage : public QWebPage { bool matchesWindowSelector(QString); void setFocus(); NetworkAccessManager *networkAccessManager(); + void setUnsupportedContentLoaded(); + bool unsupportedContentLoaded(); public slots: bool shouldInterruptJavaScript(); @@ -79,6 +81,7 @@ class WebPage : public QWebPage { QString m_uuid; WebPageManager *m_manager; QString m_errorPageMessage; + bool m_unsupportedContentLoaded; }; #endif //_WEBPAGE_H diff --git a/src/body.cpp b/src/body.cpp index 0e16734..ed90855 100644 --- a/src/body.cpp +++ b/src/body.cpp @@ -6,6 +6,11 @@ Body::Body(WebPageManager *manager, QStringList &arguments, QObject *parent) : S } void Body::start() { - QString result = page()->currentFrame()->toHtml(); + QString result; + if (page()->unsupportedContentLoaded()) + result = page()->currentFrame()->toPlainText(); + else + result = page()->currentFrame()->toHtml(); + emit finished(new Response(true, result)); }