Skip to content

Commit 3167f5f

Browse files
authored
fix: applyHtmlCode util should always clear targeted native element (#926)
- when calling `applyHtmlCode` when a native element, we use `appendChild` but of course that will append to what already exist, however in the case of the util we should always clear the target element first since that is what we do when passing html string (assigning with `innerHTML` will overwrite any previous content) - this was caught in one of the example when collapsing groups, we ended up seeing duplicate groups and this came up after converting SlickGroupItemMetadataProvider Formatter to return native element
1 parent 6ee5e8e commit 3167f5f

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/slick.grid.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,17 +518,21 @@ export class SlickGrid<TData = any, C extends Column<TData> = Column<TData>, O e
518518

519519
/**
520520
* Apply HTML code by 3 different ways depending on what is provided as input and what options are enabled.
521-
* 1. value is an HTMLElement, then simply append the HTML to the target element.
521+
* 1. value is an HTMLElement or DocumentFragment, then first empty the target and simply append the HTML to the target element.
522522
* 2. value is string and `enableHtmlRendering` is enabled, then use `target.innerHTML = value;`
523523
* 3. value is string and `enableHtmlRendering` is disabled, then use `target.textContent = value;`
524524
* @param target - target element to apply to
525525
* @param val - input value can be either a string or an HTMLElement
526526
*/
527-
applyHtmlCode(target: HTMLElement, val: string | HTMLElement | DocumentFragment) {
527+
applyHtmlCode(target: HTMLElement, val: string | HTMLElement | DocumentFragment, emptyTarget = true) {
528528
if (target) {
529529
if (val instanceof HTMLElement || val instanceof DocumentFragment) {
530+
// first empty target and then append new HTML element
531+
if (emptyTarget) {
532+
Utils.emptyElement(target);
533+
}
530534
target.appendChild(val);
531-
} else if (typeof val === 'string') {
535+
} else {
532536
if (this._options.enableHtmlRendering) {
533537
target.innerHTML = this.sanitizeHtmlString(val as string);
534538
} else {

0 commit comments

Comments
 (0)