From 3e086564c9629015e753ceda6475a031653a8dbe Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Mon, 21 Feb 2022 11:32:20 -0500 Subject: [PATCH 1/7] Don't linkify code blocks Signed-off-by: Robin Townsend --- src/components/views/messages/TextualBody.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/views/messages/TextualBody.tsx b/src/components/views/messages/TextualBody.tsx index 3b6fe38bdf6..50f8f365525 100644 --- a/src/components/views/messages/TextualBody.tsx +++ b/src/components/views/messages/TextualBody.tsx @@ -92,7 +92,7 @@ export default class TextualBody extends React.Component { // are still sent as plaintext URLs. If these are ever pillified in the composer, // we should be pillify them here by doing the linkifying BEFORE the pillifying. pillifyLinks([this.contentRef.current], this.props.mxEvent, this.pills); - HtmlUtils.linkifyElement(this.contentRef.current); + HtmlUtils.linkifyElement(this.contentRef.current, { ignoreTags: ['pre'] }); this.calculateUrlPreview(); if (this.props.mxEvent.getContent().format === "org.matrix.custom.html") { From af25bb9be57798a418897a766df9befff5a5e3a5 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Mon, 21 Feb 2022 11:46:16 -0500 Subject: [PATCH 2/7] Put the linkify ignoreTags option in the right place Signed-off-by: Robin Townsend --- src/components/views/messages/TextualBody.tsx | 2 +- src/linkify-matrix.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/components/views/messages/TextualBody.tsx b/src/components/views/messages/TextualBody.tsx index 50f8f365525..3b6fe38bdf6 100644 --- a/src/components/views/messages/TextualBody.tsx +++ b/src/components/views/messages/TextualBody.tsx @@ -92,7 +92,7 @@ export default class TextualBody extends React.Component { // are still sent as plaintext URLs. If these are ever pillified in the composer, // we should be pillify them here by doing the linkifying BEFORE the pillifying. pillifyLinks([this.contentRef.current], this.props.mxEvent, this.pills); - HtmlUtils.linkifyElement(this.contentRef.current, { ignoreTags: ['pre'] }); + HtmlUtils.linkifyElement(this.contentRef.current); this.calculateUrlPreview(); if (this.props.mxEvent.getContent().format === "org.matrix.custom.html") { diff --git a/src/linkify-matrix.ts b/src/linkify-matrix.ts index 1fdd8106ba1..1bd63b22ea1 100644 --- a/src/linkify-matrix.ts +++ b/src/linkify-matrix.ts @@ -223,6 +223,8 @@ export const options = { rel: 'noreferrer noopener', }, + ignoreTags: ['pre'], + className: 'linkified', target: function(href: string, type: Type | string): string { From a293d19a6168f356d9e95358576b0c70cf924559 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Mon, 21 Feb 2022 12:50:38 -0500 Subject: [PATCH 3/7] Add code to list of ignored linkification tags as well Signed-off-by: Robin Townsend --- src/linkify-matrix.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linkify-matrix.ts b/src/linkify-matrix.ts index 1bd63b22ea1..c431a05d087 100644 --- a/src/linkify-matrix.ts +++ b/src/linkify-matrix.ts @@ -223,7 +223,7 @@ export const options = { rel: 'noreferrer noopener', }, - ignoreTags: ['pre'], + ignoreTags: ['pre', 'code'], className: 'linkified', From 05270f368471e9779be9c63626dac740f844151e Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Mon, 21 Feb 2022 13:01:48 -0500 Subject: [PATCH 4/7] Test that code blocks skip linkification Signed-off-by: Robin Townsend --- .../views/messages/TextualBody-test.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/components/views/messages/TextualBody-test.js b/test/components/views/messages/TextualBody-test.js index 48ab8ed2445..51e11e3aca9 100644 --- a/test/components/views/messages/TextualBody-test.js +++ b/test/components/views/messages/TextualBody-test.js @@ -133,6 +133,33 @@ describe("", () => { 'Visit ' + 'https://matrix.org/'); }); + + it("linkification is not applied to code blocks", () => { + const ev = mkEvent({ + type: "m.room.message", + room: "room_id", + user: "sender", + content: { + body: "Visit `https://matrix.org/`\n```\nhttps://matrix.org/\n```", + msgtype: "m.text", + format: "org.matrix.custom.html", + formatted_body: "

Visit https://matrix.org/

\n
https://matrix.org/\n
\n", + }, + event: true, + }); + + const wrapper = mount(); + expect(wrapper.text()).toBe("Visit https://matrix.org/\n1https://matrix.org/\n\n"); + const content = wrapper.find(".mx_EventTile_body"); + expect(content.html()).toBe('' + + '

Visit https://matrix.org/

