8#if QT_CONFIG(textmarkdownreader)
9#include "qtextmarkdownimporter_p.h"
11#if QT_CONFIG(textmarkdownwriter)
12#include "qtextmarkdownwriter_p.h"
16#include <qbytearray.h>
17#include <qdatastream.h>
19#include <QtCore/private/qstringiterator_p.h>
23using namespace Qt::StringLiterals;
25QTextCopyHelper::QTextCopyHelper(
const QTextCursor &_source,
const QTextCursor &_destination,
bool forceCharFormat,
const QTextCharFormat &fmt)
27 : formatCollection(*_destination.d->priv->formatCollection()), originalText((
const QString)_source.d->priv->buffer())
29 : formatCollection(*_destination.d->priv->formatCollection()), originalText(_source.d->priv->buffer())
32 src = _source.d->priv;
33 dst = _destination.d->priv;
34 insertPos = _destination.position();
35 this->forceCharFormat = forceCharFormat;
36 primaryCharFormatIndex = convertFormatIndex(fmt);
40int QTextCopyHelper::convertFormatIndex(
const QTextFormat &oldFormat,
int objectIndexToSet)
42 QTextFormat fmt = oldFormat;
43 if (objectIndexToSet != -1) {
44 fmt.setObjectIndex(objectIndexToSet);
45 }
else if (fmt.objectIndex() != -1) {
46 int newObjectIndex = objectIndexMap.value(fmt.objectIndex(), -1);
47 if (newObjectIndex == -1) {
48 QTextFormat objFormat = src->formatCollection()->objectFormat(fmt.objectIndex());
49 Q_ASSERT(objFormat.objectIndex() == -1);
50 newObjectIndex = formatCollection.createObjectIndex(objFormat);
51 objectIndexMap.insert(fmt.objectIndex(), newObjectIndex);
53 fmt.setObjectIndex(newObjectIndex);
55 int idx = formatCollection.indexForFormat(fmt);
56 Q_ASSERT(formatCollection.format(idx).type() == oldFormat.type());
60int QTextCopyHelper::appendFragment(
int pos,
int endPos,
int objectIndex)
62 QTextDocumentPrivate::FragmentIterator fragIt = src->find(pos);
65 Q_ASSERT(objectIndex == -1
66 || (frag->size_array[0] == 1 && src->formatCollection()->format(frag->format).objectIndex() != -1));
70 charFormatIndex = primaryCharFormatIndex;
72 charFormatIndex = convertFormatIndex(frag
->format, objectIndex);
74 const int inFragmentOffset = qMax(0, pos - fragIt.position());
75 int charsToCopy = qMin(
int(frag->size_array[0] - inFragmentOffset), endPos - pos);
77 QTextBlock nextBlock = src->blocksFind(pos + 1);
80 if (nextBlock.position() == pos + 1) {
81 blockIdx = convertFormatIndex(nextBlock.blockFormat());
82 }
else if (pos == 0 && insertPos == 0) {
83 dst->setBlockFormat(dst->blocksBegin(), dst->blocksBegin(), convertFormat(src->blocksBegin().blockFormat()).toBlockFormat());
84 dst->setCharFormat(-1, 1, convertFormat(src->blocksBegin().charFormat()).toCharFormat());
87 QString txtToInsert(originalText.constData() + frag->stringPosition + inFragmentOffset, charsToCopy);
88 if (txtToInsert.size() == 1
89 && (txtToInsert.at(0) == QChar::ParagraphSeparator
94 dst->insertBlock(txtToInsert.at(0), insertPos, blockIdx, charFormatIndex);
97 if (nextBlock.textList()) {
98 QTextBlock dstBlock = dst->blocksFind(insertPos);
99 if (!dstBlock.textList()) {
103 int listBlockFormatIndex = convertFormatIndex(nextBlock.blockFormat());
104 int listCharFormatIndex = convertFormatIndex(nextBlock.charFormat());
105 dst->insertBlock(insertPos, listBlockFormatIndex, listCharFormatIndex);
109 dst->insert(insertPos, txtToInsert, charFormatIndex);
110 const int userState = nextBlock.userState();
112 dst->blocksFind(insertPos).setUserState(userState);
113 insertPos += txtToInsert.size();
121 Q_ASSERT(pos < endPos);
124 pos += appendFragment(pos, endPos);
129 if (cursor.hasComplexSelection()) {
130 QTextTable *table = cursor.currentTable();
131 int row_start, col_start, num_rows, num_cols;
132 cursor.selectedTableCells(&row_start, &num_rows, &col_start, &num_cols);
134 QTextTableFormat tableFormat = table->format();
135 tableFormat.setColumns(num_cols);
136 tableFormat.clearColumnWidthConstraints();
137 const int objectIndex = dst->formatCollection()->createObjectIndex(tableFormat);
139 Q_ASSERT(row_start != -1);
140 for (
int r = row_start; r < row_start + num_rows; ++r) {
141 for (
int c = col_start; c < col_start + num_cols; ++c) {
142 QTextTableCell cell = table->cellAt(r, c);
143 const int rspan = cell.rowSpan();
144 const int cspan = cell.columnSpan();
151 int cc = cell.column();
157 QTextCharFormat cellFormat = cell.format();
158 if (r + rspan >= row_start + num_rows) {
159 cellFormat.setTableCellRowSpan(row_start + num_rows - r);
161 if (c + cspan >= col_start + num_cols) {
162 cellFormat.setTableCellColumnSpan(col_start + num_cols - c);
164 const int charFormatIndex = convertFormatIndex(cellFormat, objectIndex);
167 const int cellPos = cell.firstPosition();
168 QTextBlock block = src->blocksFind(cellPos);
169 if (block.position() == cellPos) {
170 blockIdx = convertFormatIndex(block.blockFormat());
177 if (cell.lastPosition() > cellPos) {
179 appendFragments(cellPos, cell.lastPosition());
185 int end = table->lastPosition();
186 appendFragment(end, end+1, objectIndex);
188 appendFragments(cursor.selectionStart(), cursor.selectionEnd());
195 doc->setUndoRedoEnabled(
false);
197 if (!_cursor.hasSelection())
200 QTextDocumentPrivate *p = QTextDocumentPrivate::get(doc);
202 QTextCursor destCursor(doc);
207 p->mergeCachedResources(_cursor.d->priv);
212 if (_cursor.isNull())
215 QTextDocumentPrivate *destPieceTable = _cursor.d->priv;
216 destPieceTable->beginEditBlock();
218 QTextCursor sourceCursor(doc);
219 sourceCursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
220 QTextCopyHelper(sourceCursor, _cursor, importedFromPlainText, _cursor.charFormat()).copy();
222 destPieceTable->endEditBlock();
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
248
249
250
251
252QTextDocumentFragment::QTextDocumentFragment()
258
259
260
261
262QTextDocumentFragment::QTextDocumentFragment(
const QTextDocument *document)
268 QTextCursor cursor(
const_cast<QTextDocument *>(document));
269 cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
270 d =
new QTextDocumentFragmentPrivate(cursor);
274
275
276
277
278
279QTextDocumentFragment::QTextDocumentFragment(
const QTextCursor &cursor)
282 if (!cursor.hasSelection())
285 d =
new QTextDocumentFragmentPrivate(cursor);
289
290
291
292
293QTextDocumentFragment::QTextDocumentFragment(
const QTextDocumentFragment &rhs)
301
302
303
304
305QTextDocumentFragment &QTextDocumentFragment::operator=(
const QTextDocumentFragment &rhs)
309 if (d && !d->ref.deref())
316
317
318QTextDocumentFragment::~QTextDocumentFragment()
320 if (d && !d->ref.deref())
325
326
327bool QTextDocumentFragment::isEmpty()
const
329 return d ==
nullptr || d->doc ==
nullptr || QTextDocumentPrivate::get(d->doc)->length() <= 1;
333
334
335
336
337
338
339
340
341
342
343QString QTextDocumentFragment::toPlainText()
const
348 return d->doc->toPlainText();
352
353
354
355
356
357
358QString QTextDocumentFragment::toRawText()
const
363 return d->doc->toRawText();
366#ifndef QT_NO_TEXTHTMLPARSER
369
370
371
372
373
374
375QString QTextDocumentFragment::toHtml()
const
380 return QTextHtmlExporter(d->doc).toHtml(QTextHtmlExporter::ExportFragment);
385#if QT_CONFIG(textmarkdownwriter)
388
389
390
391
392
393
394
395QString QTextDocumentFragment::toMarkdown(QTextDocument::MarkdownFeatures features)
const
400 return d->doc->toMarkdown(features);
406
407
408
409
410
411QTextDocumentFragment QTextDocumentFragment::fromPlainText(
const QString &plainText)
413 QTextDocumentFragment res;
415 res.d =
new QTextDocumentFragmentPrivate;
416 res.d->importedFromPlainText =
true;
417 QTextCursor cursor(res.d->doc);
418 cursor.insertText(plainText);
422#ifndef QT_NO_TEXTHTMLPARSER
426 if (style == QTextListFormat::ListDisc)
427 return QTextListFormat::ListCircle;
428 else if (style == QTextListFormat::ListCircle)
429 return QTextListFormat::ListSquare;
434 : indent(0), headingLevel(0), compressNextWhitespace(PreserveWhiteSpace), doc(_doc), importMode(mode)
436 cursor = QTextCursor(doc);
437 wsm = QTextHtmlParserNode::WhiteSpaceNormal;
439 QString html = _html;
440 const int startFragmentPos = html.indexOf(
"<!--StartFragment-->"_L1);
441 if (startFragmentPos != -1) {
442 const auto qt3RichTextHeader =
"<meta name=\"qrichtext\" content=\"1\" />"_L1;
445 const bool hasQtRichtextMetaTag = html.contains(qt3RichTextHeader);
447 const int endFragmentPos = html.indexOf(
"<!--EndFragment-->"_L1);
448 if (startFragmentPos < endFragmentPos)
449 html = html.mid(startFragmentPos, endFragmentPos - startFragmentPos);
451 html = html.mid(startFragmentPos);
453 if (hasQtRichtextMetaTag)
454 html.prepend(qt3RichTextHeader);
457 parse(html, resourceProvider ? resourceProvider : doc);
463 cursor.beginEditBlock();
465 forceBlockMerging =
false;
466 compressNextWhitespace = RemoveWhiteSpace;
467 blockTagClosed =
false;
468 for (currentNodeIdx = 0; currentNodeIdx < count(); ++currentNodeIdx) {
469 currentNode = &at(currentNodeIdx);
470 wsm = textEditMode ? QTextHtmlParserNode::WhiteSpacePreWrap : currentNode->wsm;
473
474
475
476
477
478
479
480
481
482
483
486
487
488
489
490
491 if (currentNodeIdx > 0 && (currentNode->parent != currentNodeIdx - 1)) {
492 const bool lastBlockTagClosed = closeTag();
493 blockTagClosed = blockTagClosed || lastBlockTagClosed;
498 && !currentNode->isBlock()
499 && currentNode->id != Html_unknown)
502 }
else if (blockTagClosed && hasBlock) {
504 QTextBlockFormat blockFormat = currentNode->blockFormat;
505 blockFormat.setIndent(indent);
507 QTextBlockFormat oldFormat = cursor.blockFormat();
508 if (oldFormat.hasProperty(QTextFormat::PageBreakPolicy)) {
509 QTextFormat::PageBreakFlags pageBreak = oldFormat.pageBreakPolicy();
510 if (pageBreak == QTextFormat::PageBreak_AlwaysAfter)
512
513
514
515 pageBreak = QTextFormat::PageBreak_AlwaysBefore;
516 blockFormat.setPageBreakPolicy(pageBreak);
519 cursor.setBlockFormat(blockFormat);
523 if (currentNode->displayMode == QTextHtmlElement::DisplayNone) {
524 if (currentNode->id == Html_title)
525 doc->setMetaInformation(QTextDocument::DocumentTitle, currentNode->text);
530 if (processSpecialNodes() == ContinueWithNextNode)
536 && !currentNode->isBlock()
537 && !currentNode->text.isEmpty() && !currentNode->hasOnlyWhitespace()
538 && currentNode->displayMode == QTextHtmlElement::DisplayInline) {
540 QTextBlockFormat block = currentNode->blockFormat;
541 block.setIndent(indent);
543 appendBlock(block, currentNode->charFormat);
545 blockTagClosed =
false;
549 if (currentNode->isBlock()) {
551 if (result == ContinueWithNextNode) {
553 }
else if (result == ContinueWithNextSibling) {
554 currentNodeIdx += currentNode->children.size();
559 if (currentNode->charFormat.isAnchor()) {
560 const auto names = currentNode->charFormat.anchorNames();
561 if (!names.isEmpty())
562 namedAnchors.append(names.constFirst());
565 if (appendNodeText())
570 cursor.endEditBlock();
575 const int initialCursorPosition = cursor.position();
576 QTextCharFormat format = currentNode->charFormat;
578 if (wsm == QTextHtmlParserNode::WhiteSpacePre || wsm == QTextHtmlParserNode::WhiteSpacePreWrap)
579 compressNextWhitespace = PreserveWhiteSpace;
581 const QString text = currentNode->text;
583 QString textToInsert;
584 textToInsert.reserve(text.size());
586 QStringIterator it(text);
587 while (it.hasNext()) {
588 char32_t ch = it.next();
590 if (QChar::isSpace(ch)
592 && ch != QChar::ParagraphSeparator) {
594 if (wsm == QTextHtmlParserNode::WhiteSpacePreLine && (ch == u'\n' || ch == u'\r'))
595 compressNextWhitespace = PreserveWhiteSpace;
597 if (compressNextWhitespace == CollapseWhiteSpace)
598 compressNextWhitespace = RemoveWhiteSpace;
599 else if (compressNextWhitespace == RemoveWhiteSpace)
602 if (wsm == QTextHtmlParserNode::WhiteSpacePre
608 }
else if (ch == u'\r') {
611 }
else if (wsm != QTextHtmlParserNode::WhiteSpacePreWrap) {
612 compressNextWhitespace = RemoveWhiteSpace;
613 if (wsm == QTextHtmlParserNode::WhiteSpacePreLine && (ch == u'\n' || ch == u'\r'))
615 else if (wsm == QTextHtmlParserNode::WhiteSpaceNoWrap)
621 compressNextWhitespace = PreserveWhiteSpace;
625 || ch == QChar::ParagraphSeparator) {
627 if (!textToInsert.isEmpty()) {
628 if (wsm == QTextHtmlParserNode::WhiteSpacePreLine && textToInsert.at(textToInsert.size() - 1) == u' ')
629 textToInsert = textToInsert.chopped(1);
630 cursor.insertText(textToInsert, format);
631 textToInsert.clear();
634 QTextBlockFormat fmt = cursor.blockFormat();
636 if (fmt.hasProperty(QTextFormat::BlockBottomMargin)) {
637 QTextBlockFormat tmp = fmt;
638 tmp.clearProperty(QTextFormat::BlockBottomMargin);
639 cursor.setBlockFormat(tmp);
642 fmt.clearProperty(QTextFormat::BlockTopMargin);
643 appendBlock(fmt, cursor.charFormat());
645 if (!namedAnchors.isEmpty()) {
646 if (!textToInsert.isEmpty()) {
647 cursor.insertText(textToInsert, format);
648 textToInsert.clear();
651 format.setAnchor(
true);
652 format.setAnchorNames(namedAnchors);
653 cursor.insertText(QString::fromUcs4(&ch, 1), format);
654 namedAnchors.clear();
655 format.clearProperty(QTextFormat::IsAnchor);
656 format.clearProperty(QTextFormat::AnchorName);
658 textToInsert += QChar::fromUcs4(ch);
663 if (!textToInsert.isEmpty()) {
664 cursor.insertText(textToInsert, format);
667 return cursor.position() != initialCursorPosition;
672 switch (currentNode->id) {
674 if (currentNode->charFormat.background().style() != Qt::NoBrush) {
675 QTextFrameFormat fmt = doc->rootFrame()->frameFormat();
676 fmt.setBackground(currentNode->charFormat.background());
677 doc->rootFrame()->setFrameFormat(fmt);
678 const_cast<QTextHtmlParserNode *>(currentNode)->charFormat.clearProperty(QTextFormat::BackgroundBrush);
680 compressNextWhitespace = RemoveWhiteSpace;
685 QTextListFormat::Style style = currentNode->listStyle;
687 if (currentNode->id == Html_ul && !currentNode->hasOwnListStyle && currentNode->parent) {
688 const QTextHtmlParserNode *n = &at(currentNode->parent);
690 if (n->id == Html_ul) {
691 style = nextListStyle(currentNode->listStyle);
700 QTextListFormat listFmt;
701 listFmt.setStyle(style);
702 if (!currentNode->textListNumberPrefix.isNull())
703 listFmt.setNumberPrefix(currentNode->textListNumberPrefix);
704 if (!currentNode->textListNumberSuffix.isNull())
705 listFmt.setNumberSuffix(currentNode->textListNumberSuffix);
706 if (currentNode->listStart != 1)
707 listFmt.setStart(currentNode->listStart);
709 if (currentNode->hasCssListIndent) {
710 indent += currentNode->cssListIndent;
711 listFmt.setIndent(currentNode->cssListIndent);
714 listFmt.setIndent(indent);
719 l.listNode = currentNodeIdx;
721 compressNextWhitespace = RemoveWhiteSpace;
724 const QString simpl = currentNode->text.simplified();
725 if (simpl.isEmpty() || simpl.at(0).isSpace())
726 return ContinueWithNextNode;
731 Table t = scanTable(currentNodeIdx);
734 compressNextWhitespace = RemoveWhiteSpace;
735 return ContinueWithNextNode;
739 return ContinueWithNextNode;
742 QTextImageFormat fmt;
743 fmt.setName(currentNode->imageName);
744 if (!currentNode->text.isEmpty())
745 fmt.setProperty(QTextFormat::ImageTitle, currentNode->text);
746 if (!currentNode->imageAlt.isEmpty())
747 fmt.setProperty(QTextFormat::ImageAltText, currentNode->imageAlt);
749 fmt.merge(currentNode->charFormat);
751 if (currentNode->imageWidth != -1)
752 fmt.setWidth(currentNode->imageWidth);
753 if (currentNode->imageHeight != -1)
754 fmt.setHeight(currentNode->imageHeight);
756 cursor.insertImage(fmt, QTextFrameFormat::Position(currentNode->cssFloat));
758 cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
759 cursor.mergeCharFormat(currentNode->charFormat);
760 cursor.movePosition(QTextCursor::NextCharacter);
761 compressNextWhitespace = CollapseWhiteSpace;
764 return ContinueWithNextNode;
768 QTextBlockFormat blockFormat = currentNode->blockFormat;
769 blockFormat.setTopMargin(topMargin(currentNodeIdx));
770 blockFormat.setBottomMargin(bottomMargin(currentNodeIdx));
771 blockFormat.setProperty(QTextFormat::BlockTrailingHorizontalRulerWidth, currentNode->width);
772 if (hasBlock && importMode == ImportToDocument)
773 cursor.mergeBlockFormat(blockFormat);
775 appendBlock(blockFormat);
777 compressNextWhitespace = RemoveWhiteSpace;
778 return ContinueWithNextNode;
803 return ContinueWithCurrentNode;
809 const QTextHtmlParserNode *closedNode = &at(currentNodeIdx - 1);
810 const int endDepth = depth(currentNodeIdx) - 1;
811 int depth =
this->depth(currentNodeIdx - 1);
812 bool blockTagClosed =
false;
814 while (depth > endDepth) {
816 if (!tables.isEmpty())
819 switch (closedNode->id) {
821 if (t && !t->isTextFrame) {
825 while (!t->currentCell.atEnd() && t->currentCell.row < t->currentRow)
829 blockTagClosed =
true;
835 indent = t->lastIndent;
837 tables.resize(tables.size() - 1);
840 if (tables.isEmpty()) {
841 cursor = doc->rootFrame()->lastCursorPosition();
845 cursor = t->frame->lastCursorPosition();
846 else if (!t->currentCell.atEnd())
847 cursor = t->currentCell.cell().lastCursorPosition();
853 blockTagClosed =
false;
854 compressNextWhitespace = RemoveWhiteSpace;
859 if (t && !t->isTextFrame)
861 blockTagClosed =
true;
862 compressNextWhitespace = RemoveWhiteSpace;
869 lists.resize(lists.size() - 1);
870 if (currentNode->hasCssListIndent)
871 indent -= currentNode->cssListIndent;
874 blockTagClosed =
true;
878 compressNextWhitespace = RemoveWhiteSpace;
882 if (cursor.position() > 0) {
883 const QChar curChar = cursor.document()->characterAt(cursor.position() - 1);
884 if (!closedNode->children.isEmpty() && curChar != QChar::LineSeparator) {
885 blockTagClosed =
true;
896 blockTagClosed =
true;
899 if (closedNode->isBlock())
900 blockTagClosed =
true;
904 closedNode = &at(closedNode->parent);
908 return blockTagClosed;
916 QList<QTextLength> columnWidths;
918 int tableHeaderRowCount = 0;
920 rowNodes.reserve(at(tableNodeIdx).children.size());
921 for (
int row : at(tableNodeIdx).children) {
922 switch (at(row).id) {
929 for (
int potentialRow : at(row).children) {
930 if (at(potentialRow).id == Html_tr) {
931 rowNodes += potentialRow;
932 if (at(row).id == Html_thead)
933 ++tableHeaderRowCount;
941 QList<RowColSpanInfo> rowColSpans;
942 QList<RowColSpanInfo> rowColSpanForColumn;
944 int effectiveRow = 0;
945 for (
int row : std::as_const(rowNodes)) {
948 for (
int cell : at(row).children) {
949 if (at(cell).isTableCell()) {
951 while (colsInRow < rowColSpanForColumn.size()) {
952 const RowColSpanInfo &spanInfo = rowColSpanForColumn.at(colsInRow);
954 if (spanInfo.row + spanInfo.rowSpan > effectiveRow) {
955 Q_ASSERT(spanInfo.col == colsInRow);
956 colsInRow += spanInfo.colSpan;
961 const QTextHtmlParserNode &c = at(cell);
962 const int currentColumn = colsInRow;
963 colsInRow += c.tableCellColSpan;
965 RowColSpanInfo spanInfo;
966 spanInfo.row = effectiveRow;
967 spanInfo.col = currentColumn;
968 spanInfo.colSpan = c.tableCellColSpan;
969 spanInfo.rowSpan = c.tableCellRowSpan;
970 if (spanInfo.colSpan > 1 || spanInfo.rowSpan > 1)
971 rowColSpans.append(spanInfo);
973 columnWidths.resize(qMax(columnWidths.size(), colsInRow));
974 rowColSpanForColumn.resize(columnWidths.size());
975 for (
int i = currentColumn; i < currentColumn + c.tableCellColSpan; ++i) {
976 if (columnWidths.at(i).type() == QTextLength::VariableLength) {
977 QTextLength w = c.width;
978 if (c.tableCellColSpan > 1 && w.type() != QTextLength::VariableLength)
979 w = QTextLength(w.type(), w.value(100.) / c.tableCellColSpan);
982 rowColSpanForColumn[i] = spanInfo;
987 table.columns = qMax(table.columns, colsInRow);
991 table.rows = effectiveRow;
993 table.lastIndent = indent;
996 if (table.rows == 0 || table.columns == 0)
999 QTextFrameFormat fmt;
1000 const QTextHtmlParserNode &node = at(tableNodeIdx);
1002 if (!node.isTextFrame) {
1003 QTextTableFormat tableFmt;
1004 tableFmt.setCellSpacing(node.tableCellSpacing);
1005 tableFmt.setCellPadding(node.tableCellPadding);
1006 if (node.blockFormat.hasProperty(QTextFormat::BlockAlignment))
1007 tableFmt.setAlignment(node.blockFormat.alignment());
1008 tableFmt.setColumns(table.columns);
1009 tableFmt.setColumnWidthConstraints(columnWidths);
1010 tableFmt.setHeaderRowCount(tableHeaderRowCount);
1011 tableFmt.setBorderCollapse(node.borderCollapse);
1015 fmt.setTopMargin(topMargin(tableNodeIdx));
1016 fmt.setBottomMargin(bottomMargin(tableNodeIdx));
1017 fmt.setLeftMargin(leftMargin(tableNodeIdx)
1018 + table.lastIndent * 40
1020 fmt.setRightMargin(rightMargin(tableNodeIdx));
1023 if (qFuzzyCompare(fmt.leftMargin(), fmt.rightMargin())
1024 && qFuzzyCompare(fmt.leftMargin(), fmt.topMargin())
1025 && qFuzzyCompare(fmt.leftMargin(), fmt.bottomMargin()))
1026 fmt.setProperty(QTextFormat::FrameMargin, fmt.leftMargin());
1028 fmt.setBorderStyle(node.borderStyle);
1029 fmt.setBorderBrush(node.borderBrush);
1030 fmt.setBorder(node.tableBorder);
1031 fmt.setWidth(node.width);
1032 fmt.setHeight(node.height);
1033 if (node.blockFormat.hasProperty(QTextFormat::PageBreakPolicy))
1034 fmt.setPageBreakPolicy(node.blockFormat.pageBreakPolicy());
1036 if (node.blockFormat.hasProperty(QTextFormat::LayoutDirection))
1037 fmt.setLayoutDirection(node.blockFormat.layoutDirection());
1038 if (node.charFormat.background().style() != Qt::NoBrush)
1039 fmt.setBackground(node.charFormat.background());
1040 fmt.setPosition(QTextFrameFormat::Position(node.cssFloat));
1042 if (node.isTextFrame) {
1043 if (node.isRootFrame) {
1044 table.frame = cursor.currentFrame();
1045 table.frame->setFrameFormat(fmt);
1047 table.frame = cursor.insertFrame(fmt);
1049 table.isTextFrame =
true;
1051 const int oldPos = cursor.position();
1052 QTextTable *textTable = cursor.insertTable(table.rows, table.columns, fmt.toTableFormat());
1053 table.frame = textTable;
1055 for (
int i = 0; i < rowColSpans.size(); ++i) {
1056 const RowColSpanInfo &nfo = rowColSpans.at(i);
1057 textTable->mergeCells(nfo.row, nfo.col, nfo.rowSpan, nfo.colSpan);
1060 table.currentCell = TableCellIterator(textTable);
1061 cursor.setPosition(oldPos);
1068 QTextBlockFormat block;
1069 QTextCharFormat charFmt;
1070 bool modifiedBlockFormat =
true;
1071 bool modifiedCharFormat =
true;
1073 if (currentNode->isTableCell() && !tables.isEmpty()) {
1074 Table &t = tables.last();
1075 if (!t.isTextFrame && !t.currentCell.atEnd()) {
1076 QTextTableCell cell = t.currentCell.cell();
1077 if (cell.isValid()) {
1078 QTextTableCellFormat fmt = cell.format().toTableCellFormat();
1079 if (topPadding(currentNodeIdx) >= 0)
1080 fmt.setTopPadding(topPadding(currentNodeIdx));
1081 if (bottomPadding(currentNodeIdx) >= 0)
1082 fmt.setBottomPadding(bottomPadding(currentNodeIdx));
1083 if (leftPadding(currentNodeIdx) >= 0)
1084 fmt.setLeftPadding(leftPadding(currentNodeIdx));
1085 if (rightPadding(currentNodeIdx) >= 0)
1086 fmt.setRightPadding(rightPadding(currentNodeIdx));
1087#ifndef QT_NO_CSSPARSER
1088 if (tableCellBorder(currentNodeIdx, QCss::TopEdge) > 0)
1089 fmt.setTopBorder(tableCellBorder(currentNodeIdx, QCss::TopEdge));
1090 if (tableCellBorder(currentNodeIdx, QCss::RightEdge) > 0)
1091 fmt.setRightBorder(tableCellBorder(currentNodeIdx, QCss::RightEdge));
1092 if (tableCellBorder(currentNodeIdx, QCss::BottomEdge) > 0)
1093 fmt.setBottomBorder(tableCellBorder(currentNodeIdx, QCss::BottomEdge));
1094 if (tableCellBorder(currentNodeIdx, QCss::LeftEdge) > 0)
1095 fmt.setLeftBorder(tableCellBorder(currentNodeIdx, QCss::LeftEdge));
1096 if (tableCellBorderStyle(currentNodeIdx, QCss::TopEdge) != QTextFrameFormat::BorderStyle_None)
1097 fmt.setTopBorderStyle(tableCellBorderStyle(currentNodeIdx, QCss::TopEdge));
1098 if (tableCellBorderStyle(currentNodeIdx, QCss::RightEdge) != QTextFrameFormat::BorderStyle_None)
1099 fmt.setRightBorderStyle(tableCellBorderStyle(currentNodeIdx, QCss::RightEdge));
1100 if (tableCellBorderStyle(currentNodeIdx, QCss::BottomEdge) != QTextFrameFormat::BorderStyle_None)
1101 fmt.setBottomBorderStyle(tableCellBorderStyle(currentNodeIdx, QCss::BottomEdge));
1102 if (tableCellBorderStyle(currentNodeIdx, QCss::LeftEdge) != QTextFrameFormat::BorderStyle_None)
1103 fmt.setLeftBorderStyle(tableCellBorderStyle(currentNodeIdx, QCss::LeftEdge));
1104 if (tableCellBorderBrush(currentNodeIdx, QCss::TopEdge) != Qt::NoBrush)
1105 fmt.setTopBorderBrush(tableCellBorderBrush(currentNodeIdx, QCss::TopEdge));
1106 if (tableCellBorderBrush(currentNodeIdx, QCss::RightEdge) != Qt::NoBrush)
1107 fmt.setRightBorderBrush(tableCellBorderBrush(currentNodeIdx, QCss::RightEdge));
1108 if (tableCellBorderBrush(currentNodeIdx, QCss::BottomEdge) != Qt::NoBrush)
1109 fmt.setBottomBorderBrush(tableCellBorderBrush(currentNodeIdx, QCss::BottomEdge));
1110 if (tableCellBorderBrush(currentNodeIdx, QCss::LeftEdge) != Qt::NoBrush)
1111 fmt.setLeftBorderBrush(tableCellBorderBrush(currentNodeIdx, QCss::LeftEdge));
1114 cell.setFormat(fmt);
1116 cursor.setPosition(cell.firstPosition());
1120 compressNextWhitespace = RemoveWhiteSpace;
1122 if (currentNode->charFormat.background().style() != Qt::NoBrush) {
1123 charFmt.setBackground(currentNode->charFormat.background());
1124 cursor.mergeBlockCharFormat(charFmt);
1129 block = cursor.blockFormat();
1130 charFmt = cursor.blockCharFormat();
1131 modifiedBlockFormat =
false;
1132 modifiedCharFormat =
false;
1137 qreal tm = qreal(topMargin(currentNodeIdx));
1138 if (tm > block.topMargin()) {
1139 block.setTopMargin(tm);
1140 modifiedBlockFormat =
true;
1144 int bottomMargin =
this->bottomMargin(currentNodeIdx);
1148 const QTextHtmlParserNode *parentNode = currentNode->parent ? &at(currentNode->parent) :
nullptr;
1149 if ((currentNode->id == Html_li || currentNode->id == Html_dt || currentNode->id == Html_dd)
1151 && (parentNode->isListStart() || parentNode->id == Html_dl)
1152 && (parentNode->children.last() == currentNodeIdx)) {
1153 bottomMargin = qMax(bottomMargin,
this->bottomMargin(currentNode->parent));
1156 if (block.bottomMargin() != bottomMargin) {
1157 block.setBottomMargin(bottomMargin);
1158 modifiedBlockFormat =
true;
1162 const qreal lm = leftMargin(currentNodeIdx);
1163 const qreal rm = rightMargin(currentNodeIdx);
1165 if (block.leftMargin() != lm) {
1166 block.setLeftMargin(lm);
1167 modifiedBlockFormat =
true;
1169 if (block.rightMargin() != rm) {
1170 block.setRightMargin(rm);
1171 modifiedBlockFormat =
true;
1175 if (currentNode->id != Html_li
1179 || !lists.constLast().list
1180 || lists.constLast().list->itemNumber(cursor.block()) == -1
1183 block.setIndent(indent);
1184 modifiedBlockFormat =
true;
1188 block.setHeadingLevel(headingLevel);
1189 modifiedBlockFormat =
true;
1192 if (currentNode->blockFormat.propertyCount() > 0) {
1193 modifiedBlockFormat =
true;
1194 block.merge(currentNode->blockFormat);
1197 if (currentNode->charFormat.propertyCount() > 0) {
1198 modifiedCharFormat =
true;
1199 charFmt.merge(currentNode->charFormat);
1205 if (wsm == QTextHtmlParserNode::WhiteSpacePre
1206 || wsm == QTextHtmlParserNode::WhiteSpaceNoWrap) {
1207 block.setNonBreakableLines(
true);
1208 modifiedBlockFormat =
true;
1211 if (currentNode->charFormat.background().style() != Qt::NoBrush && !currentNode->isTableCell()) {
1212 block.setBackground(currentNode->charFormat.background());
1213 modifiedBlockFormat =
true;
1216 if (hasBlock && (!currentNode->isEmptyParagraph || forceBlockMerging)) {
1217 if (modifiedBlockFormat)
1218 cursor.setBlockFormat(block);
1219 if (modifiedCharFormat)
1220 cursor.setBlockCharFormat(charFmt);
1222 if (currentNodeIdx == 1 && cursor.position() == 0 && currentNode->isEmptyParagraph) {
1223 cursor.setBlockFormat(block);
1224 cursor.setBlockCharFormat(charFmt);
1226 appendBlock(block, charFmt);
1230 if (currentNode->userState != -1)
1231 cursor.block().setUserState(currentNode->userState);
1233 if (currentNode->id == Html_li && !lists.isEmpty()) {
1234 List &l = lists.last();
1236 l.list->add(cursor.block());
1238 l.list = cursor.createList(l.format);
1239 const qreal listTopMargin = topMargin(l.listNode);
1240 if (listTopMargin > block.topMargin()) {
1241 block.setTopMargin(listTopMargin);
1242 cursor.mergeBlockFormat(block);
1246 QTextBlockFormat fmt;
1247 fmt.setIndent(currentNode->blockFormat.indent());
1248 cursor.mergeBlockFormat(fmt);
1252 forceBlockMerging =
false;
1253 if (currentNode->id == Html_body || currentNode->id == Html_html)
1254 forceBlockMerging =
true;
1256 if (currentNode->isEmptyParagraph) {
1258 return ContinueWithNextSibling;
1262 blockTagClosed =
false;
1263 return ContinueWithCurrentNode;
1266void QTextHtmlImporter::appendBlock(
const QTextBlockFormat &format, QTextCharFormat charFmt)
1268 if (!namedAnchors.isEmpty()) {
1269 charFmt.setAnchor(
true);
1270 charFmt.setAnchorNames(namedAnchors);
1271 namedAnchors.clear();
1274 cursor.insertBlock(format, charFmt);
1276 if (wsm != QTextHtmlParserNode::WhiteSpacePre && wsm != QTextHtmlParserNode::WhiteSpacePreWrap)
1277 compressNextWhitespace = RemoveWhiteSpace;
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1293QTextDocumentFragment QTextDocumentFragment::fromHtml(
const QString &html,
const QTextDocument *resourceProvider)
1295 QTextDocumentFragment res;
1296 res.d =
new QTextDocumentFragmentPrivate;
1298 QTextHtmlImporter importer(res.d->doc, html, QTextHtmlImporter::ImportToFragment, resourceProvider);
1305#if QT_CONFIG(textmarkdownreader)
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320QTextDocumentFragment QTextDocumentFragment::fromMarkdown(
const QString &markdown, QTextDocument::MarkdownFeatures features)
1322 QTextDocumentFragment res;
1323 res.d =
new QTextDocumentFragmentPrivate;
1325 QTextMarkdownImporter(res.d->doc, features).import(markdown);
void insert(QTextCursor &cursor) const
QTextDocumentFragmentPrivate(const QTextCursor &cursor=QTextCursor())
Combined button and popup list for selecting options.
#define QTextBeginningOfFrame
static QTextListFormat::Style nextListStyle(QTextListFormat::Style style)