1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

[AdBlock] Fixed hiding rules when more than 5000 rules in subscription.

Closes #873
This commit is contained in:
nowrep 2013-05-02 21:01:17 +02:00
parent bef40d2ca6
commit 1b617068b2
6 changed files with 38 additions and 12 deletions

View File

@ -348,11 +348,6 @@ QString AdBlockManager::elementHidingRules() const
rules.append(subscription->elementHidingRules()); rules.append(subscription->elementHidingRules());
} }
// Remove last ","
if (!rules.isEmpty()) {
rules = rules.left(rules.size() - 1);
}
return rules; return rules;
} }

View File

@ -377,7 +377,6 @@ void AdBlockRule::parseFilter()
} }
m_cssSelector = parsedLine.mid(pos + 2); m_cssSelector = parsedLine.mid(pos + 2);
m_cssSelector.remove('\\');
// CSS rule cannot have more options -> stop parsing // CSS rule cannot have more options -> stop parsing
return; return;

View File

@ -258,12 +258,28 @@ QString AdBlockSubscription::elementHidingRulesForDomain(const QString &domain)
{ {
QString rules; QString rules;
int addedRulesCount = 0;
int count = m_domainRestrictedCssRules.count(); int count = m_domainRestrictedCssRules.count();
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
const AdBlockRule* rule = m_domainRestrictedCssRules.at(i); const AdBlockRule* rule = m_domainRestrictedCssRules.at(i);
if (rule->matchDomain(domain)) { if (!rule->matchDomain(domain)) {
rules.append(rule->cssSelector() + QLatin1Char(',')); continue;
} }
if (Q_UNLIKELY(addedRulesCount == 1000)) {
rules.append(rule->cssSelector());
rules.append("{display:none !important;}\n");
addedRulesCount = 0;
}
else {
rules.append(rule->cssSelector() + QLatin1Char(','));
addedRulesCount++;
}
}
if (addedRulesCount != 0) {
rules = rules.left(rules.size() - 1);
rules.append("{display:none !important;}\n");
} }
return rules; return rules;
@ -357,6 +373,11 @@ void AdBlockSubscription::populateCache()
m_documentRules.clear(); m_documentRules.clear();
m_elemhideRules.clear(); m_elemhideRules.clear();
// Apparently, excessive amount of selectors for one CSS rule is not what WebKit likes.
// (In my testings, 4931 is the number that makes it crash)
// So let's split it by 1000 selectors...
int hidingRulesCount = 0;
int count = m_rules.count(); int count = m_rules.count();
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
const AdBlockRule* rule = m_rules.at(i); const AdBlockRule* rule = m_rules.at(i);
@ -368,8 +389,14 @@ void AdBlockSubscription::populateCache()
if (rule->isDomainRestricted()) { if (rule->isDomainRestricted()) {
m_domainRestrictedCssRules.append(rule); m_domainRestrictedCssRules.append(rule);
} }
else if (Q_UNLIKELY(hidingRulesCount == 1000)) {
m_elementHidingRules.append(rule->cssSelector());
m_elementHidingRules.append("{display:none !important;} ");
hidingRulesCount = 0;
}
else { else {
m_elementHidingRules.append(rule->cssSelector() + ","); m_elementHidingRules.append(rule->cssSelector() + QLatin1Char(','));
hidingRulesCount++;
} }
} }
else if (rule->isDocument()) { else if (rule->isDocument()) {
@ -385,6 +412,11 @@ void AdBlockSubscription::populateCache()
m_networkBlockRules.append(rule); m_networkBlockRules.append(rule);
} }
} }
if (hidingRulesCount != 0) {
m_elementHidingRules = m_elementHidingRules.left(m_elementHidingRules.size() - 1);
m_elementHidingRules.append("{display:none !important;} ");
}
} }
AdBlockSubscription::~AdBlockSubscription() AdBlockSubscription::~AdBlockSubscription()

View File

@ -1016,7 +1016,7 @@ QUrl MainApplication::userStyleSheet(const QString &filePath) const
userStyle += QString("::selection {background: %1; color: %2;} ").arg(highlightColor, highlightedTextColor); userStyle += QString("::selection {background: %1; color: %2;} ").arg(highlightColor, highlightedTextColor);
#endif #endif
userStyle += AdBlockManager::instance()->elementHidingRules() + "{ display:none !important;}"; userStyle += AdBlockManager::instance()->elementHidingRules();
QFile file(filePath); QFile file(filePath);
if (!filePath.isEmpty() && file.open(QFile::ReadOnly)) { if (!filePath.isEmpty() && file.open(QFile::ReadOnly)) {

View File

@ -152,7 +152,7 @@ void SourceViewer::loadSource()
QString html = m_frame.data()->toHtml(); QString html = m_frame.data()->toHtml();
// Remove AdBlock element hiding rules // Remove AdBlock element hiding rules
html.remove(QzRegExp("<style type=\"text/css\">\n/\\* AdBlock for QupZilla \\*/\n.*\\{display: none !important;\\}\n</style>")); html.remove(QzRegExp("<style type=\"text/css\">\n/\\* AdBlock for QupZilla \\*/\n.*\\{display:none !important;\\}\n</style>"));
m_sourceEdit->setPlainText(html); m_sourceEdit->setPlainText(html);
// Highlight selectedHtml // Highlight selectedHtml

View File

@ -639,7 +639,7 @@ void WebPage::cleanBlockedObjects()
return; return;
} }
elementHiding.append(QLatin1String("{display: none !important;}\n</style>")); elementHiding.append(QLatin1String("\n</style>"));
QWebElement bodyElement = docElement.findFirst("body"); QWebElement bodyElement = docElement.findFirst("body");
bodyElement.appendInside("<style type=\"text/css\">\n/* AdBlock for QupZilla */\n" + elementHiding); bodyElement.appendInside("<style type=\"text/css\">\n/* AdBlock for QupZilla */\n" + elementHiding);