\n' + + '
' + + '
' +
+                '1' +
+                'https://matrix.org/\n
' + + '' + + '
\n
'); + }); }); describe("renders formatted m.text correctly", () => { From 52a8a5c119a427d12604bb7d1567a42a1ce766aa Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Mon, 21 Feb 2022 13:03:11 -0500 Subject: [PATCH 5/7] Move test to the right spot Signed-off-by: Robin Townsend --- .../views/messages/TextualBody-test.js | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/test/components/views/messages/TextualBody-test.js b/test/components/views/messages/TextualBody-test.js index 51e11e3aca9..85e1d24c4fb 100644 --- a/test/components/views/messages/TextualBody-test.js +++ b/test/components/views/messages/TextualBody-test.js @@ -133,33 +133,6 @@ describe("", () => { 'Visit ' + 'https://matrix.org/'); }); - - it("linkification is not applied to code blocks", () => { - const ev = mkEvent({ - type: "m.room.message", - room: "room_id", - user: "sender", - content: { - body: "Visit `https://matrix.org/`\n```\nhttps://matrix.org/\n```", - msgtype: "m.text", - format: "org.matrix.custom.html", - formatted_body: "

Visit https://matrix.org/

\n
https://matrix.org/\n
\n", - }, - event: true, - }); - - const wrapper = mount(); - expect(wrapper.text()).toBe("Visit https://matrix.org/\n1https://matrix.org/\n\n"); - const content = wrapper.find(".mx_EventTile_body"); - expect(content.html()).toBe('' + - '

Visit https://matrix.org/

\n' + - '
' + - '
' +
-                '1' +
-                'https://matrix.org/\n
' + - '' + - '
\n
'); - }); }); describe("renders formatted m.text correctly", () => { @@ -223,6 +196,33 @@ describe("", () => { ''); }); + it("linkification is not applied to code blocks", () => { + const ev = mkEvent({ + type: "m.room.message", + room: "room_id", + user: "sender", + content: { + body: "Visit `https://matrix.org/`\n```\nhttps://matrix.org/\n```", + msgtype: "m.text", + format: "org.matrix.custom.html", + formatted_body: "

Visit https://matrix.org/

\n
https://matrix.org/\n
\n", + }, + event: true, + }); + + const wrapper = mount(); + expect(wrapper.text()).toBe("Visit https://matrix.org/\n1https://matrix.org/\n\n"); + const content = wrapper.find(".mx_EventTile_body"); + expect(content.html()).toBe('' + + '

Visit https://matrix.org/

\n' + + '
' + + '
' +
+                '1' +
+                'https://matrix.org/\n
' + + '' + + '
\n
'); + }); + // If pills were rendered within a Portal/same shadow DOM then it'd be easier to test it("pills get injected correctly into the DOM", () => { const ev = mkEvent({ From bb4c78c540d7fb176534490f627d3f02297e0b45 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Mon, 21 Feb 2022 15:35:32 -0500 Subject: [PATCH 6/7] Use a snapshot instead for test Signed-off-by: Robin Townsend --- test/components/views/messages/TextualBody-test.js | 9 +-------- .../messages/__snapshots__/TextualBody-test.js.snap | 8 ++++++++ 2 files changed, 9 insertions(+), 8 deletions(-) create mode 100644 test/components/views/messages/__snapshots__/TextualBody-test.js.snap diff --git a/test/components/views/messages/TextualBody-test.js b/test/components/views/messages/TextualBody-test.js index 85e1d24c4fb..1ec381f6c88 100644 --- a/test/components/views/messages/TextualBody-test.js +++ b/test/components/views/messages/TextualBody-test.js @@ -213,14 +213,7 @@ describe("", () => { const wrapper = mount(); expect(wrapper.text()).toBe("Visit https://matrix.org/\n1https://matrix.org/\n\n"); const content = wrapper.find(".mx_EventTile_body"); - expect(content.html()).toBe('' + - '

Visit https://matrix.org/

\n' + - '
' + - '
' +
-                '1' +
-                'https://matrix.org/\n
' + - '' + - '
\n
'); + expect(content.html()).toMatchSnapshot(); }); // If pills were rendered within a Portal/same shadow DOM then it'd be easier to test diff --git a/test/components/views/messages/__snapshots__/TextualBody-test.js.snap b/test/components/views/messages/__snapshots__/TextualBody-test.js.snap new file mode 100644 index 00000000000..1e5f4dc5e4d --- /dev/null +++ b/test/components/views/messages/__snapshots__/TextualBody-test.js.snap @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[` renders formatted m.text correctly linkification is not applied to code blocks 1`] = ` +"

Visit https://matrix.org/

+
1https://matrix.org/
+
+
" +`; From 5e3cef0744eddb145c5f5110d4598bf542c9cf34 Mon Sep 17 00:00:00 2001 From: Robin Townsend Date: Mon, 2 May 2022 20:19:02 -0400 Subject: [PATCH 7/7] Fix tests --- test/components/views/messages/TextualBody-test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/components/views/messages/TextualBody-test.tsx b/test/components/views/messages/TextualBody-test.tsx index 42de21426f8..fe4dbf99467 100644 --- a/test/components/views/messages/TextualBody-test.tsx +++ b/test/components/views/messages/TextualBody-test.tsx @@ -230,7 +230,7 @@ describe("", () => { event: true, }); - const wrapper = mount(); + const wrapper = getComponent({ mxEvent: ev }, matrixClient); expect(wrapper.text()).toBe("Visit https://matrix.org/\n1https://matrix.org/\n\n"); const content = wrapper.find(".mx_EventTile_body"); expect(content.html()).toMatchSnapshot();