1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

GreaseMonkey: Use better metadata parser

Use QTextStream instead of parsing with regexps
This commit is contained in:
David Rosca 2017-04-19 16:27:23 +02:00
parent efc4725e91
commit 0c4f20ff2d

View File

@ -24,6 +24,7 @@
#include "mainapplication.h" #include "mainapplication.h"
#include <QFile> #include <QFile>
#include <QTextStream>
#include <QStringList> #include <QStringList>
#include <QWebEngineScript> #include <QWebEngineScript>
#include <QCryptographicHash> #include <QCryptographicHash>
@ -195,21 +196,23 @@ void GM_Script::parseScript()
m_fileWatcher->addPath(m_fileName); m_fileWatcher->addPath(m_fileName);
} }
const QString fileData = QString::fromUtf8(file.readAll()); const QByteArray fileData = file.readAll();
QzRegExp rx(QSL("(?:^|[\\r\\n])// ==UserScript==(.*)(?:\\r\\n|[\\r\\n])// ==/UserScript==(?:[\\r\\n]|$)")); bool inMetadata = false;
rx.indexIn(fileData);
QString metadataBlock = rx.cap(1).trimmed();
if (metadataBlock.isEmpty()) { QTextStream stream(fileData);
qWarning() << "GreaseMonkey: File does not contain metadata block" << m_fileName; QString line;
return; while (stream.readLineInto(&line)) {
} if (line.startsWith(QL1S("// ==UserScript=="))) {
inMetadata = true;
}
if (line.startsWith(QL1S("// ==/UserScript=="))) {
break;
}
if (!inMetadata) {
continue;
}
QzRegExp rxNL(QSL("(?:\\r\\n|[\\r\\n])"));
const QStringList lines = metadataBlock.split(rxNL, QString::SkipEmptyParts);
foreach (QString line, lines) {
if (!line.startsWith(QLatin1String("// @"))) { if (!line.startsWith(QLatin1String("// @"))) {
continue; continue;
} }
@ -268,6 +271,11 @@ void GM_Script::parseScript()
} }
} }
if (!inMetadata) {
qWarning() << "GreaseMonkey: File does not contain metadata block" << m_fileName;
return;
}
if (m_include.isEmpty()) { if (m_include.isEmpty()) {
m_include.append(QSL("*")); m_include.append(QSL("*"));
} }