From d8a9bec35fb0c60a0e5990c1a12ffe6f39fdbf2d Mon Sep 17 00:00:00 2001 From: Nils Jeisecke Date: Thu, 17 Dec 2015 16:38:15 +0100 Subject: QTextDocument: add css-styling of table cell borders to HTML import/export Supported style attributes: style: supports "border-collapse: collapse" and "border-color". border: width of the outer border bordercolor: basic color for all borders style: not supported
/ style: supports the "border", "border-[top|left|bottom|right]]" shorthand styles and the "border-width", "border-color" and "border-style" (and the top/left/bottom/right variants) attributes will render a simple 1px table grid. Notes: The QTextDocument table model is much simpler than the HTML table model. It basically only has
and and
support. So the HTML parser is forced to map markup and styling to the QTextDocument model which is not without loss. In other words: While QTextDocument -> HTML -> QTextDocument should preserve the QTextDocument structure, HTML -> QTextDocument -> HTML does not preserve the HTML DOM at all. So for now the HTML importer and writer only support border styles on the and nodes. In future updates, the HTML parser might be enhanced to map
CSS styles to the cells. Change-Id: If9e7312fa6cbf270cf8f7b3c72ba1fa094107517 Reviewed-by: Shawn Rutledge --- .../gui/text/qtextdocument/tst_qtextdocument.cpp | 29 +++++ .../tst_qtextdocumentfragment.cpp | 134 +++++++++++++++++++++ tests/auto/gui/text/qtexttable/tst_qtexttable.cpp | 108 +++++++++++++++++ 3 files changed, 271 insertions(+) (limited to 'tests') diff --git a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp index 58810f73c1..52e56feb5a 100644 --- a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp +++ b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp @@ -50,6 +50,7 @@ #include #include "common.h" +// #define DEBUG_WRITE_OUTPUT QT_FORWARD_DECLARE_CLASS(QTextDocument) @@ -196,6 +197,7 @@ private: void backgroundImage_checkExpectedHtml(const QTextDocument &doc); void buildRegExpData(); static QString cssFontSizeString(const QFont &font); + void writeActualAndExpected(const char* testTag, const QString &actual, const QString &expected); QTextDocument *doc; QTextCursor cursor; @@ -224,6 +226,27 @@ QString tst_QTextDocument::cssFontSizeString(const QFont &font) : QString::number(font.pixelSize()) + QStringLiteral("px"); } +void tst_QTextDocument::writeActualAndExpected(const char *testTag, const QString &actual, const QString &expected) +{ +#ifdef DEBUG_WRITE_OUTPUT + { + QFile out(QDir::temp().absoluteFilePath(QLatin1String(testTag) + QLatin1String("-actual.html"))); + out.open(QFile::WriteOnly); + out.write(actual.toUtf8()); + out.close(); + } { + QFile out(QDir::temp().absoluteFilePath(QLatin1String(testTag) + QLatin1String("-expected.html"))); + out.open(QFile::WriteOnly); + out.write(expected.toUtf8()); + out.close(); + } +#else + Q_UNUSED(testTag) + Q_UNUSED(actual) + Q_UNUSED(expected) +#endif +} + // Testing get/set functions void tst_QTextDocument::getSetCheck() { @@ -1765,6 +1788,8 @@ void tst_QTextDocument::toHtml() QString output = doc->toHtml(); + writeActualAndExpected(QTest::currentDataTag(), output, expectedOutput); + QCOMPARE(output, expectedOutput); QDomDocument document; @@ -1962,6 +1987,8 @@ void tst_QTextDocument::toHtmlRootFrameProperties() expectedOutput.replace("DEFAULTBLOCKSTYLE", "style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\""); expectedOutput.append(htmlTail); + writeActualAndExpected(QTest::currentTestFunction(), doc.toHtml(), expectedOutput); + QCOMPARE(doc.toHtml(), expectedOutput); } @@ -2734,6 +2761,8 @@ void tst_QTextDocument::backgroundImage_checkExpectedHtml(const QTextDocument &d .arg(defaultFont.weight() * 8) .arg((defaultFont.italic() ? "italic" : "normal")); + writeActualAndExpected(QTest::currentTestFunction(), doc.toHtml(), expectedHtml); + QCOMPARE(doc.toHtml(), expectedHtml); } diff --git a/tests/auto/gui/text/qtextdocumentfragment/tst_qtextdocumentfragment.cpp b/tests/auto/gui/text/qtextdocumentfragment/tst_qtextdocumentfragment.cpp index c5243d1535..b6917f1208 100644 --- a/tests/auto/gui/text/qtextdocumentfragment/tst_qtextdocumentfragment.cpp +++ b/tests/auto/gui/text/qtextdocumentfragment/tst_qtextdocumentfragment.cpp @@ -181,6 +181,11 @@ private slots: void html_tableCellBackground(); void css_bodyBackground(); void css_tableCellBackground(); + void css_tableCellBorder(); + void css_tableCellBorderShorthand(); + void css_tableCellAllBordersShorthand(); + void css_tableCellOverrideOneBorder(); + void css_tableBorderCollapse(); void css_fontWeight(); void css_float(); void css_textIndent(); @@ -1753,6 +1758,135 @@ void tst_QTextDocumentFragment::css_tableCellBackground() QCOMPARE(cell.format().background().style(), Qt::TexturePattern); } +void tst_QTextDocumentFragment::css_tableCellBorder() +{ + const char html[] = "
Foo
"; + doc->setHtml(html); + + cursor.movePosition(QTextCursor::Start); + cursor.movePosition(QTextCursor::NextBlock); + QTextTable *table = cursor.currentTable(); + QVERIFY(table); + + QTextTableCell cell = table->cellAt(0, 0); + QTextTableCellFormat cellFormat = cell.format().toTableCellFormat(); + QCOMPARE(cellFormat.leftBorder(), qreal(4)); + QCOMPARE(cellFormat.leftBorderBrush(), QBrush(QColor("red"))); + QCOMPARE(cellFormat.leftBorderStyle(), QTextFrameFormat::BorderStyle_Dashed); + + QCOMPARE(cellFormat.rightBorder(), qreal(8)); + QCOMPARE(cellFormat.rightBorderBrush(), QBrush(QColor("green"))); + QCOMPARE(cellFormat.rightBorderStyle(), QTextFrameFormat::BorderStyle_Groove); + + QCOMPARE(cellFormat.bottomBorder(), qreal(8)); + QCOMPARE(cellFormat.bottomBorderBrush(), QBrush(QColor("green"))); + QCOMPARE(cellFormat.bottomBorderStyle(), QTextFrameFormat::BorderStyle_Groove); + + QCOMPARE(cellFormat.topBorder(), qreal(8)); + QCOMPARE(cellFormat.topBorderBrush(), QBrush(QColor("green"))); + QCOMPARE(cellFormat.topBorderStyle(), QTextFrameFormat::BorderStyle_Groove); +} + +void tst_QTextDocumentFragment::css_tableCellBorderShorthand() +{ + const char html[] = "
Foo
"; + doc->setHtml(html); + + cursor.movePosition(QTextCursor::Start); + cursor.movePosition(QTextCursor::NextBlock); + QTextTable *table = cursor.currentTable(); + QVERIFY(table); + + QTextTableCell cell = table->cellAt(0, 0); + QTextTableCellFormat cellFormat = cell.format().toTableCellFormat(); + QCOMPARE(cellFormat.leftBorder(), qreal(1)); + QCOMPARE(cellFormat.leftBorderBrush(), QBrush(QColor("green"))); + QCOMPARE(cellFormat.leftBorderStyle(), QTextFrameFormat::BorderStyle_Solid); + + QCOMPARE(cellFormat.rightBorder(), qreal(2)); + QCOMPARE(cellFormat.rightBorderBrush(), QBrush(QColor("red"))); + QCOMPARE(cellFormat.rightBorderStyle(), QTextFrameFormat::BorderStyle_Dashed); + + QCOMPARE(cellFormat.bottomBorder(), qreal(3)); + QCOMPARE(cellFormat.bottomBorderBrush(), QBrush(QColor("yellow"))); + QCOMPARE(cellFormat.bottomBorderStyle(), QTextFrameFormat::BorderStyle_Dotted); + + QCOMPARE(cellFormat.topBorder(), qreal(4)); + QCOMPARE(cellFormat.topBorderBrush(), QBrush(QColor("blue"))); + QCOMPARE(cellFormat.topBorderStyle(), QTextFrameFormat::BorderStyle_DotDash); +} + +void tst_QTextDocumentFragment::css_tableCellAllBordersShorthand() +{ + const char html[] = "
Foo
"; + doc->setHtml(html); + + cursor.movePosition(QTextCursor::Start); + cursor.movePosition(QTextCursor::NextBlock); + QTextTable *table = cursor.currentTable(); + QVERIFY(table); + + QTextTableCell cell = table->cellAt(0, 0); + QTextTableCellFormat cellFormat = cell.format().toTableCellFormat(); + QCOMPARE(cellFormat.leftBorder(), qreal(2)); + QCOMPARE(cellFormat.leftBorderBrush(), QBrush(QColor("green"))); + QCOMPARE(cellFormat.leftBorderStyle(), QTextFrameFormat::BorderStyle_Dashed); + + QCOMPARE(cellFormat.rightBorder(), qreal(2)); + QCOMPARE(cellFormat.rightBorderBrush(), QBrush(QColor("green"))); + QCOMPARE(cellFormat.rightBorderStyle(), QTextFrameFormat::BorderStyle_Dashed); + + QCOMPARE(cellFormat.bottomBorder(), qreal(2)); + QCOMPARE(cellFormat.bottomBorderBrush(), QBrush(QColor("green"))); + QCOMPARE(cellFormat.bottomBorderStyle(), QTextFrameFormat::BorderStyle_Dashed); + + QCOMPARE(cellFormat.topBorder(), qreal(2)); + QCOMPARE(cellFormat.topBorderBrush(), QBrush(QColor("green"))); + QCOMPARE(cellFormat.topBorderStyle(), QTextFrameFormat::BorderStyle_Dashed); +} + +void tst_QTextDocumentFragment::css_tableCellOverrideOneBorder() +{ + const char html[] = "
Foo
"; + doc->setHtml(html); + + cursor.movePosition(QTextCursor::Start); + cursor.movePosition(QTextCursor::NextBlock); + QTextTable *table = cursor.currentTable(); + QVERIFY(table); + + QTextTableCell cell = table->cellAt(0, 0); + QTextTableCellFormat cellFormat = cell.format().toTableCellFormat(); + QCOMPARE(cellFormat.leftBorder(), qreal(4)); + QCOMPARE(cellFormat.leftBorderBrush(), QBrush(QColor("red"))); + QCOMPARE(cellFormat.leftBorderStyle(), QTextFrameFormat::BorderStyle_Solid); + + QCOMPARE(cellFormat.rightBorder(), qreal(2)); + QCOMPARE(cellFormat.rightBorderBrush(), QBrush(QColor("green"))); + QCOMPARE(cellFormat.rightBorderStyle(), QTextFrameFormat::BorderStyle_Dashed); + + QCOMPARE(cellFormat.bottomBorder(), qreal(2)); + QCOMPARE(cellFormat.bottomBorderBrush(), QBrush(QColor("green"))); + QCOMPARE(cellFormat.bottomBorderStyle(), QTextFrameFormat::BorderStyle_Dashed); + + QCOMPARE(cellFormat.topBorder(), qreal(2)); + QCOMPARE(cellFormat.topBorderBrush(), QBrush(QColor("green"))); + QCOMPARE(cellFormat.topBorderStyle(), QTextFrameFormat::BorderStyle_Dashed); +} + +void tst_QTextDocumentFragment::css_tableBorderCollapse() +{ + const char html[] = "
Foo
"; + doc->setHtml(html); + + cursor.movePosition(QTextCursor::Start); + cursor.movePosition(QTextCursor::NextBlock); + QTextTable *table = cursor.currentTable(); + QVERIFY(table); + + QCOMPARE(table->format().borderCollapse(), true); +} + void tst_QTextDocumentFragment::css_cellPaddings() { const char html[] = "" diff --git a/tests/auto/gui/text/qtexttable/tst_qtexttable.cpp b/tests/auto/gui/text/qtexttable/tst_qtexttable.cpp index f21b969aa7..7b2ff4cc10 100644 --- a/tests/auto/gui/text/qtexttable/tst_qtexttable.cpp +++ b/tests/auto/gui/text/qtexttable/tst_qtexttable.cpp @@ -50,6 +50,8 @@ typedef QList IntList; QT_FORWARD_DECLARE_CLASS(QTextDocument) +Q_DECLARE_METATYPE(QTextFrameFormat::BorderStyle); + class tst_QTextTable : public QObject { Q_OBJECT @@ -95,6 +97,8 @@ private slots: #if !defined(QT_NO_PRINTER) && defined(QT_BUILD_INTERNAL) void QTBUG31330_renderBackground(); #endif + void checkBorderAttributes_data(); + void checkBorderAttributes(); private: QTextTable *create2x2Table(); @@ -1170,5 +1174,109 @@ void tst_QTextTable::QTBUG31330_renderBackground() } #endif +void tst_QTextTable::checkBorderAttributes_data() +{ + QTest::addColumn("html"); + QTest::addColumn("topBorderWidth"); + QTest::addColumn("bottomBorderWidth"); + QTest::addColumn("leftBorderWidth"); + QTest::addColumn("rightBorderWidth"); + QTest::addColumn("topBorderStyle"); + QTest::addColumn("bottomBorderStyle"); + QTest::addColumn("leftBorderStyle"); + QTest::addColumn("rightBorderStyle"); + QTest::addColumn("topBorderBrush"); + QTest::addColumn("bottomBorderBrush"); + QTest::addColumn("leftBorderBrush"); + QTest::addColumn("rightBorderBrush"); + + const QString tableHtmlStart = QStringLiteral("" + "
Foo
" + "
OneTwo
ThreeFour
"); + QTest::newRow("1px-solid-colors") + << QString("%1" + "td {" + "border-top: 1px solid red;" + "border-bottom: 1px solid blue;" + "border-left: 1px solid green;" + "border-right: 1px solid yellow;" + "}" + "%2").arg(tableHtmlStart).arg(tableHtmlEnd) + << 1.0 << 1.0 << 1.0 << 1.0 + << QTextFrameFormat::BorderStyle_Solid << QTextFrameFormat::BorderStyle_Solid + << QTextFrameFormat::BorderStyle_Solid << QTextFrameFormat::BorderStyle_Solid + << QBrush(Qt::red) << QBrush(Qt::blue) << QBrush(Qt::darkGreen) << QBrush(Qt::yellow); + QTest::newRow("MixedWidth-solid-colors") + << QString("%1" + "td {" + "border-top: 1px solid red;" + "border-bottom: 2px solid blue;" + "border-left: 3px solid green;" + "border-right: 4px solid yellow;" + "}" + "%2").arg(tableHtmlStart).arg(tableHtmlEnd) + << 1.0 << 2.0 << 3.0 << 4.0 + << QTextFrameFormat::BorderStyle_Solid << QTextFrameFormat::BorderStyle_Solid + << QTextFrameFormat::BorderStyle_Solid << QTextFrameFormat::BorderStyle_Solid + << QBrush(Qt::red) << QBrush(Qt::blue) << QBrush(Qt::darkGreen) << QBrush(Qt::yellow); + QTest::newRow("MixedWidth-MixedStyle-colors") + << QString("%1" + "td {" + "border-top: 1px solid red;" + "border-bottom: 2px dotted blue;" + "border-left: 3px dashed green;" + "border-right: 4px inset yellow;" + "}" + "%2").arg(tableHtmlStart).arg(tableHtmlEnd) + << 1.0 << 2.0 << 3.0 << 4.0 + << QTextFrameFormat::BorderStyle_Solid << QTextFrameFormat::BorderStyle_Dotted + << QTextFrameFormat::BorderStyle_Dashed << QTextFrameFormat::BorderStyle_Inset + << QBrush(Qt::red) << QBrush(Qt::blue) << QBrush(Qt::darkGreen) << QBrush(Qt::yellow); +} + +void tst_QTextTable::checkBorderAttributes() +{ + QFETCH(QString, html); + QFETCH(qreal, topBorderWidth); + QFETCH(qreal, bottomBorderWidth); + QFETCH(qreal, leftBorderWidth); + QFETCH(qreal, rightBorderWidth); + QFETCH(QTextFrameFormat::BorderStyle, topBorderStyle); + QFETCH(QTextFrameFormat::BorderStyle, bottomBorderStyle); + QFETCH(QTextFrameFormat::BorderStyle, leftBorderStyle); + QFETCH(QTextFrameFormat::BorderStyle, rightBorderStyle); + QFETCH(QBrush, topBorderBrush); + QFETCH(QBrush, bottomBorderBrush); + QFETCH(QBrush, leftBorderBrush); + QFETCH(QBrush, rightBorderBrush); + + QTextDocument doc; + doc.setHtml(html); + QTextCursor cursor(doc.firstBlock()); + cursor.movePosition(QTextCursor::Right); + + QTextTable *currentTable = cursor.currentTable(); + QVERIFY(currentTable); + for (int row = 0; row < 2; row++) { + for (int column = 0; column < 2; column++) { + QTextTableCell cell = currentTable->cellAt(row, column); + QTextCharFormat cellFormat = cell.format(); + QCOMPARE(cellFormat.doubleProperty(QTextFormat::TableCellTopBorder), topBorderWidth); + QCOMPARE(cellFormat.doubleProperty(QTextFormat::TableCellBottomBorder), bottomBorderWidth); + QCOMPARE(cellFormat.doubleProperty(QTextFormat::TableCellLeftBorder), leftBorderWidth); + QCOMPARE(cellFormat.doubleProperty(QTextFormat::TableCellRightBorder), rightBorderWidth); + QCOMPARE(cellFormat.property(QTextFormat::TableCellTopBorderStyle), topBorderStyle); + QCOMPARE(cellFormat.property(QTextFormat::TableCellBottomBorderStyle), bottomBorderStyle); + QCOMPARE(cellFormat.property(QTextFormat::TableCellLeftBorderStyle), leftBorderStyle); + QCOMPARE(cellFormat.property(QTextFormat::TableCellRightBorderStyle), rightBorderStyle); + QCOMPARE(cellFormat.brushProperty(QTextFormat::TableCellTopBorderBrush), topBorderBrush); + QCOMPARE(cellFormat.brushProperty(QTextFormat::TableCellBottomBorderBrush), bottomBorderBrush); + QCOMPARE(cellFormat.brushProperty(QTextFormat::TableCellLeftBorderBrush), leftBorderBrush); + QCOMPARE(cellFormat.brushProperty(QTextFormat::TableCellRightBorderBrush), rightBorderBrush); + } + } +} + QTEST_MAIN(tst_QTextTable) #include "tst_qtexttable.moc" -- cgit v1.2.3