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);
710 if (currentNode->hasCssListIndent)
711 listFmt.setIndent(currentNode->cssListIndent);
713 listFmt.setIndent(indent);
717 l.listNode = currentNodeIdx;
719 compressNextWhitespace = RemoveWhiteSpace;
722 const QString simpl = currentNode->text.simplified();
723 if (simpl.isEmpty() || simpl.at(0).isSpace())
724 return ContinueWithNextNode;
729 Table t = scanTable(currentNodeIdx);
732 compressNextWhitespace = RemoveWhiteSpace;
733 return ContinueWithNextNode;
737 return ContinueWithNextNode;
740 QTextImageFormat fmt;
741 fmt.setName(currentNode->imageName);
742 if (!currentNode->text.isEmpty())
743 fmt.setProperty(QTextFormat::ImageTitle, currentNode->text);
744 if (!currentNode->imageAlt.isEmpty())
745 fmt.setProperty(QTextFormat::ImageAltText, currentNode->imageAlt);
747 fmt.merge(currentNode->charFormat);
749 if (currentNode->imageWidth != -1)
750 fmt.setWidth(currentNode->imageWidth);
751 if (currentNode->imageHeight != -1)
752 fmt.setHeight(currentNode->imageHeight);
754 cursor.insertImage(fmt, QTextFrameFormat::Position(currentNode->cssFloat));
756 cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
757 cursor.mergeCharFormat(currentNode->charFormat);
758 cursor.movePosition(QTextCursor::NextCharacter);
759 compressNextWhitespace = CollapseWhiteSpace;
762 return ContinueWithNextNode;
766 QTextBlockFormat blockFormat = currentNode->blockFormat;
767 blockFormat.setTopMargin(topMargin(currentNodeIdx));
768 blockFormat.setBottomMargin(bottomMargin(currentNodeIdx));
769 blockFormat.setProperty(QTextFormat::BlockTrailingHorizontalRulerWidth, currentNode->width);
770 if (hasBlock && importMode == ImportToDocument)
771 cursor.mergeBlockFormat(blockFormat);
773 appendBlock(blockFormat);
775 compressNextWhitespace = RemoveWhiteSpace;
776 return ContinueWithNextNode;
801 return ContinueWithCurrentNode;
807 const QTextHtmlParserNode *closedNode = &at(currentNodeIdx - 1);
808 const int endDepth = depth(currentNodeIdx) - 1;
809 int depth =
this->depth(currentNodeIdx - 1);
810 bool blockTagClosed =
false;
812 while (depth > endDepth) {
814 if (!tables.isEmpty())
817 switch (closedNode->id) {
819 if (t && !t->isTextFrame) {
823 while (!t->currentCell.atEnd() && t->currentCell.row < t->currentRow)
827 blockTagClosed =
true;
833 indent = t->lastIndent;
835 tables.resize(tables.size() - 1);
838 if (tables.isEmpty()) {
839 cursor = doc->rootFrame()->lastCursorPosition();
843 cursor = t->frame->lastCursorPosition();
844 else if (!t->currentCell.atEnd())
845 cursor = t->currentCell.cell().lastCursorPosition();
851 blockTagClosed =
false;
852 compressNextWhitespace = RemoveWhiteSpace;
857 if (t && !t->isTextFrame)
859 blockTagClosed =
true;
860 compressNextWhitespace = RemoveWhiteSpace;
867 lists.resize(lists.size() - 1);
869 blockTagClosed =
true;
873 compressNextWhitespace = RemoveWhiteSpace;
877 if (cursor.position() > 0) {
878 const QChar curChar = cursor.document()->characterAt(cursor.position() - 1);
879 if (!closedNode->children.isEmpty() && curChar != QChar::LineSeparator) {
880 blockTagClosed =
true;
891 blockTagClosed =
true;
894 if (closedNode->isBlock())
895 blockTagClosed =
true;
899 closedNode = &at(closedNode->parent);
903 return blockTagClosed;
911 QList<QTextLength> columnWidths;
913 int tableHeaderRowCount = 0;
915 rowNodes.reserve(at(tableNodeIdx).children.size());
916 for (
int row : at(tableNodeIdx).children) {
917 switch (at(row).id) {
924 for (
int potentialRow : at(row).children) {
925 if (at(potentialRow).id == Html_tr) {
926 rowNodes += potentialRow;
927 if (at(row).id == Html_thead)
928 ++tableHeaderRowCount;
936 QList<RowColSpanInfo> rowColSpans;
937 QList<RowColSpanInfo> rowColSpanForColumn;
939 int effectiveRow = 0;
940 for (
int row : std::as_const(rowNodes)) {
943 for (
int cell : at(row).children) {
944 if (at(cell).isTableCell()) {
946 while (colsInRow < rowColSpanForColumn.size()) {
947 const RowColSpanInfo &spanInfo = rowColSpanForColumn.at(colsInRow);
949 if (spanInfo.row + spanInfo.rowSpan > effectiveRow) {
950 Q_ASSERT(spanInfo.col == colsInRow);
951 colsInRow += spanInfo.colSpan;
956 const QTextHtmlParserNode &c = at(cell);
957 const int currentColumn = colsInRow;
958 colsInRow += c.tableCellColSpan;
960 RowColSpanInfo spanInfo;
961 spanInfo.row = effectiveRow;
962 spanInfo.col = currentColumn;
963 spanInfo.colSpan = c.tableCellColSpan;
964 spanInfo.rowSpan = c.tableCellRowSpan;
965 if (spanInfo.colSpan > 1 || spanInfo.rowSpan > 1)
966 rowColSpans.append(spanInfo);
968 columnWidths.resize(qMax(columnWidths.size(), colsInRow));
969 rowColSpanForColumn.resize(columnWidths.size());
970 for (
int i = currentColumn; i < currentColumn + c.tableCellColSpan; ++i) {
971 if (columnWidths.at(i).type() == QTextLength::VariableLength) {
972 QTextLength w = c.width;
973 if (c.tableCellColSpan > 1 && w.type() != QTextLength::VariableLength)
974 w = QTextLength(w.type(), w.value(100.) / c.tableCellColSpan);
977 rowColSpanForColumn[i] = spanInfo;
982 table.columns = qMax(table.columns, colsInRow);
986 table.rows = effectiveRow;
988 table.lastIndent = indent;
991 if (table.rows == 0 || table.columns == 0)
994 QTextFrameFormat fmt;
995 const QTextHtmlParserNode &node = at(tableNodeIdx);
997 if (!node.isTextFrame) {
998 QTextTableFormat tableFmt;
999 tableFmt.setCellSpacing(node.tableCellSpacing);
1000 tableFmt.setCellPadding(node.tableCellPadding);
1001 if (node.blockFormat.hasProperty(QTextFormat::BlockAlignment))
1002 tableFmt.setAlignment(node.blockFormat.alignment());
1003 tableFmt.setColumns(table.columns);
1004 tableFmt.setColumnWidthConstraints(columnWidths);
1005 tableFmt.setHeaderRowCount(tableHeaderRowCount);
1006 tableFmt.setBorderCollapse(node.borderCollapse);
1010 fmt.setTopMargin(topMargin(tableNodeIdx));
1011 fmt.setBottomMargin(bottomMargin(tableNodeIdx));
1012 fmt.setLeftMargin(leftMargin(tableNodeIdx)
1013 + table.lastIndent * 40
1015 fmt.setRightMargin(rightMargin(tableNodeIdx));
1018 if (qFuzzyCompare(fmt.leftMargin(), fmt.rightMargin())
1019 && qFuzzyCompare(fmt.leftMargin(), fmt.topMargin())
1020 && qFuzzyCompare(fmt.leftMargin(), fmt.bottomMargin()))
1021 fmt.setProperty(QTextFormat::FrameMargin, fmt.leftMargin());
1023 fmt.setBorderStyle(node.borderStyle);
1024 fmt.setBorderBrush(node.borderBrush);
1025 fmt.setBorder(node.tableBorder);
1026 fmt.setWidth(node.width);
1027 fmt.setHeight(node.height);
1028 if (node.blockFormat.hasProperty(QTextFormat::PageBreakPolicy))
1029 fmt.setPageBreakPolicy(node.blockFormat.pageBreakPolicy());
1031 if (node.blockFormat.hasProperty(QTextFormat::LayoutDirection))
1032 fmt.setLayoutDirection(node.blockFormat.layoutDirection());
1033 if (node.charFormat.background().style() != Qt::NoBrush)
1034 fmt.setBackground(node.charFormat.background());
1035 fmt.setPosition(QTextFrameFormat::Position(node.cssFloat));
1037 if (node.isTextFrame) {
1038 if (node.isRootFrame) {
1039 table.frame = cursor.currentFrame();
1040 table.frame->setFrameFormat(fmt);
1042 table.frame = cursor.insertFrame(fmt);
1044 table.isTextFrame =
true;
1046 const int oldPos = cursor.position();
1047 QTextTable *textTable = cursor.insertTable(table.rows, table.columns, fmt.toTableFormat());
1048 table.frame = textTable;
1050 for (
int i = 0; i < rowColSpans.size(); ++i) {
1051 const RowColSpanInfo &nfo = rowColSpans.at(i);
1052 textTable->mergeCells(nfo.row, nfo.col, nfo.rowSpan, nfo.colSpan);
1055 table.currentCell = TableCellIterator(textTable);
1056 cursor.setPosition(oldPos);
1063 QTextBlockFormat block;
1064 QTextCharFormat charFmt;
1065 bool modifiedBlockFormat =
true;
1066 bool modifiedCharFormat =
true;
1068 if (currentNode->isTableCell() && !tables.isEmpty()) {
1069 Table &t = tables.last();
1070 if (!t.isTextFrame && !t.currentCell.atEnd()) {
1071 QTextTableCell cell = t.currentCell.cell();
1072 if (cell.isValid()) {
1073 QTextTableCellFormat fmt = cell.format().toTableCellFormat();
1074 if (topPadding(currentNodeIdx) >= 0)
1075 fmt.setTopPadding(topPadding(currentNodeIdx));
1076 if (bottomPadding(currentNodeIdx) >= 0)
1077 fmt.setBottomPadding(bottomPadding(currentNodeIdx));
1078 if (leftPadding(currentNodeIdx) >= 0)
1079 fmt.setLeftPadding(leftPadding(currentNodeIdx));
1080 if (rightPadding(currentNodeIdx) >= 0)
1081 fmt.setRightPadding(rightPadding(currentNodeIdx));
1082#ifndef QT_NO_CSSPARSER
1083 if (tableCellBorder(currentNodeIdx, QCss::TopEdge) > 0)
1084 fmt.setTopBorder(tableCellBorder(currentNodeIdx, QCss::TopEdge));
1085 if (tableCellBorder(currentNodeIdx, QCss::RightEdge) > 0)
1086 fmt.setRightBorder(tableCellBorder(currentNodeIdx, QCss::RightEdge));
1087 if (tableCellBorder(currentNodeIdx, QCss::BottomEdge) > 0)
1088 fmt.setBottomBorder(tableCellBorder(currentNodeIdx, QCss::BottomEdge));
1089 if (tableCellBorder(currentNodeIdx, QCss::LeftEdge) > 0)
1090 fmt.setLeftBorder(tableCellBorder(currentNodeIdx, QCss::LeftEdge));
1091 if (tableCellBorderStyle(currentNodeIdx, QCss::TopEdge) != QTextFrameFormat::BorderStyle_None)
1092 fmt.setTopBorderStyle(tableCellBorderStyle(currentNodeIdx, QCss::TopEdge));
1093 if (tableCellBorderStyle(currentNodeIdx, QCss::RightEdge) != QTextFrameFormat::BorderStyle_None)
1094 fmt.setRightBorderStyle(tableCellBorderStyle(currentNodeIdx, QCss::RightEdge));
1095 if (tableCellBorderStyle(currentNodeIdx, QCss::BottomEdge) != QTextFrameFormat::BorderStyle_None)
1096 fmt.setBottomBorderStyle(tableCellBorderStyle(currentNodeIdx, QCss::BottomEdge));
1097 if (tableCellBorderStyle(currentNodeIdx, QCss::LeftEdge) != QTextFrameFormat::BorderStyle_None)
1098 fmt.setLeftBorderStyle(tableCellBorderStyle(currentNodeIdx, QCss::LeftEdge));
1099 if (tableCellBorderBrush(currentNodeIdx, QCss::TopEdge) != Qt::NoBrush)
1100 fmt.setTopBorderBrush(tableCellBorderBrush(currentNodeIdx, QCss::TopEdge));
1101 if (tableCellBorderBrush(currentNodeIdx, QCss::RightEdge) != Qt::NoBrush)
1102 fmt.setRightBorderBrush(tableCellBorderBrush(currentNodeIdx, QCss::RightEdge));
1103 if (tableCellBorderBrush(currentNodeIdx, QCss::BottomEdge) != Qt::NoBrush)
1104 fmt.setBottomBorderBrush(tableCellBorderBrush(currentNodeIdx, QCss::BottomEdge));
1105 if (tableCellBorderBrush(currentNodeIdx, QCss::LeftEdge) != Qt::NoBrush)
1106 fmt.setLeftBorderBrush(tableCellBorderBrush(currentNodeIdx, QCss::LeftEdge));
1109 cell.setFormat(fmt);
1111 cursor.setPosition(cell.firstPosition());
1115 compressNextWhitespace = RemoveWhiteSpace;
1117 if (currentNode->charFormat.background().style() != Qt::NoBrush) {
1118 charFmt.setBackground(currentNode->charFormat.background());
1119 cursor.mergeBlockCharFormat(charFmt);
1124 block = cursor.blockFormat();
1125 charFmt = cursor.blockCharFormat();
1126 modifiedBlockFormat =
false;
1127 modifiedCharFormat =
false;
1132 qreal tm = qreal(topMargin(currentNodeIdx));
1133 if (tm > block.topMargin()) {
1134 block.setTopMargin(tm);
1135 modifiedBlockFormat =
true;
1139 int bottomMargin =
this->bottomMargin(currentNodeIdx);
1143 const QTextHtmlParserNode *parentNode = currentNode->parent ? &at(currentNode->parent) :
nullptr;
1144 if ((currentNode->id == Html_li || currentNode->id == Html_dt || currentNode->id == Html_dd)
1146 && (parentNode->isListStart() || parentNode->id == Html_dl)
1147 && (parentNode->children.last() == currentNodeIdx)) {
1148 bottomMargin = qMax(bottomMargin,
this->bottomMargin(currentNode->parent));
1151 if (block.bottomMargin() != bottomMargin) {
1152 block.setBottomMargin(bottomMargin);
1153 modifiedBlockFormat =
true;
1157 const qreal lm = leftMargin(currentNodeIdx);
1158 const qreal rm = rightMargin(currentNodeIdx);
1160 if (block.leftMargin() != lm) {
1161 block.setLeftMargin(lm);
1162 modifiedBlockFormat =
true;
1164 if (block.rightMargin() != rm) {
1165 block.setRightMargin(rm);
1166 modifiedBlockFormat =
true;
1170 if (currentNode->id != Html_li
1174 || !lists.constLast().list
1175 || lists.constLast().list->itemNumber(cursor.block()) == -1
1178 block.setIndent(indent);
1179 modifiedBlockFormat =
true;
1183 block.setHeadingLevel(headingLevel);
1184 modifiedBlockFormat =
true;
1187 if (currentNode->blockFormat.propertyCount() > 0) {
1188 modifiedBlockFormat =
true;
1189 block.merge(currentNode->blockFormat);
1192 if (currentNode->charFormat.propertyCount() > 0) {
1193 modifiedCharFormat =
true;
1194 charFmt.merge(currentNode->charFormat);
1200 if (wsm == QTextHtmlParserNode::WhiteSpacePre
1201 || wsm == QTextHtmlParserNode::WhiteSpaceNoWrap) {
1202 block.setNonBreakableLines(
true);
1203 modifiedBlockFormat =
true;
1206 if (currentNode->charFormat.background().style() != Qt::NoBrush && !currentNode->isTableCell()) {
1207 block.setBackground(currentNode->charFormat.background());
1208 modifiedBlockFormat =
true;
1211 if (hasBlock && (!currentNode->isEmptyParagraph || forceBlockMerging)) {
1212 if (modifiedBlockFormat)
1213 cursor.setBlockFormat(block);
1214 if (modifiedCharFormat)
1215 cursor.setBlockCharFormat(charFmt);
1217 if (currentNodeIdx == 1 && cursor.position() == 0 && currentNode->isEmptyParagraph) {
1218 cursor.setBlockFormat(block);
1219 cursor.setBlockCharFormat(charFmt);
1221 appendBlock(block, charFmt);
1225 if (currentNode->userState != -1)
1226 cursor.block().setUserState(currentNode->userState);
1228 if (currentNode->id == Html_li && !lists.isEmpty()) {
1229 List &l = lists.last();
1231 l.list->add(cursor.block());
1233 l.list = cursor.createList(l.format);
1234 const qreal listTopMargin = topMargin(l.listNode);
1235 if (listTopMargin > block.topMargin()) {
1236 block.setTopMargin(listTopMargin);
1237 cursor.mergeBlockFormat(block);
1241 QTextBlockFormat fmt;
1242 fmt.setIndent(currentNode->blockFormat.indent());
1243 cursor.mergeBlockFormat(fmt);
1247 forceBlockMerging =
false;
1248 if (currentNode->id == Html_body || currentNode->id == Html_html)
1249 forceBlockMerging =
true;
1251 if (currentNode->isEmptyParagraph) {
1253 return ContinueWithNextSibling;
1257 blockTagClosed =
false;
1258 return ContinueWithCurrentNode;
1261void QTextHtmlImporter::appendBlock(
const QTextBlockFormat &format, QTextCharFormat charFmt)
1263 if (!namedAnchors.isEmpty()) {
1264 charFmt.setAnchor(
true);
1265 charFmt.setAnchorNames(namedAnchors);
1266 namedAnchors.clear();
1269 cursor.insertBlock(format, charFmt);
1271 if (wsm != QTextHtmlParserNode::WhiteSpacePre && wsm != QTextHtmlParserNode::WhiteSpacePreWrap)
1272 compressNextWhitespace = RemoveWhiteSpace;
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1288QTextDocumentFragment QTextDocumentFragment::fromHtml(
const QString &html,
const QTextDocument *resourceProvider)
1290 QTextDocumentFragment res;
1291 res.d =
new QTextDocumentFragmentPrivate;
1293 QTextHtmlImporter importer(res.d->doc, html, QTextHtmlImporter::ImportToFragment, resourceProvider);
1300#if QT_CONFIG(textmarkdownreader)
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315QTextDocumentFragment QTextDocumentFragment::fromMarkdown(
const QString &markdown, QTextDocument::MarkdownFeatures features)
1317 QTextDocumentFragment res;
1318 res.d =
new QTextDocumentFragmentPrivate;
1320 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)