-
Notifications
You must be signed in to change notification settings - Fork 17.2k
fix(plugin-chart-ag-grid-table): use display text for filter and sort on HTML cells #39885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fitzee
wants to merge
4
commits into
apache:master
Choose a base branch
from
fitzee:fitzee/aggrid-html-text-filter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0091d76
fix(plugin-chart-ag-grid-table): use display text for filter and sort…
fitzee f91fa87
fix(plugin-chart-ag-grid-table): preserve default string ordering and…
fitzee dc4a3bf
fix(plugin-chart-ag-grid-table): memoize HTML stripping and gate to c…
fitzee ab6c9e3
fix(plugin-chart-ag-grid-table): cache normalized comparator value pe…
fitzee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
...t-frontend/plugins/plugin-chart-ag-grid-table/src/utils/htmlTextFilterValueGetter.test.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import { ValueGetterParams } from '@superset-ui/core/components/ThemedAgGridReact'; | ||
| import htmlTextFilterValueGetter, { | ||
| htmlTextComparator, | ||
| } from './htmlTextFilterValueGetter'; | ||
|
|
||
| const makeParams = (value: unknown): ValueGetterParams => | ||
| ({ | ||
| data: { foo: value }, | ||
| colDef: { field: 'foo' }, | ||
| }) as unknown as ValueGetterParams; | ||
|
|
||
| test('htmlTextFilterValueGetter extracts visible text from HTML anchor', () => { | ||
| expect( | ||
| htmlTextFilterValueGetter( | ||
| makeParams( | ||
| '<a href="https://jira.example.com/123/S18_3232">S18_3232</a>', | ||
| ), | ||
| ), | ||
| ).toBe('S18_3232'); | ||
| }); | ||
|
|
||
| test('htmlTextFilterValueGetter strips nested HTML markup', () => { | ||
| expect( | ||
| htmlTextFilterValueGetter( | ||
| makeParams('<div><strong>Hello</strong> <em>World</em></div>'), | ||
| ), | ||
| ).toBe('Hello World'); | ||
| }); | ||
|
|
||
| test('htmlTextFilterValueGetter passes plain strings through', () => { | ||
| expect(htmlTextFilterValueGetter(makeParams('plain value'))).toBe( | ||
| 'plain value', | ||
| ); | ||
| }); | ||
|
|
||
| test('htmlTextFilterValueGetter passes non-string values through', () => { | ||
| expect(htmlTextFilterValueGetter(makeParams(42))).toBe(42); | ||
| expect(htmlTextFilterValueGetter(makeParams(null))).toBeNull(); | ||
| expect(htmlTextFilterValueGetter(makeParams(undefined))).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('htmlTextComparator orders by visible text, not raw HTML', () => { | ||
| // URL prefixes (zzz vs bbb) would flip the order under raw-HTML sort, | ||
| // but the visible labels (S700_4002 vs S72_3212) sort the other way. | ||
| const left = '<a href="https://jira.example.com/zzz/S700_4002">S700_4002</a>'; | ||
| const right = '<a href="https://jira.example.com/bbb/S72_3212">S72_3212</a>'; | ||
| expect(htmlTextComparator(left, right)).toBeLessThan(0); | ||
| }); | ||
|
|
||
| test('htmlTextComparator handles nulls and numbers', () => { | ||
| expect(htmlTextComparator(null, null)).toBe(0); | ||
| expect(htmlTextComparator(null, 'x')).toBeLessThan(0); | ||
| expect(htmlTextComparator('x', null)).toBeGreaterThan(0); | ||
| expect(htmlTextComparator(1, 2)).toBeLessThan(0); | ||
| expect(htmlTextComparator(2, 1)).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| test('htmlTextComparator preserves default codepoint ordering for plain strings', () => { | ||
| // AG Grid's default string comparator orders by codepoint, so 'Z' (90) | ||
| // sorts before 'a' (97). A locale-aware comparator would flip this — | ||
| // verify we match the default so plain string columns are unaffected. | ||
| expect(htmlTextComparator('Z', 'a')).toBeLessThan(0); | ||
| expect(htmlTextComparator('a', 'Z')).toBeGreaterThan(0); | ||
| expect(htmlTextComparator('apple', 'banana')).toBeLessThan(0); | ||
| }); |
74 changes: 74 additions & 0 deletions
74
superset-frontend/plugins/plugin-chart-ag-grid-table/src/utils/htmlTextFilterValueGetter.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import { isProbablyHTML, sanitizeHtml } from '@superset-ui/core'; | ||
| import { ValueGetterParams } from '@superset-ui/core/components/ThemedAgGridReact'; | ||
|
|
||
| const stripHtmlToText = (html: string): string => { | ||
| const doc = new DOMParser().parseFromString(sanitizeHtml(html), 'text/html'); | ||
| return (doc.body.textContent || '').trim(); | ||
| }; | ||
|
|
||
| // Cache the comparator-ready form per raw string. Both the HTML-detection | ||
| // step (`isProbablyHTML`, which itself invokes DOMParser for HTML-looking | ||
| // values) and the extraction step (`stripHtmlToText`, also DOMParser) are | ||
| // expensive; sort runs `O(n log n)` comparator calls against the same set | ||
| // of cell values. Memoizing the combined detection + extraction means each | ||
| // unique cell value pays the cost once per session. Module-level scope; | ||
| // bounded by the count of unique string cell values seen. | ||
| const comparableTextCache = new Map<string, string>(); | ||
|
|
||
| const toComparableText = (raw: string): string => { | ||
| const cached = comparableTextCache.get(raw); | ||
| if (cached !== undefined) return cached; | ||
| const normalized = isProbablyHTML(raw) ? stripHtmlToText(raw) : raw; | ||
| comparableTextCache.set(raw, normalized); | ||
| return normalized; | ||
| }; | ||
|
|
||
| /** | ||
| * Returns the visible-text representation of an HTML cell value so AG Grid | ||
| * filters and sort operate on what the user sees, not the underlying markup. | ||
| * Pass-through for non-HTML values. | ||
| */ | ||
| const htmlTextFilterValueGetter = (params: ValueGetterParams) => { | ||
| const raw = params.data?.[params.colDef.field as string]; | ||
| return typeof raw === 'string' ? toComparableText(raw) : raw; | ||
| }; | ||
|
|
||
| /** | ||
| * Comparator that mirrors AG Grid's default string comparator (codepoint | ||
| * order, nulls first), but extracts visible text from HTML values first | ||
| * so HTML cells sort by their displayed label. Plain (non-HTML) values | ||
| * pass through unchanged, preserving default ordering — e.g. 'Z' still | ||
| * sorts before 'a' as it does under the default comparator. | ||
| */ | ||
| export const htmlTextComparator = (a: unknown, b: unknown): number => { | ||
| const toText = (v: unknown) => | ||
| typeof v === 'string' ? toComparableText(v) : v; | ||
| const aT = toText(a); | ||
| const bT = toText(b); | ||
| if (aT == null && bT == null) return 0; | ||
| if (aT == null) return -1; | ||
| if (bT == null) return 1; | ||
| if (typeof aT === 'number' && typeof bT === 'number') return aT - bT; | ||
| if (aT === bT) return 0; | ||
| return aT < bT ? -1 : 1; | ||
| }; | ||
|
|
||
| export default htmlTextFilterValueGetter; | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: The comparator sanitizes and parses HTML during every comparison call, and sorting invokes the comparator many times (
O(n log n)), which can cause major UI slowdowns on larger tables. Precompute/cache the extracted text once per row value (for example via value getter or memoization) and compare cached text instead of reparsing inside comparator hot path. [performance]Severity Level: Major⚠️
Steps of Reproduction ✅
Fix in Cursor | Fix in VSCode Claude
(Use Cmd/Ctrl + Click for best experience)
Prompt for AI Agent 🤖