Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Comments display optimization

Chocobozzz40f7d3c34684
diff --git a/client/src/app/+video-watch/shared/comment/video-comment.component.ts b/client/src/app/+video-watch/shared/comment/video-comment.component.ts
index 0be010c84e..e709f7a25a 100644
--- a/client/src/app/+video-watch/shared/comment/video-comment.component.ts
+++ b/client/src/app/+video-watch/shared/comment/video-comment.component.ts
@@ -15,6 +15,7 @@ import {
signal,
viewChild
} from '@angular/core'
+import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
import { RouterLink } from '@angular/router'
import { MarkdownService, Notifier, ScreenService, UserService } from '@app/core'
import { AuthService } from '@app/core/auth'
@@ -57,6 +58,7 @@ import { VideoCommentAddComponent } from './video-comment-add.component'
})
export class VideoCommentComponent implements OnChanges, OnDestroy {
private cd = inject(ChangeDetectorRef)
+ private domSanitizer = inject(DomSanitizer)
private markdownService = inject(MarkdownService)
private authService = inject(AuthService)
private userService = inject(UserService)
@@ -96,7 +98,8 @@ export class VideoCommentComponent implements OnChanges, OnDestroy {
prependModerationActions: DropdownAction<any>[]
- sanitizedCommentHTML = ''
+ // Already sanitized by MarkdownService/HtmlRendererService
+ sanitizedCommentHTML: SafeHtml = this.domSanitizer.bypassSecurityTrustHtml('')
newParentComments: VideoComment[] = []
commentAccount: Account
@@ -129,6 +132,10 @@ export class VideoCommentComponent implements OnChanges, OnDestroy {
this.cancelRepliesRequest()
}
+ if (changes['comment'] || changes['video']) {
+ this.updateSanitizedCommentHTML()
+ }
+
this.reinit()
this.toggleMobileReplyModalIfNeeded()
this.autoLoadRepliesIfNeeded()
@@ -371,11 +378,17 @@ export class VideoCommentComponent implements OnChanges, OnDestroy {
}
}
- private async reinit () {
+ private async updateSanitizedCommentHTML () {
// Before HTML rendering restore line feed for markdown list compatibility
const commentText = this.comment().text.replace(/<br.?\/?>/g, '\r\n')
const html = await this.markdownService.textMarkdownToHTML({ markdown: commentText, withHtml: true, withEmoji: true })
- this.sanitizedCommentHTML = this.markdownService.processVideoTimestamps(this.video().shortUUID, html)
+ const withTimestamps = this.markdownService.processVideoTimestamps(this.video().shortUUID, html)
+ this.sanitizedCommentHTML = this.domSanitizer.bypassSecurityTrustHtml(withTimestamps)
+
+ this.cd.markForCheck()
+ }
+
+ private reinit () {
this.newParentComments = this.parentComments().concat([ this.comment() ])
const comment = this.comment()
diff --git a/client/src/app/core/renderer/html-renderer.service.ts b/client/src/app/core/renderer/html-renderer.service.ts
index 047d0889c6..c37b38a73a 100644
--- a/client/src/app/core/renderer/html-renderer.service.ts
+++ b/client/src/app/core/renderer/html-renderer.service.ts
@@ -9,11 +9,15 @@ export class HtmlRendererService {
private simpleDomPurify: DOMPurifyI
private enhancedDomPurify: DOMPurifyI
+ private classDomPurify: DOMPurifyI
constructor () {
this.simpleDomPurify = DOMPurify()
this.enhancedDomPurify = DOMPurify()
+ // No hooks: only used to strip class attributes before content reaches the hook-enabled instances above
+ this.classDomPurify = DOMPurify()
+
this.addHrefHook(this.simpleDomPurify)
this.addHrefHook(this.enhancedDomPurify)
@@ -42,16 +46,16 @@ export class HtmlRendererService {
private addCheckSchemesHook (dompurifyInstance: DOMPurifyI, schemes: string[]) {
const regex = new RegExp(`^(${schemes.join('|')}):`, 'im')
+ // Reused across every node/call: cheaper than allocating a new anchor for each one, and only ever read synchronously
+ const anchor = document.createElement('a')
dompurifyInstance.addHook('afterSanitizeAttributes', (node: Element) => {
- const anchor = document.createElement('a')
+ if (!node.hasAttribute('href')) return
- if (node.hasAttribute('href')) {
- anchor.href = node.getAttribute('href')
+ anchor.href = node.getAttribute('href')
- if (anchor.protocol && !anchor.protocol.match(regex)) {
- node.removeAttribute('href')
- }
+ if (anchor.protocol && !anchor.protocol.match(regex)) {
+ node.removeAttribute('href')
}
})
}
@@ -74,7 +78,7 @@ export class HtmlRendererService {
} = {}) {
const { additionalTags = [], additionalAttributes = [] } = options
- return DOMPurify().sanitize(html, {
+ return this.classDomPurify.sanitize(html, {
ALLOWED_TAGS: [ ...getDefaultSanitizedTags(), ...additionalTags ],
ALLOWED_ATTR: [ ...getDefaultSanitizedHrefAttributes(), ...additionalAttributes ].filter(a => a !== 'class'),
ALLOW_DATA_ATTR: true
@@ -82,7 +86,7 @@ export class HtmlRendererService {
}
toSimpleSafeHtml (text: string) {
- return this.sanitize(this.simpleDomPurify, this.removeClassAttributes(text))
+ return this.sanitize(this.simpleDomPurify, text, { excludeAttributes: [ 'class' ] })
}
async toSimpleSafeHtmlWithLinks (text: string, options: {
@@ -121,12 +125,13 @@ export class HtmlRendererService {
private sanitize (domPurify: DOMPurifyI, html: string, options: {
additionalTags?: string[]
additionalAttributes?: string[]
+ excludeAttributes?: string[]
} = {}) {
- const { additionalTags = [], additionalAttributes = [] } = options
+ const { additionalTags = [], additionalAttributes = [], excludeAttributes = [] } = options
return domPurify.sanitize(html, {
ALLOWED_TAGS: [ ...getDefaultSanitizedTags(), ...additionalTags ],
- ALLOWED_ATTR: [ ...getDefaultSanitizedHrefAttributes(), ...additionalAttributes ],
+ ALLOWED_ATTR: [ ...getDefaultSanitizedHrefAttributes(), ...additionalAttributes ].filter(a => !excludeAttributes.includes(a)),
ALLOW_DATA_ATTR: true
})
}