some refactor
This commit is contained in:
20 files changed
+1204
-948
No files matched your search
@@ -10,7 +10,7 @@
|
||||
#include "backend/formats/Fb2Types.h"
|
||||
#include "backend/formats/sources/Fb2ByteSource.h"
|
||||
#include "backend/formats/ImageUrl.h"
|
||||
#include "backend/reading/layout/DomChunkPipeline.h"
|
||||
#include "backend/reading/layout/CssParser.h"
|
||||
#include "backend/reading/layout/HtmlBlockParser.h"
|
||||
#include "backend/reading/layout/ChunkLayoutEngine.h"
|
||||
|
||||
@@ -367,7 +367,7 @@ private slots:
|
||||
// Obligation 7: contract-signature.SharedDomChunkPipeline.cascade_and_chunk
|
||||
void testContractSignatureSharedDomChunkPipeline()
|
||||
{
|
||||
// Verify cascade_and_chunk signature and invariant DeterministicChunking
|
||||
// Verify parseChunks deterministic chunking invariant
|
||||
QDomDocument doc1;
|
||||
doc1.setContent(QStringLiteral("<div><p class=\"lead\">Paragraph in EPUB spine</p></div>"));
|
||||
QDomElement root1 = doc1.documentElement();
|
||||
@@ -382,10 +382,11 @@ private slots:
|
||||
const QString sectionHref = QStringLiteral("sec1");
|
||||
const int targetChars = 512;
|
||||
|
||||
QVector<ContentChunk> chunks1 = DomChunkPipeline::cascadeAndChunk(
|
||||
root1, stylesheets, scheme, sourcePath, sectionHref, QString(), targetChars);
|
||||
QVector<ContentChunk> chunks2 = DomChunkPipeline::cascadeAndChunk(
|
||||
root2, stylesheets, scheme, sourcePath, sectionHref, QString(), targetChars);
|
||||
Css::StyleSheet sheet = Css::StyleSheet::parse(stylesheets.first());
|
||||
QVector<ContentChunk> chunks1 = HtmlBlockParser::parseChunks(
|
||||
root1, sectionHref, targetChars, {}, &sheet, scheme, sourcePath);
|
||||
QVector<ContentChunk> chunks2 = HtmlBlockParser::parseChunks(
|
||||
root2, sectionHref, targetChars, {}, &sheet, scheme, sourcePath);
|
||||
|
||||
QCOMPARE(chunks1.size(), chunks2.size());
|
||||
QVERIFY(!chunks1.isEmpty());
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
#include <QtTest>
|
||||
#include "backend/formats/TextEncoding.h"
|
||||
|
||||
class TextEncodingTest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void testEmpty();
|
||||
void testUtf8NoBom();
|
||||
void testUtf8WithBom();
|
||||
void testUtf16LeWithBom();
|
||||
void testUtf16BeWithBom();
|
||||
void testDetectXmlEncoding();
|
||||
void testDetectHtml5Charset();
|
||||
void testDetectHtml4Charset();
|
||||
void testDecodeWindows1251();
|
||||
void testDecodeWithHint();
|
||||
void testDecodeWithExplicitEncoding();
|
||||
};
|
||||
|
||||
void TextEncodingTest::testEmpty()
|
||||
{
|
||||
QCOMPARE(TextEncoding::detectEncoding(QByteArray()), QString());
|
||||
QCOMPARE(TextEncoding::decode(QByteArray()), QString());
|
||||
QCOMPARE(TextEncoding::decodeWithEncoding(QByteArray(), QStringLiteral("utf-8")), QString());
|
||||
}
|
||||
|
||||
void TextEncodingTest::testUtf8NoBom()
|
||||
{
|
||||
const QByteArray bytes = "Hello, world! \xF0\x9F\x93\x96"; // book emoji
|
||||
const QString result = TextEncoding::decode(bytes);
|
||||
QCOMPARE(result, QString::fromUtf8(bytes));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testUtf8WithBom()
|
||||
{
|
||||
const QByteArray bytes = "\xEF\xBB\xBFHello, BOM!";
|
||||
QCOMPARE(TextEncoding::detectEncoding(bytes), QStringLiteral("UTF-8"));
|
||||
const QString result = TextEncoding::decode(bytes);
|
||||
QCOMPARE(result, QStringLiteral("Hello, BOM!"));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testUtf16LeWithBom()
|
||||
{
|
||||
// UTF-16 LE BOM + "AB" (0x0041, 0x0042)
|
||||
const QByteArray bytes = QByteArray("\xFF\xFE\x41\x00\x42\x00", 6);
|
||||
QCOMPARE(TextEncoding::detectEncoding(bytes), QStringLiteral("UTF-16LE"));
|
||||
const QString result = TextEncoding::decode(bytes);
|
||||
QCOMPARE(result, QStringLiteral("AB"));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testUtf16BeWithBom()
|
||||
{
|
||||
// UTF-16 BE BOM + "AB" (0x0041, 0x0042)
|
||||
const QByteArray bytes = QByteArray("\xFE\xFF\x00\x41\x00\x42", 6);
|
||||
QCOMPARE(TextEncoding::detectEncoding(bytes), QStringLiteral("UTF-16BE"));
|
||||
const QString result = TextEncoding::decode(bytes);
|
||||
QCOMPARE(result, QStringLiteral("AB"));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testDetectXmlEncoding()
|
||||
{
|
||||
const QByteArray xml1 = "<?xml version=\"1.0\" encoding=\"windows-1251\"?>\n<FictionBook>";
|
||||
QCOMPARE(TextEncoding::detectEncoding(xml1), QStringLiteral("windows-1251"));
|
||||
|
||||
const QByteArray xml2 = "<?xml version='1.0' encoding='KOI8-R'?>\n<FictionBook>";
|
||||
QCOMPARE(TextEncoding::detectEncoding(xml2), QStringLiteral("KOI8-R"));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testDetectHtml5Charset()
|
||||
{
|
||||
const QByteArray html = "<html><head><meta charset=\"shift_jis\"></head></html>";
|
||||
QCOMPARE(TextEncoding::detectEncoding(html), QStringLiteral("shift_jis"));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testDetectHtml4Charset()
|
||||
{
|
||||
const QByteArray html = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gbk\"></head></html>";
|
||||
QCOMPARE(TextEncoding::detectEncoding(html), QStringLiteral("gbk"));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testDecodeWindows1251()
|
||||
{
|
||||
// "Привет" in windows-1251: CF F0 E8 E2 E5 F2
|
||||
const QByteArray win1251Text("\xCF\xF0\xE8\xE2\xE5\xF2");
|
||||
const QByteArray xml = "<?xml version=\"1.0\" encoding=\"windows-1251\"?>\n<title>" + win1251Text + "</title>";
|
||||
|
||||
const QString result = TextEncoding::decode(xml);
|
||||
QVERIFY(result.contains(QString::fromUtf8("Привет")));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testDecodeWithHint()
|
||||
{
|
||||
// "Привет" in windows-1251 without header
|
||||
const QByteArray win1251Text("\xCF\xF0\xE8\xE2\xE5\xF2");
|
||||
const QString result = TextEncoding::decode(win1251Text, QStringLiteral("windows-1251"));
|
||||
QCOMPARE(result, QString::fromUtf8("Привет"));
|
||||
}
|
||||
|
||||
void TextEncodingTest::testDecodeWithExplicitEncoding()
|
||||
{
|
||||
const QByteArray win1251Text("\xCF\xF0\xE8\xE2\xE5\xF2");
|
||||
const QString result = TextEncoding::decodeWithEncoding(win1251Text, QStringLiteral("windows-1251"));
|
||||
QCOMPARE(result, QString::fromUtf8("Привет"));
|
||||
|
||||
// Invalid encoding returns empty
|
||||
const QString invalid = TextEncoding::decodeWithEncoding(win1251Text, QStringLiteral("invalid_encoding_xyz"));
|
||||
QVERIFY(invalid.isEmpty());
|
||||
}
|
||||
|
||||
QTEST_MAIN(TextEncodingTest)
|
||||
#include "TextEncodingTest.moc"
|
||||
@@ -111,12 +111,19 @@ private slots:
|
||||
QDomElement p = doc.elementsByTagName(QStringLiteral("p")).at(0).toElement();
|
||||
|
||||
auto decls = Css::cascade(sheet, p);
|
||||
QString serialized = Css::serialize(decls);
|
||||
bool hasBold = false;
|
||||
bool hasRed = false;
|
||||
for (const auto &d : decls) {
|
||||
if (d.property == QLatin1String("font-weight") && d.value == QLatin1String("bold"))
|
||||
hasBold = true;
|
||||
if (d.property == QLatin1String("color") && d.value == QLatin1String("red"))
|
||||
hasRed = true;
|
||||
}
|
||||
|
||||
// font-weight: bold should match div > p.highlight
|
||||
QVERIFY(serialized.contains(QStringLiteral("font-weight:bold")));
|
||||
QVERIFY(hasBold);
|
||||
// color: red should win because it is !important in #special, beating inline style="color: green"
|
||||
QVERIFY(serialized.contains(QStringLiteral("color:red")));
|
||||
QVERIFY(hasRed);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user