Restore the URL parsing behaviour in from before the KF5 port

This fixes shelltest

REVIEW: 124738
This commit is contained in:
Alex Richardson 2015-08-14 16:54:33 +01:00
parent c3decc55f7
commit ab700b2453
3 changed files with 43 additions and 14 deletions

View file

@ -5,7 +5,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR}/..)
ecm_add_test(shelltest.cpp ../shell/shellutils.cpp
TEST_NAME "shelltest"
LINK_LIBRARIES Qt5::Test okularcore KF5::KDELibs4Support
LINK_LIBRARIES Qt5::Test okularcore
)
ecm_add_test(parttest.cpp

View file

@ -9,18 +9,17 @@
#include <QtTest>
#include <qdir.h>
#include <kcmdlineargs.h>
#include <kurl.h>
#include <QDir>
#include <QUrl>
#include "../shell/shellutils.h"
static const QUrl makeUrlFromCwd( const QString& u, const QString& ref = QString() )
{
QUrl url( QUrl::fromLocalFile( QDir::currentPath() + '/' + u ));
QUrl url = QUrl::fromLocalFile( QDir::currentPath() + '/' + u) ;
if ( !ref.isEmpty() )
url.setFragment( ref );
url.setPath(QDir::cleanPath(url.path()));
url.setPath(QDir::cleanPath( url.path() ) );
return url;
}
@ -48,8 +47,6 @@ class ShellTest
void ShellTest::initTestCase()
{
qRegisterMetaType<QUrl>();
KCmdLineArgs::setCwd( QDir::currentPath().toLocal8Bit() );
}
void ShellTest::testUrlArgs_data()
@ -84,19 +81,32 @@ void ShellTest::testUrlArgs_data()
QTest::newRow( "http://kde.org/foo.pdf" )
<< "http://kde.org/foo.pdf"
<< true
<< makeUrlFromCwd( "http://kde.org/foo.pdf" );
<< QUrl( "http://kde.org/foo.pdf" );
// make sure we don't have a fragment
QUrl hashInName( "http://kde.org" );
QVERIFY( hashInName.path().isEmpty() );
hashInName.setPath( "/foo#bar.pdf" );
QVERIFY( hashInName.fragment().isEmpty() );
QTest::newRow( "http://kde.org/foo#bar.pdf" )
<< "http://kde.org/foo#bar.pdf"
<< true
<< makeUrlFromCwd( "http://kde.org/foo#bar.pdf" );
<< hashInName;
QUrl withAnchor( "http://kde.org/foo.pdf" );
withAnchor.setFragment( "anchor" );
QTest::newRow( "http://kde.org/foo.pdf#anchor" )
<< "http://kde.org/foo.pdf#anchor"
<< true
<< makeUrlFromCwd( "http://kde.org/foo.pdf", "anchor" );
<< withAnchor;
QTest::newRow( "#207461" )
<< "http://homepages.inf.ed.ac.uk/mef/file%20with%20spaces.pdf"
<< true
<< QUrl( "http://homepages.inf.ed.ac.uk/mef/file%20with%20spaces.pdf" );
QUrl openOnPage3 = QUrl( "http://itzsimpl.info/lectures/CG/L2-transformations.pdf" );
openOnPage3.setFragment( "3" );
QTest::newRow( "RR124738" )
<< "http://itzsimpl.info/lectures/CG/L2-transformations.pdf#3"
<< true
<< openOnPage3;
}
void ShellTest::testUrlArgs()
@ -104,7 +114,7 @@ void ShellTest::testUrlArgs()
QFETCH( QString, arg );
QFETCH( bool, exists );
QFETCH( QUrl, resUrl );
qDebug() << "Expected url:" << resUrl << "path =" << resUrl.path() << "fragment =" << resUrl.fragment();
QUrl url = ShellUtils::urlFromArg( arg, exists ? fileExist_always_Func : fileExist_never_Func );
QCOMPARE( url, resUrl );
}

View file

@ -39,7 +39,7 @@ QUrl urlFromArg( const QString& _arg, FileExistFunc exist_func, const QString& p
{
// ## TODO remove exist_func
#if QT_VERSION >= 0x050400
QUrl url = QUrl::fromUserInput(_arg, QDir::currentPath());
QUrl url = QUrl::fromUserInput(_arg, QDir::currentPath(), QUrl::AssumeLocalFile);
#else
// Code from QUrl::fromUserInput(QString, QString)
QUrl url = QUrl::fromUserInput(_arg);
@ -49,8 +49,27 @@ QUrl urlFromArg( const QString& _arg, FileExistFunc exist_func, const QString& p
if (fileInfo.exists())
url = QUrl::fromLocalFile(fileInfo.absoluteFilePath());
}
#endif
if ( url.isLocalFile() ) {
// make sure something like /tmp/foo#bar.pdf is treated as a path name (default)
// but something like /tmp/foo.pdf#bar is foo.pdf plus an anchor "bar"
const QString path = url.path();
int hashIndex = path.lastIndexOf( QLatin1Char ( '#' ) );
int lastDotIndex = path.lastIndexOf( QLatin1Char ( '.' ) );
// make sure that we don't change the path if .pdf comes after the #
if ( hashIndex != -1 && hashIndex > lastDotIndex) {
url.setPath( path.left( hashIndex ) );
url.setFragment( path.mid( hashIndex + 1 ) );
qDebug() << "Added fragment to url:" << url.path() << url.fragment();
}
} else if ( !url.fragment().isEmpty() ) {
// make sure something like http://example.org/foo#bar.pdf is treated as a path name
// but something like http://example.org/foo.pdf#bar is foo.pdf plus an anchor "bar"
if ( url.fragment().contains( QLatin1Char( '.' ) ) ) {
url.setPath( url.path() + '#' + url.fragment() );
url.setFragment( QString() );
}
}
if ( !pageArg.isEmpty() )
{
url.setFragment( pageArg );