Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/**
* Copyright IBM Corp. 2025
* Copyright IBM Corp. 2025, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import { defaultFilterRows } from '../filter';

describe('defaultFilterRows', () => {
Expand Down Expand Up @@ -58,6 +59,98 @@ describe('defaultFilterRows', () => {
expect(result).toEqual([]);
});

it.each([
['null', null],
['undefined', undefined],
])('should not match the text %s in an empty cell', (inputValue, value) => {
const result = defaultFilterRows({
...defaultOptions,
rowIds: ['1'],
cellsById: {
...cellsById,
'1-name': { value },
},
inputValue,
});

expect(result).toEqual([]);
});

it('should not match the string form of objects and React elements', () => {
const result = defaultFilterRows({
...defaultOptions,
rowIds: ['1', '2'],
cellsById: {
...cellsById,
'1-name': { value: { first: 'Alice' } },
'2-name': { value: <a href="#bob">Bob</a> },
},
inputValue: 'object',
});

expect(result).toEqual([]);
});

it('should not throw on objects without a prototype', () => {
const result = defaultFilterRows({
...defaultOptions,
rowIds: ['1'],
cellsById: {
...cellsById,
'1-name': { value: Object.create(null) },
},
inputValue: 'object',
});

expect(result).toEqual([]);
});

it('should not call the string conversion of objects', () => {
const toString = jest.fn(() => 'Allison');

const result = defaultFilterRows({
...defaultOptions,
rowIds: ['1', '2'],
cellsById: {
...cellsById,
'1-name': { value: { toString } },
'2-name': { value: ['Alice', 'Allison'] },
},
inputValue: 'allison',
});

expect(result).toEqual([]);
expect(toString).not.toHaveBeenCalled();
});

it('should match bigint values', () => {
const result = defaultFilterRows({
...defaultOptions,
rowIds: ['1'],
cellsById: {
...cellsById,
'1-age': { value: BigInt(42) },
},
inputValue: '42',
});

expect(result).toEqual(['1']);
});

it('should match the number zero', () => {
const result = defaultFilterRows({
...defaultOptions,
rowIds: ['1'],
cellsById: {
...cellsById,
'1-age': { value: 0 },
},
inputValue: '0',
});

expect(result).toEqual(['1']);
});

it('should perform a case-insensitive search', () => {
const result = defaultFilterRows({
...defaultOptions,
Expand Down Expand Up @@ -86,6 +179,17 @@ describe('defaultFilterRows', () => {
expect(result).toEqual([]);
});

it('should not throw when no cell in a row matches and a cell is missing', () => {
const result = defaultFilterRows({
...defaultOptions,
rowIds: ['1'],
cellsById: { '1-name': { value: 'Alice' } },
inputValue: 'xyz',
});

expect(result).toEqual([]);
});

it('should handle a missing cell gracefully', () => {
const incompleteCellsById = { '1-name': { value: 'Alice' } };

Expand Down
28 changes: 22 additions & 6 deletions packages/react/src/components/DataTable/tools/filter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
/**
* Copyright IBM Corp. 2016, 2025
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* Converts a cell value to searchable text. Only primitive values with an
* unambiguous text form are supported, so no user-defined or inherited
* `toString` method is ever invoked. Returns `null` for everything else.
*/
const getFilterableText = (value: unknown): string | null => {
if (
typeof value === 'string' ||
typeof value === 'number' ||
typeof value === 'bigint'
) {
return String(value);
}

return null;
};

@adamalston adamalston Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the value be converted to a string once instead? Cross realm plain objects can still pass this check, and accessing toString here and again during String(value) can invoke a getter twice. String conversion can also throw an error.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Execution_model#realms

Edit: Hmm, even if the value is converted to a string once, the conversion can still invoke inherited methods. Could the default filter limit string conversion to explicitly supported types instead of using prototype checks?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. It now converts only strings, numbers and bigints to text and ignores everything else, so no toString is ever invoked (officialasishkumar@b04de82).


/**
* Filters row IDs based on whether any of the cell values in the row include
* the input value as a substring. Boolean cell values are ignored.
* the input value as a substring. Cell values without filterable text are
* ignored.
*/
export const defaultFilterRows = ({
rowIds,
Expand All @@ -34,11 +52,9 @@ export const defaultFilterRows = ({
return rowIds.filter((rowId) =>
headers.some(({ key }) => {
const cellId = getCellId(rowId, key);
const cell = cellsById[cellId];

if (typeof cell.value === 'boolean') return false;
const text = getFilterableText(cellsById[cellId]?.value);

return String(cell.value).toLowerCase().includes(normalizedInput);
return text?.toLowerCase().includes(normalizedInput) ?? false;
})
);
};
Loading