Closed Bug 188645 Opened 22 years ago Closed 22 years ago

insertRow(-1) work as insertRow(rowCount-1) for HTMLTableElement.cpp

Categories

(Core :: DOM: Core & HTML, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: croo, Assigned: jst)

Details

Attachments

(2 files, 1 obsolete file)

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3b) Gecko/20030110
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.3b) Gecko/20030110

When I wrote answer to bug 188635, I notice, that IncertRow(-1) add row not to
the end of table, but as rowCount-1. Look at testcase.

I check it, and source of this behaviour is error in nsHTMLTableElement.cpp, in
line 801:

800 // the index is greater than the number of rows, so just append
801       if ((0 <= aIndex) && (PRInt32(rowCount) <= aIndex)) {
802         rv = parent->AppendChild(newRowNode, getter_AddRefs(retChild));
803       }
804       else
805       {
806         // insert the new row before the reference row we found above
807         rv = parent->InsertBefore(newRowNode, refRow,
808                                   getter_AddRefs(retChild));
809       }

This If must distinguish append and insert cases, but it works only when
aIndex>=rowCount. If aIndex==-1, then new row inserted to a refIndex position,
and RefIndex=rowCount-1 for aIndex==-1 (line 778).

So I suggest to change line 801 to

801       if (((0 <= aIndex) && (PRInt32(rowCount) <= aIndex)) || (aIndex == -1)) {

Reproducible: Always

Steps to Reproduce:
1. Open testcase
2. Play with buttons -- they left do insertRow(-1), right insertRow(rowCount)
3. Notice the difference

Actual Results:  
insertRow(-1) is equal to insertRow(rowCount-1) 

Expected Results:  
insertRow(-1) should be equal to insertRow(rowCount)
Hardware: Other → All
Attached patch Proposed patch (obsolete) — Splinter Review
Comment on attachment 111290 [details] [diff] [review]
Proposed patch

>       // the index is greater than the number of rows, so just append
>-      if ((0 <= aIndex) && (PRInt32(rowCount) <= aIndex)) {
>+      if (((0 <= aIndex) && (PRInt32(rowCount) <= aIndex)) 
>+          || (aIndex == -1)) {

Actually, you are guaranteed (by the spec) that by the time you get here, -1 <=
aIndex <= rowCount.  Note that the comment is wrong.  the index CANNOT be
greater than the number of rows.  If it is, we throw an exception (the DOM
specification demands it).  The spec[1] says "If index is -1 or equal to the
number of rows, the new row is appended.".  Please update the comment to copy
the spec, as well as make the code do that.

Note, that I know you are not at fault for the old code, but if you are going
to be complaining about spec stuff, please make it be accurate and correct. 
:-)


[1]
http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/html.html#ID-39872903
Attachment #111290 - Flags: review-
You want the following:

      // If index is -1 or equal to the number of rows, the new row
      // is appended.
      if (aIndex == -1 || PRUint32(aIndex) == rowCount) {
Status: UNCONFIRMED → NEW
Ever confirmed: true
Oops. I see that if aIndex > rowCount, than old code does not made an exeption,
simply add a row. Is it correct bahaviour? 

We do throw the exception.  See line 766 (and also 754).  So again, by the time
you get there, you know for a fact that it is between -1 and the rowCount,
inclusively.  All you need to do is check against the two endpoints.
Sorry, I mean that in case: rowCount=10, aIndex= -2, than exeption is not
thrown, as I see. Or I wrong? Because exeption thrown now 

((PRUint32)aIndex > rowCount && aIndex != -1)

When (aIndex > rowCount or -aIndex > rowCount)  and (aIndex != -1).
correct would be ((PRUint32)aIndex > rowCount || aIndex < -1)

Or I wrong?
Hmm. I wrong again :-). I incorrect calculate unsigned int. This exeption is not
throu if aIndex=-10 and rowCound=MaxInt-1, bit this is not possible cause.
Patch, implement comment #4 proposal.
Attachment #111290 - Attachment is obsolete: true
Comment on attachment 111296 [details] [diff] [review]
Proposed patch v 1.1

Remove the extra parens about the two parts of the condition, and sr=bzbarsky.
Attachment #111296 - Flags: superreview?(bzbarsky) → superreview+
Sorry, you little misguide me. Should I remove this parens?
Checked in.
Status: NEW → RESOLVED
Closed: 22 years ago
Resolution: --- → FIXED
Component: DOM: HTML → DOM: Core & HTML
QA Contact: stummala → general
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: