Commit ac121e1c0858ef6a290732c2d13284cb33c63e25

  • avatar
  • Jakub Wieczorek <faw217 @gm…l.com>
  • Tue Jun 30 21:19:43 CEST 2009
Fix a design failure in the WebPage::linkedResources() function. It
should return absolute URLs so that the code using it doesn't have to
resolve the relative URLs on its own.
  
287287
288288void tst_WebPage::linkedResources()
289289{
290 QLatin1String html("<html>"
291 " <head>"
292 " <link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\"/>"
293 " <link rel=\"alternate\" type=\"application/rss+xml\" href=\"rss1.xml\"/>"
294 " <link rel=\"alternate\" type=\"application/rss+xml\" href=\"rss2.xml\" title=\"Lorem Ipsum\"/>"
295 " <link rel=\"alternate\" type=\"application/atom+xml\" href=\"atom1.xml\"/>"
296 " </head>"
297 " <body>"
298 " <link rel=\"alternate\" type=\"application/atom+xml\" href=\"atom2.xml\"/>"
299 " </body>"
300 "</html>");
301
302290 SubWebPage page;
303 page.mainFrame()->setHtml(QString(html));
304291
305 QList<WebPageLinkedResource> linkedResources = page.linkedResources();
306 QCOMPARE(linkedResources.count(), 4);
292 QString html = "<html>"
293 "<head>"
294 "<link rel=\"stylesheet\" type=\"text/css\" href=\"styles/common.css\" />"
295 "<link rel=\"alternate\" type=\"application/rss+xml\" href=\"./rss.xml\" />"
296 "<link rel=\"alternate\" type=\"application/atom+xml\" href=\"../atom.xml\" title=\"Feed\" />"
297 "<link rel=\"search\" type=\"application/opensearchdescription+xml\" href=\"http://external.foo/search.xml\" />"
298 "</head>"
299 "<body>"
300 "<link rel=\"stylesheet\" type=\"text/css\" href=\"styles/ie.css\" />"
301 "</body>"
302 "</html>";
307303
308 QCOMPARE(linkedResources.at(0).rel, QLatin1String("stylesheet"));
309 QCOMPARE(linkedResources.at(0).type, QLatin1String("text/css"));
310 QCOMPARE(linkedResources.at(0).href, QLatin1String("styles.css"));
311 QCOMPARE(linkedResources.at(0).title, QString());
304 page.mainFrame()->setHtml(html, QUrl("http://foobar.baz/foo/"));
312305
313 QCOMPARE(linkedResources.at(1).rel, QLatin1String("alternate"));
314 QCOMPARE(linkedResources.at(1).type, QLatin1String("application/rss+xml"));
315 QCOMPARE(linkedResources.at(1).href, QLatin1String("rss1.xml"));
306 QList<WebPageLinkedResource> resources = page.linkedResources();
307 QCOMPARE(resources.count(), 4);
316308
317 QCOMPARE(linkedResources.at(2).title, QLatin1String("Lorem Ipsum"));
309 QCOMPARE(resources.at(0).rel, QString("stylesheet"));
310 QCOMPARE(resources.at(0).type, QString("text/css"));
311 QCOMPARE(resources.at(0).href, QUrl("http://foobar.baz/foo/styles/common.css"));
312 QCOMPARE(resources.at(0).title, QString());
318313
319 QCOMPARE(linkedResources.at(3).type, QLatin1String("application/atom+xml"));
314 QCOMPARE(resources.at(1).rel, QString("alternate"));
315 QCOMPARE(resources.at(1).type, QString("application/rss+xml"));
316 QCOMPARE(resources.at(1).href, QUrl("http://foobar.baz/foo/rss.xml"));
320317
321 QList<WebPageLinkedResource> linkedFeeds = page.linkedResources(QLatin1String("alternate"));
322 QCOMPARE(linkedFeeds.count(), 3);
318 QCOMPARE(resources.at(2).href, QUrl("http://foobar.baz/atom.xml"));
319 QCOMPARE(resources.at(2).title, QString("Feed"));
323320
324 QCOMPARE(linkedFeeds.at(2).rel, QLatin1String("alternate"));
325 QCOMPARE(linkedFeeds.at(2).href, QLatin1String("atom1.xml"));
321 QCOMPARE(resources.at(3).rel, QString("search"));
322 QCOMPARE(resources.at(3).type, QString("application/opensearchdescription+xml"));
323 QCOMPARE(resources.at(3).href, QUrl("http://external.foo/search.xml"));
324
325 QString js = "var base = document.createElement('base');"
326 "base.setAttribute('href', 'http://barbaz.foo/bar/');"
327 "document.getElementsByTagName('head')[0].appendChild(base);";
328
329 page.mainFrame()->evaluateJavaScript(js);
330
331 resources = page.linkedResources();
332 QCOMPARE(resources.count(), 4);
333
334 QCOMPARE(resources.at(0).href, QUrl("http://barbaz.foo/bar/styles/common.css"));
335 QCOMPARE(resources.at(1).href, QUrl("http://barbaz.foo/bar/rss.xml"));
336 QCOMPARE(resources.at(2).href, QUrl("http://barbaz.foo/atom.xml"));
337 QCOMPARE(resources.at(3).href, QUrl("http://external.foo/search.xml"));
326338}
327339
328340QTEST_MAIN(tst_WebPage)
  
307307 for (int i = 0; i < engines.count(); ++i) {
308308 WebPageLinkedResource engine = engines.at(i);
309309
310 QUrl url = QUrl(engine.href);
310 QUrl url = engine.href;
311311 QString title = engine.title;
312312 QString mimetype = engine.type;
313313
315315 continue;
316316 if (url.isEmpty())
317317 continue;
318
319 if (url.isRelative())
320 url = webView->url().resolved(url);
321318
322319 if (title.isEmpty())
323320 title = webView->title().isEmpty() ? url.host() : webView->title();
  
8181 QList<WebPageLinkedResource> resources;
8282
8383#if QT_VERSION >= 0x040600 || defined(WEBKIT_TRUNK)
84 QUrl baseUrl = mainFrame()->baseUrl();
85
8486 QList<QWebElement> linkElements = mainFrame()->findAllElements(QLatin1String("html > head > link"));
8587
8688 foreach (const QWebElement &linkElement, linkElements) {
9999 WebPageLinkedResource resource;
100100 resource.rel = rel;
101101 resource.type = type;
102 resource.href = href;
102 resource.href = baseUrl.resolved(QUrl::fromEncoded(href.toUtf8()));
103103 resource.title = title;
104104
105105 resources.append(resource);
106106 }
107107#else
108 QString baseUrlString = mainFrame()->evaluateJavaScript(QLatin1String("document.baseURI")).toString();
109 QUrl baseUrl = QUrl::fromEncoded(baseUrlString.toUtf8());
110
108111 QFile file(QLatin1String(":fetchLinks.js"));
109112 if (!file.open(QFile::ReadOnly))
110113 return resources;
129129 WebPageLinkedResource resource;
130130 resource.rel = rel;
131131 resource.type = type;
132 resource.href = href;
132 resource.href = baseUrl.resolved(QUrl::fromEncoded(href.toUtf8()));
133133 resource.title = title;
134134
135135 resources.append(resource);
  
3030public:
3131 QString rel;
3232 QString type;
33 QString href;
33 QUrl href;
3434 QString title;
3535};
3636