Issue 80762

Summary: XEnumerationAccess of TextRange inside TextTable returns incorrect enumeration
Product: App Dev Reporter: knipes <xeridanus>
Component: apiAssignee: AOO issues mailing list <issues>
Status: CONFIRMED --- QA Contact:
Severity: Trivial    
Priority: P3 CC: chandlerb, debyld, issues, j.tronel, linda_octa, mathias, mux2005
Version: 3.3.0 or older (OOo)   
Target Milestone: ---   
Hardware: All   
OS: All   
Issue Type: DEFECT Latest Confirmation in: ---
Developer Difficulty: ---
Attachments:
Description Flags
Demonstrates that TextRange inside TextTable always returns the whole table when enumerating.
none
Demonstrates problem and workarround none

Description knipes 2007-08-17 01:39:42 UTC
the code below doesn't work as it should. when used within a table it also gets
all the text in the next cell in the table. I'm using it to apply bold (as well
as other character attributes) and it works perfectly fine outside of tables.

oVC = thisComponent.currentController.ViewCursor
oTextCursor = oVC.Text.createTextCursorByRange(oVC)
If oVC.isCollapsed() Then
	oTextCursor.gotoStartOfWord (False)
	oTextCursor.gotoEndOfWord (True)
End If

I've put this under the most recent version that I've observed it in (WinXP). I
have also observed it on a Mac.
Comment 1 michael.ruess 2007-08-17 08:58:07 UTC
MRU->JSK: could you please have a look? Thanks!
Comment 2 joerg.skottke 2007-11-28 13:40:07 UTC
to cn
Comment 3 chne 2008-01-16 09:04:18 UTC
cn: I can not reproduce this behavior in ooh680_m3, which will be the OOo 2.4
Comment 4 knipes 2008-07-01 06:02:55 UTC
whoops. i didn't trace it properly. the code as a whole is what does it. sorry.

subToggleCharacterStyles("i-code",
array("CharFontCharSet","CharFontFamily","CharFontName","CharFontPitch"))

...

Sub subToggleCharacterStyles(sStyle, mResetProps)
oCurSelection = thisComponent.getCurrentSelection()
If oCurSelection.supportsService("com.sun.star.text.TextRanges") Then
	nCount = oCurSelection.Count
	If nCount = 1 Then
		'If nothing selected then select current word
		oVC = thisComponent.CurrentController.ViewCursor
		oTextCursor = oVC.Text.createTextCursorByRange(oVC)
		If oVC.isCollapsed() Then
			oTextCursor.gotoStartOfWord (False)
			oTextCursor.gotoEndOfWord (True)
		End If
		'oTextRange = oCurSelection.getByIndex(0)
		subProcessTextRange(oTextCursor, sStyle, mResetProps)
	Else
		For i = 1 To nCount - 1
			oTextRange = oCurSelection.getByIndex(i)
			subProcessTextRange(oTextRange, sStyle, mResetProps)
		Next
	End If
End If
End Sub


Sub subProcessTextRange(oTextRange, sStyle, mResetProps)
oTextElementEnum = oTextRange.createEnumeration
While oTextElementEnum.hasMoreElements()
	oTextElement = oTextElementEnum.nextElement()
	If oTextElement.supportsService("com.sun.star.text.Paragraph") Then
		subProcessParagraph(oTextElement, sStyle, mResetProps)
	Else	'if oTextElement.supportsService("com.sun.star.text.TextTable") then
		subProcessTable(oTextElement, sStyle, mResetProps)
	End If
Wend
End Sub


Sub subProcessTable(oTextElement, sStyle, mResetProps)
mCells = oTextElement.getCellNames()
For i = 0 To UBound(mCells)
	oCell = oTextElement.getCellByName(mCells(i))
	subProcessTextRange(oCell.text, sStyle, mResetProps)
Next
End Sub


