-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [1708] Add support for insertRow and deleteRow to HTMLTableSect…
…ionElement (#1709)
- Loading branch information
1 parent
a72b016
commit df84257
Showing
3 changed files
with
196 additions
and
1 deletion.
There are no files selected for viewing
83 changes: 82 additions & 1 deletion
83
packages/happy-dom/src/nodes/html-table-section-element/HTMLTableSectionElement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,88 @@ | ||
import QuerySelector from '../../query-selector/QuerySelector.js'; | ||
import * as PropertySymbol from '../../PropertySymbol.js'; | ||
import HTMLElement from '../html-element/HTMLElement.js'; | ||
import HTMLTableRowElement from '../html-table-row-element/HTMLTableRowElement.js'; | ||
import DOMExceptionNameEnum from '../../exception/DOMExceptionNameEnum.js'; | ||
/** | ||
* HTMLTableSectionElement | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableSectionElement | ||
*/ | ||
export default class HTMLTableSectionElement extends HTMLElement {} | ||
export default class HTMLTableSectionElement extends HTMLElement { | ||
/** | ||
* Returns an HTMLTableRowElement representing a new row of the table. It inserts it in the rows collection immediately before the <tr> element at the given index position. If the index is -1, the new row is appended to the collection. If the index is smaller than -1 or greater than the number of rows in the collection, a DOMException with the value IndexSizeError is raised. | ||
* | ||
* @param [index] Index. | ||
* @returns Row. | ||
*/ | ||
public insertRow(index: number = -1): HTMLTableRowElement { | ||
if (typeof index !== 'number') { | ||
index = -1; | ||
} | ||
|
||
const rows = QuerySelector.querySelectorAll(this, 'tr')[PropertySymbol.items]; | ||
|
||
if (index < -1) { | ||
throw new this[PropertySymbol.window].DOMException( | ||
`Failed to execute 'insertRow' on 'HTMLTableSectionElement': The index provided (${index}) is less than -1.`, | ||
DOMExceptionNameEnum.indexSizeError | ||
); | ||
} | ||
|
||
if (index > rows.length) { | ||
throw new this[PropertySymbol.window].DOMException( | ||
`Failed to execute 'insertRow' on 'HTMLTableSectionElement': The index provided (${index}) is greater than the number of rows (${rows.length}).`, | ||
DOMExceptionNameEnum.indexSizeError | ||
); | ||
} | ||
|
||
const row = this[PropertySymbol.ownerDocument].createElement('tr'); | ||
|
||
if (index === -1 || index === rows.length) { | ||
this.appendChild(row); | ||
} else { | ||
this.insertBefore(row, rows[index]); | ||
} | ||
|
||
return row; | ||
} | ||
|
||
/** | ||
* Removes the row corresponding to the index given in parameter. If the index value is -1 the last row is removed; if it is smaller than -1 or greater than the amount of rows in the collection, a DOMException with the value IndexSizeError is raised. | ||
* | ||
* @param index Index. | ||
*/ | ||
public deleteRow(index: number): void { | ||
if (arguments.length === 0) { | ||
throw new this[PropertySymbol.window].TypeError( | ||
"Failed to execute 'deleteRow' on 'HTMLTableSectionElement': 1 argument required, but only 0 present." | ||
); | ||
} | ||
|
||
if (typeof index !== 'number') { | ||
index = -1; | ||
} | ||
|
||
if (index < -1) { | ||
throw new this[PropertySymbol.window].DOMException( | ||
`Failed to execute 'deleteRow' on 'HTMLTableSectionElement': The index provided (${index}) is less than -1.`, | ||
DOMExceptionNameEnum.indexSizeError | ||
); | ||
} | ||
|
||
const rows = QuerySelector.querySelectorAll(this, 'tr')[PropertySymbol.items]; | ||
|
||
if (index >= rows.length) { | ||
throw new this[PropertySymbol.window].DOMException( | ||
`Failed to execute 'deleteRow' on 'HTMLTableSectionElement': The index provided (${index}) is greater than the number of rows in the table (${rows.length}).`, | ||
DOMExceptionNameEnum.indexSizeError | ||
); | ||
} | ||
|
||
if (index === -1) { | ||
index = rows.length - 1; | ||
} | ||
|
||
rows[index].remove(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters