Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Commit

Permalink
Parse href in findLinks with QUrl::fromPercentEncoding
Browse files Browse the repository at this point in the history
This allows for both internal and external links containing percent escaped characters to be parsed correctly.
Previously, http://example.com/foo%26bar was parsed as http://example.com/foo%2526bar instead of
http://example.com/foo%26bar (http://example.com/foo&bar). Same issue for other escaped characters.

According to the docs for the QUrl constructor, in it's default parsing using TolerantMode, it'll only
transform %20 back to space, and will treat all other % signs as raw symbols to be encoded.
  • Loading branch information
daveallie committed Nov 22, 2022
1 parent 024b2b2 commit 7b6eaad
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/lib/pdfconverter.cc
Expand Up @@ -566,7 +566,7 @@ void PdfConverterPrivate::findLinks(QWebFrame * frame, QVector<QPair<QWebElement
if (h.startsWith("__WKANCHOR_")) {
local.push_back( qMakePair(elm, h) );
} else {
QUrl href(h);
QUrl href = QUrl::fromEncoded(h.toLocal8Bit());
if (href.isEmpty()) continue;
href=frame->baseUrl().resolved(href);
QString key = QUrl::fromPercentEncoding(href.toString(QUrl::RemoveFragment).toLocal8Bit());
Expand All @@ -589,7 +589,7 @@ void PdfConverterPrivate::findLinks(QWebFrame * frame, QVector<QPair<QWebElement
}
}
} else if (uexternal) {
external.push_back( qMakePair(elm, settings.resolveRelativeLinks ? href.toString() : h) );
external.push_back( qMakePair(elm, settings.resolveRelativeLinks ? QString::fromLocal8Bit(href.toEncoded()) : h) );
}
}
}
Expand Down Expand Up @@ -680,7 +680,7 @@ void PdfConverterPrivate::endPage(PageObject & object, bool hasHeaderFooter, int
}
foreach (const p_t & p, external) {
QRectF r = wp.elementLocation(p.first).second;
painter->addHyperlink(r, QUrl(p.second));
painter->addHyperlink(r, QUrl::fromEncoded(p.second.toLocal8Bit()));
}
wp.spoolPage(1);
// restore margins
Expand Down Expand Up @@ -713,7 +713,7 @@ void PdfConverterPrivate::endPage(PageObject & object, bool hasHeaderFooter, int
}
foreach (const p_t & p, external) {
QRectF r = wp.elementLocation(p.first).second;
painter->addHyperlink(r, QUrl(p.second));
painter->addHyperlink(r, QUrl::fromEncoded(p.second.toLocal8Bit()));
}
wp.spoolPage(1);
// restore margins
Expand Down Expand Up @@ -871,7 +871,7 @@ void PdfConverterPrivate::spoolPage(int page) {
for (QVector< QPair<QWebElement,QString> >::iterator i=pageExternalLinks[page+1].begin();
i != pageExternalLinks[page+1].end(); ++i) {
QRectF r = webPrinter->elementLocation(i->first).second;
painter->addHyperlink(r, QUrl(i->second));
painter->addHyperlink(r, QUrl::fromEncoded(i->second.toLocal8Bit()));
}
endPage(objects[currentObject], pageHasHeaderFooter, page, pageNumber);
actualPage++;
Expand Down

0 comments on commit 7b6eaad

Please sign in to comment.