#include #include #include "backend/reading/layout/CssParser.h" class CssParserTest : public QObject { Q_OBJECT private slots: void parseLengthUnits() { Css::LengthContext ctx; ctx.em = 20.0; ctx.rem = 16.0; ctx.percentBase = 500.0; ctx.absoluteScale = 1.0; auto l1 = Css::parseLength(QStringLiteral("2em"), ctx); QVERIFY(l1.has_value()); QCOMPARE(*l1, 40.0); auto l2 = Css::parseLength(QStringLiteral("1.5rem"), ctx); QVERIFY(l2.has_value()); QCOMPARE(*l2, 24.0); auto l3 = Css::parseLength(QStringLiteral("50%"), ctx); QVERIFY(l3.has_value()); QCOMPARE(*l3, 250.0); auto l4 = Css::parseLength(QStringLiteral("10px"), ctx); QVERIFY(l4.has_value()); QCOMPARE(*l4, 10.0); auto l5 = Css::parseLength(QStringLiteral("0"), ctx); QVERIFY(l5.has_value()); QCOMPARE(*l5, 0.0); } void parseColors() { QColor c1 = Css::parseColor(QStringLiteral("#ff0000")); QCOMPARE(c1, QColor(255, 0, 0)); QColor c2 = Css::parseColor(QStringLiteral("rgb(0, 128, 255)")); QCOMPARE(c2, QColor(0, 128, 255)); QColor c3 = Css::parseColor(QStringLiteral("rgba(0, 0, 0, 0.5)")); QCOMPARE(c3.alpha(), 128); // 0.5 * 255 rounded = 128 QColor c4 = Css::parseColor(QStringLiteral("transparent")); QCOMPARE(c4, QColor(Qt::transparent)); QColor c5 = Css::parseColor(QStringLiteral("red")); QCOMPARE(c5, QColor(Qt::red)); } void stylesheetParsingAndCascade() { const QString css = QStringLiteral( "p { color: black; font-size: 16px; }\n" ".highlight { color: yellow; }\n" "#special { color: red !important; }\n" "div > p.highlight { font-weight: bold; }\n" "@font-face { font-family: 'MyFont'; src: url('myfont.otf'); }\n" ); auto sheet = Css::StyleSheet::parse(css); QCOMPARE(sheet.fontFaces.size(), 1); QCOMPARE(sheet.fontFaces[0].family, QStringLiteral("MyFont")); QCOMPARE(sheet.fontFaces[0].src, QStringLiteral("myfont.otf")); QDomDocument doc; doc.setContent(QStringLiteral("

Hello

")); QDomElement p = doc.elementsByTagName(QStringLiteral("p")).at(0).toElement(); auto decls = Css::cascade(sheet, p); QString serialized = Css::serialize(decls); // font-weight: bold should match div > p.highlight QVERIFY(serialized.contains(QStringLiteral("font-weight:bold"))); // color: red should win because it is !important in #special, beating inline style="color: green" QVERIFY(serialized.contains(QStringLiteral("color:red"))); } }; QTEST_MAIN(CssParserTest) #include "CssParserTest.moc"