diff --git a/src/widgets/accessible/qaccessiblewidgets.cpp b/src/widgets/accessible/qaccessiblewidgets.cpp index f459affd07..6e1e3df89a 100644 --- a/src/widgets/accessible/qaccessiblewidgets.cpp +++ b/src/widgets/accessible/qaccessiblewidgets.cpp @@ -727,19 +727,31 @@ QString QAccessibleTextWidget::attributes(int offset, int *startOffset, int *end int blockEnd = blockStart + block.length(); QTextBlock::iterator iter = block.begin(); - while (!iter.fragment().contains(offset)) + int lastFragmentIndex = blockStart; + while (!iter.atEnd()) { + QTextFragment f = iter.fragment(); + if (f.contains(offset)) + break; + lastFragmentIndex = f.position() + f.length(); ++iter; + } - QTextFragment fragment = iter.fragment(); - int pos = fragment.position(); - - // text block and fragment may overlap, use the smallest common range - *startOffset = qMax(pos, blockStart); + QTextCharFormat charFormat; + if (!iter.atEnd()) { + QTextFragment fragment = iter.fragment(); + charFormat = fragment.charFormat(); + int pos = fragment.position(); + // text block and fragment may overlap, use the smallest common range + *startOffset = qMax(pos, blockStart); + *endOffset = qMin(pos + fragment.length(), blockEnd); + } else { + charFormat = block.charFormat(); + *startOffset = lastFragmentIndex; + *endOffset = blockEnd; + } Q_ASSERT(*startOffset <= offset); - *endOffset = qMin(pos + fragment.length(), blockEnd); Q_ASSERT(*endOffset >= offset); - QTextCharFormat charFormat = fragment.charFormat(); QTextBlockFormat blockFormat = cursor.blockFormat(); QMap attrs; diff --git a/tests/auto/other/qaccessibility/tst_qaccessibility.cpp b/tests/auto/other/qaccessibility/tst_qaccessibility.cpp index e59509a944..6be339d16b 100644 --- a/tests/auto/other/qaccessibility/tst_qaccessibility.cpp +++ b/tests/auto/other/qaccessibility/tst_qaccessibility.cpp @@ -733,6 +733,7 @@ void tst_QAccessibility::textAttributes_data() QTest::newRow("left right left aligned 10") << leftRightLeftAligned << 10 << 10 << 11 << rightAlign; QTest::newRow("left right left aligned 11") << leftRightLeftAligned << 11 << 11 << 15 << bold; QTest::newRow("left right left aligned 15") << leftRightLeftAligned << 15 << 11 << 15 << bold; + QTest::newRow("empty with no fragments") << QString::fromLatin1("\n\n\n\n") << 0 << 0 << 1 << defaults; } void tst_QAccessibility::textAttributes()