Skip to content

Commit

Permalink
Fix sanitizer call in ToC if html data is array of strings (jupyterla…
Browse files Browse the repository at this point in the history
…b#17114)

* Fix html sanitizer, if html data is array of strings

* Better use \n, when joining html data

* Add test for cell headings multiline HTML problem

* Harmonize the test description

---------

Co-authored-by: Michał Krassowski <5832902+krassowski@users.noreply.github.com>
  • Loading branch information
martenrichter and krassowski authored Jan 2, 2025
1 parent c4da9f1 commit befe9f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/cells/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1312,9 +1312,13 @@ export class CodeCell extends Cell<ICodeCellModel> {

// Parse HTML output
if (htmlType) {
let htmlData = m.data[htmlType] as string | string[];
if (typeof htmlData !== 'string') {
htmlData = htmlData.join('\n');
}
headings.push(
...TableOfContentsUtils.getHTMLHeadings(
this._rendermime.sanitizer.sanitize(m.data[htmlType] as string)
this._rendermime.sanitizer.sanitize(htmlData)
).map(heading => {
return {
...heading,
Expand Down
21 changes: 21 additions & 0 deletions packages/cells/test/widget.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,27 @@ describe('cells/widget', () => {
});
});

describe('#headings', () => {
it('should not throw when outputs contain an array of strings', () => {
const modelheadings = new CodeCellModel();
const widget = new CodeCell({
contentFactory: NBTestUtils.createCodeCellFactory(),
model: modelheadings,
rendermime
});
widget.initializeState();
widget.model.outputs.add({
output_type: 'display_data',
data: {
'text/html': ['Some ', ' thing']
},
metadata: {}
});
// try if headings got a problem with multi line html
expect(widget.headings).toEqual([]);
});
});

describe('#outputArea', () => {
it('should be the output area used by the cell', () => {
const widget = new CodeCell({
Expand Down

0 comments on commit befe9f7

Please sign in to comment.