mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-05 09:33:25 -08:00
Qt's SVG renderer has no CSS cascade, so a `currentColor` fill has to be substituted before the bytes reach QSvgRenderer. - Match and replace on bytes: both the sentinel and the replacement are ASCII, so the document never goes through a UTF-16 round trip, and files that are not valid UTF-8 survive unchanged. - Document that the match is a plain case-insensitive substring, so an identifier that merely starts with the sentinel gets rewritten too. Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
66 lines
1.9 KiB
C++
66 lines
1.9 KiB
C++
#include "libcockatrice/utility/svg_recolor.h"
|
|
|
|
#include <QByteArray>
|
|
#include <gtest/gtest.h>
|
|
|
|
namespace
|
|
{
|
|
|
|
TEST(SvgRecolor, ReplacesEverySentinel)
|
|
{
|
|
const QByteArray svg = R"(<svg><path fill="currentColor"/><path stroke="currentColor"/></svg>)";
|
|
const QByteArray result = resolveSvgColorSentinel(svg, "#ff8800");
|
|
|
|
EXPECT_EQ(result.count("#ff8800"), 2);
|
|
EXPECT_FALSE(result.contains("currentColor"));
|
|
}
|
|
|
|
TEST(SvgRecolor, ReplacesSentinelRegardlessOfCase)
|
|
{
|
|
const QByteArray svg = R"(<svg><path fill="CurrentColor"/><path stroke="CURRENTCOLOR"/></svg>)";
|
|
|
|
EXPECT_EQ(resolveSvgColorSentinel(svg, "#ff8800").count("#ff8800"), 2);
|
|
}
|
|
|
|
TEST(SvgRecolor, LeavesTheRestOfTheDocumentIntact)
|
|
{
|
|
const QByteArray svg =
|
|
R"(<svg viewBox="0 0 24 24"><!-- an ordinary comment --><path d="M0 0h24v24" fill="currentColor"/></svg>)";
|
|
const QByteArray result = resolveSvgColorSentinel(svg, "#123456");
|
|
|
|
EXPECT_TRUE(result.contains(R"(viewBox="0 0 24 24")"));
|
|
EXPECT_TRUE(result.contains(R"(d="M0 0h24v24")"));
|
|
EXPECT_TRUE(result.contains("an ordinary comment"));
|
|
}
|
|
|
|
TEST(SvgRecolor, DoesNotModifyTheInput)
|
|
{
|
|
const QByteArray svg = R"(<svg><path fill="currentColor"/></svg>)";
|
|
const QByteArray original = svg;
|
|
|
|
resolveSvgColorSentinel(svg, "#000000");
|
|
|
|
EXPECT_EQ(svg, original);
|
|
}
|
|
|
|
TEST(SvgRecolor, PreservesBytesThatAreNotValidUtf8)
|
|
{
|
|
QByteArray svg = "<svg>";
|
|
svg.append("\xff\xfe");
|
|
svg.append(R"(<path fill="currentColor"/></svg>)");
|
|
|
|
const QByteArray result = resolveSvgColorSentinel(svg, "#ff8800");
|
|
|
|
EXPECT_TRUE(result.contains("\xff\xfe"));
|
|
EXPECT_FALSE(result.contains("currentColor"));
|
|
EXPECT_EQ(result.count("#ff8800"), 1);
|
|
}
|
|
|
|
TEST(SvgRecolor, NoSentinelIsANoOp)
|
|
{
|
|
const QByteArray svg = R"(<svg><path fill="#abcdef"/></svg>)";
|
|
|
|
EXPECT_EQ(resolveSvgColorSentinel(svg, "#ffffff"), svg);
|
|
}
|
|
|
|
} // namespace
|