Skip to content
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

Don't append content in rendered output upon rerendering. #6513

Merged
merged 3 commits into from Jun 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/rendermime/src/widgets.ts
Expand Up @@ -63,10 +63,21 @@ export abstract class RenderedCommon extends Widget
* @param model - The mime model to render.
*
* @returns A promise which resolves when rendering is complete.
*
* #### Notes
* If the DOM node for this widget already has content, it is emptied
* before rendering. Subclasses that do not want this behavior
* (if, for instance, they are using DOM diffing), should override
* this method and not call `super.renderModel()`.
*/
async renderModel(model: IRenderMime.IMimeModel): Promise<void> {
// TODO compare model against old model for early bail?

// Empty any existing content in the node from previous renders
while (this.node.firstChild) {
this.node.removeChild(this.node.firstChild);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it easier to just do this.node.textContent=''?

Copy link
Contributor

Choose a reason for hiding this comment

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

huh, https://stackoverflow.com/questions/3955229/remove-all-child-elements-of-a-dom-node-in-javascript indicates your method (or maybe equivalently this.node.firstChild.remove()?) may be faster, and possibly more correct if there is, for example, svg content.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I was looking at the same post. I otherwise don't have any reason to chose one method over the other.

// Toggle the trusted class on the widget.
this.toggleClass('jp-mod-trusted', model.trusted);

Expand Down
43 changes: 43 additions & 0 deletions tests/test-rendermime/src/factories.spec.ts
Expand Up @@ -66,6 +66,16 @@ describe('rendermime/factories', () => {
expect(w.node.innerHTML).to.equal('<pre>x = 2 ** a</pre>');
});

it('should be re-renderable', async () => {
const f = textRendererFactory;
const mimeType = 'text/plain';
const model = createModel(mimeType, 'x = 2 ** a');
const w = f.createRenderer({ mimeType, ...defaultOptions });
await w.renderModel(model);
await w.renderModel(model);
expect(w.node.innerHTML).to.equal('<pre>x = 2 ** a</pre>');
});

it('should output the correct HTML with ansi colors', async () => {
const f = textRendererFactory;
const source = 'There is no text but \x1b[01;41;32mtext\x1b[00m.\nWoo.';
Expand Down Expand Up @@ -116,6 +126,17 @@ describe('rendermime/factories', () => {
await w.renderModel(model);
expect(w.node.textContent).to.equal(source);
});

it('should be re-renderable', async () => {
const source = 'sumlimits_{i=0}^{infty} \frac{1}{n^2}';
const f = latexRendererFactory;
const mimeType = 'text/latex';
const model = createModel(mimeType, source);
const w = f.createRenderer({ mimeType, ...defaultOptions });
await w.renderModel(model);
await w.renderModel(model);
expect(w.node.textContent).to.equal(source);
});
});
});

Expand Down Expand Up @@ -174,6 +195,17 @@ describe('rendermime/factories', () => {
expect(w.node.innerHTML).to.equal(source);
});

it('it should be re-renderable', async () => {
const f = markdownRendererFactory;
const source = '<p>hello</p>';
const mimeType = 'text/markdown';
const model = createModel(mimeType, source);
const w = f.createRenderer({ mimeType, ...defaultOptions });
await w.renderModel(model);
await w.renderModel(model);
expect(w.node.innerHTML).to.equal(source);
});

it('should add header anchors', async () => {
const source = require('../../../examples/filebrowser/sample.md')
.default as string;
Expand Down Expand Up @@ -229,6 +261,17 @@ describe('rendermime/factories', () => {
expect(w.node.innerHTML).to.equal('<h1>This is great</h1>');
});

it('should be re-renderable', async () => {
const f = htmlRendererFactory;
const source = '<h1>This is great</h1>';
const mimeType = 'text/html';
const model = createModel(mimeType, source);
const w = f.createRenderer({ mimeType, ...defaultOptions });
await w.renderModel(model);
await w.renderModel(model);
expect(w.node.innerHTML).to.equal('<h1>This is great</h1>');
});

// TODO we are disabling script execution for now.
it.skip('should execute a script tag when attached', () => {
const source = '<script>window.y=3;</script>';
Expand Down