Skip to content

Commit 6a67456

Browse files
authored
Merge pull request #57594 from nextcloud/refactor/ocp-comments
refactor(core): migrate `OCP.Comments` away from jQuery
2 parents fb1dc43 + c16a68a commit 6a67456

File tree

5 files changed

+33
-23
lines changed

5 files changed

+33
-23
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { expect, it } from 'vitest'
7-
import * as Comments from '../../OCP/comments.js'
7+
import { plainToRich } from './comments.ts'
88

99
it.for([
1010
{ input: 'nextcloud.com', expected: 'nextcloud.com' },
@@ -28,6 +28,6 @@ it.for([
2828
{ input: 'FirebaseInstanceId.getInstance().deleteInstanceId()', expected: 'FirebaseInstanceId.getInstance().deleteInstanceId()' },
2929
{ input: 'I mean...it', expected: 'I mean...it' },
3030
])('OCP.Comments should parse URLs only', ({ input, expected }) => {
31-
const result = Comments.plainToRich(input)
31+
const result = plainToRich(input)
3232
expect(result).toEqual(expected)
3333
})
Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
55

6-
import $ from 'jquery'
7-
86
/*
97
* Detects links:
108
* Either the http(s) protocol is given or two strings, basically limited to ascii with the last
@@ -19,23 +17,29 @@ import $ from 'jquery'
1917
const urlRegex = /(\s|^)(https?:\/\/)([-A-Z0-9+_.]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|$)/ig
2018

2119
/**
22-
* @param {any} content -
20+
* Converts plain text to rich text
21+
*
22+
* @param content - The plain text content
2323
*/
24-
export function plainToRich(content) {
25-
return this.formatLinksRich(content)
24+
export function plainToRich(content: string) {
25+
return formatLinksRich(content)
2626
}
2727

2828
/**
29-
* @param {any} content -
29+
* Converts rich text to plain text
30+
*
31+
* @param content - The rich text content
3032
*/
31-
export function richToPlain(content) {
32-
return this.formatLinksPlain(content)
33+
export function richToPlain(content: string) {
34+
return formatLinksPlain(content)
3335
}
3436

3537
/**
36-
* @param {any} content -
38+
* Format links in the given content to rich text links
39+
*
40+
* @param content - The content containing plain text URLs
3741
*/
38-
export function formatLinksRich(content) {
42+
export function formatLinksRich(content: string) {
3943
return content.replace(urlRegex, function(_, leadingSpace, protocol, url, trailingSpace) {
4044
let linkText = url
4145
if (!protocol) {
@@ -49,13 +53,16 @@ export function formatLinksRich(content) {
4953
}
5054

5155
/**
52-
* @param {any} content -
56+
* Format links in the given content to plain text links
57+
*
58+
* @param content - The content containing rich text URLs
5359
*/
54-
export function formatLinksPlain(content) {
55-
const $content = $('<div></div>').html(content)
56-
$content.find('a').each(function() {
57-
const $this = $(this)
58-
$this.html($this.attr('href'))
60+
export function formatLinksPlain(content: string) {
61+
const el = document.createElement('div')
62+
el.innerHTML = content
63+
el.querySelectorAll('a').forEach((anchor) => {
64+
anchor.replaceWith(document.createTextNode(anchor.getAttribute('href') || ''))
5965
})
60-
return $content.html()
66+
67+
return el.innerHTML
6168
}

core/src/OCP/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { loadState } from '@nextcloud/initial-state'
77
import Accessibility from './accessibility.js'
88
import * as AppConfig from './appconfig.ts'
99
import Collaboration from './collaboration.js'
10-
import * as Comments from './comments.js'
10+
import * as Comments from './comments.ts'
1111
import Loader from './loader.js'
1212
import Toast from './toast.js'
1313
import * as WhatsNew from './whatsnew.js'
@@ -17,6 +17,9 @@ export default {
1717
Accessibility,
1818
AppConfig,
1919
Collaboration,
20+
/**
21+
* @deprecated 33.0.0
22+
*/
2023
Comments,
2124
InitialState: {
2225
/**

dist/core-main.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/core-main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)