This project has moved and is read-only. For the latest updates, please go here.

Bug in Paragraph.cs GetElementTextLength()

Aug 12, 2009 at 5:24 PM

I found a bug in this method:

        static internal int GetElementTextLength(XElement run)
        {
            int count = 0;
            
            if (run == null)
                return count;

            foreach (var d in run.Descendants())
            {
                switch (d.Name.LocalName)
                {
                    case "tab": goto case "br"; break;
                    case "br": count++; break;
                    case "t": goto case "delText";
                    case "delText": count += d.Value.Length; break;
                    default: break;
                }
            }
            return count;
        }

In the paragraph XML, you can have something like this:

<w:p>
    <w:pPr>
        <w:tabs>
            <w:tab w:val="left" w:pos="360" />
            <w:tab w:val="left" w:pos="7200" />
        </w:tabs>
    </w:pPr>
    <w:r>
        <w:rPr>
            <w:b />
            <w:sz w:val="24" />
            <w:szCs w:val="24" />
        </w:rPr>
        <w:tab />
        <w:t xml:space="preserve">LUMP SUM PRICE: $0.00 TAX EXEMPT</w:t>
    </w:r>
</w:p>

The bug happens when you have a paragraph like this one that has tabs defined in the <w:pPr> section.  The code sees an element called "tab" and thinks that it's a tab in the paragraph when it's really a tab stop defined in the paragraph.  The method should probably look something like this:

        static internal int GetElementTextLength(XElement run)
        {
            int count = 0;
            
            if (run == null)
                return count;

            foreach (var d in run.Descendants())
            {
                switch (d.Name.LocalName)
                {
                    case "tab":
                        if (d.Parent.Name.LocalName != "tabs")
                            count++;
                        break;
                    case "br": count++; break;
                    case "t": goto case "delText";
                    case "delText": count += d.Value.Length; break;
                    default: break;
                }
            }
            return count;
        }

- 
Aug 12, 2009 at 6:02 PM
Hi krugs525,

this is an excellent find. Congratulations by the way, you are the first person to follow a bug into the source code and then post a solution. Thank you very much for this effort.

I have added your fix to DocX version 1.0.0.7 which I am currently working on.

thank you again,
Cathal