Sub subProcessParagraph(oTextElement, sStyle, mResetProps)
oCharStyles = thisComponent.styleFamilies.getByName("CharacterStyles")
If Not oCharStyles.hasByName(sStyle) Then
	subCreateMissingStyle (sStyle)
End If
oPortionEnum = oTextElement.createEnumeration
While oPortionEnum.hasMoreElements()
	oPortion = oPortionEnum.nextElement()
	subResetProperties(oPortion, mResetProps)
	mCharStyleNames = oPortion.getPropertyValue("CharStyleNames")
	sCharStyleNames = ""
	If IsEmpty(mCharStyleNames) Then
		sCharStyleNames = sStyle
	ElseIf UBound(mCharStyleNames) < 0 Then
		sCharStyleNames = sStyle
	Else
		sCharStyleNames = Join(mCharStyleNames, ",") & ","
		nLoc = InStr(sCharStyleNames, sStyle & ",")
		If nLoc > 0 Then
			sCharStyleNames = Left(sCharStyleNames, nLoc - 1) & Right(sCharStyleNames,
Len(sCharStyleNames) - (nLoc + Len(sStyle)))
			If Right(sCharStyleNames, 1) = "," Then sCharStyleNames =
Left(sCharStyleNames, Len(sCharStyleNames) - 1)
		Else
			sCharStyleNames = sCharStyleNames & sStyle
		End If
	End If
	mCharStyleNames = Split(sCharStyleNames, ",")
	If IsEmpty(mCharStyleNames) Then
		oPortion.setPropertyToDefault ("CharStyleNames")
	ElseIf UBound(mCharStyleNames) < 0 Then
		oPortion.setPropertyToDefault ("CharStyleNames")
	Else
		oPortion.setPropertyToDefault ("CharStyleNames")
		oPortion.setPropertyValue("CharStyleNames", mCharStyleNames)
	End If
Wend
End Sub


Sub subResetProperties(oPortion, mResetProps)
For i = 0 To UBound(mResetProps)
	oPortion.setPropertyToDefault (mResetProps(i))
Next
End Sub
Comment 5 mux2005 2008-07-03 15:41:38 UTC
The summary is misleading. This bug is unrelated to createTextCursorByRange. I
suggest to change the summary to "XEnumerationAccess of TextRange inside
TextTable returns incorrect enumeration". 

The general issue is that any TextRange inside a TextTable will always return
(via its XEnumerationAccess interface) an enumeration that contains only the
complete table rather than just the paragraphs contained in the range. This is
not limited to cursors. It can also be reproduced with a Bookmark's anchor text
range.
Comment 6 knipes 2008-07-07 03:06:49 UTC
changed summary.
Comment 7 mux2005 2008-07-07 09:11:57 UTC
Created attachment 54973 [details]
Demonstrates that TextRange inside TextTable always returns the whole table when enumerating.
Comment 8 mux2005 2008-07-07 09:20:35 UTC
I've attached a simpler test case for the issue. The document contains 2
bookmarks, "test" and "test1". The first is inside the table (on the text
"bookmark"), the 2nd is outside of the table. If you press the button push me, a
macro spits out the getString() value of the corresponding text ranges as well
as the implementation name of the 1st element of the corresponding enumeration.
The macro shows that the text ranges do know what they contain (the getString()
value is correct in both cases), but the textrange inside the table returns the
complete table when enumerating.
Comment 9 chne 2008-07-07 09:26:51 UTC
cn->tl: one for you
Comment 10 leguff 2009-07-02 21:07:32 UTC
Created attachment 63335 [details]
Demonstrates problem and workarround
Comment 11 leguff 2009-07-02 21:12:08 UTC
Maybe there is a connection between this problem and Issue 98552?
Comment 12 j.tronel 2013-11-21 01:32:33 UTC
Hi,

Stumbled on this, as I was building a macro to reformat note anchors (i.e. between brackets or parentheses, with leading space or not...)

Not likely to be solved soon, is it ? And why is it impossible to vote ?