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

Ensure scripts provided by textformatter are run #2415

Merged
merged 3 commits into from
Oct 29, 2020
Merged
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
21 changes: 17 additions & 4 deletions js/src/forum/components/CommentPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,36 @@ export default class CommentPost extends Post {
]);
}

onupdate(vnode) {
super.onupdate();

refreshContent() {
const contentHtml = this.isEditing() ? '' : this.attrs.post.contentHtml();

// If the post content has changed since the last render, we'll run through
// all of the <script> tags in the content and evaluate them. This is
// necessary because TextFormatter outputs them for e.g. syntax highlighting.
if (this.contentHtml !== contentHtml) {
this.$('.Post-body script').each(function () {
eval.call(window, $(this).text());
const script = document.createElement('script');
script.textContent = this.textContent;
Array.from(this.attributes).forEach((attr) => script.setAttribute(attr.name, attr.value));
this.parentNode.replaceChild(script, this);
});
}

this.contentHtml = contentHtml;
}

oncreate(vnode) {
super.oncreate(vnode);

this.refreshContent();
}

onupdate(vnode) {
super.onupdate(vnode);

this.refreshContent();
}

isEditing() {
return app.composer.bodyMatches(EditPostComposer, { post: this.attrs.post });
}
Expand Down