V15: Rich Text Editor links do not work with query strings and anchors (#17288)

* fix: anchors and query strings do not work

Since the change from UDIs to localLinks in href, the pattern matched a little too much in the href section completely ignoring any "extras" such as querystrings and anchors after the locallink, which meant that the locallink did not get replaced at all if they were present. This is fixed by limiting the regexp a bit.

* fix: legacy links do not follow the same regexp as new links

Because we are no longer matching the whole `href` attribute but only some of its contents, we need to fix up the old pattern. It has been extended with matching groups that follow the same pattern as the new links.

* feat: allow a-tags to be multiline

example:

```html
<a
  type="document"
  href="/{localLink:<GUID>}">
Test
</a>
```

* fix: split regex into two parts: first a tokenizer for a-tags and then a type-finder

* fix: ensure only "document" and "media" are matching to speed up the pattern

* feat: allow a-tags to be multiline
This commit is contained in:
Jacob Overgaard
2024-10-16 16:53:10 +02:00
committed by GitHub
parent 1945ac2150
commit 35e8f2e460
2 changed files with 69 additions and 43 deletions

View File

@@ -117,6 +117,34 @@ public class HtmlLocalLinkParserTests
[TestCase(
"<a href=\"/{localLink:9931BDE0-AAC3-4BAB-B838-909A7B47570E}\" title=\"world\"type=\"media\">world</a>",
"<a href=\"/media/1001/my-image.jpg\" title=\"world\">world</a>")]
[TestCase(
"<p><a type=\"document\" href=\"/{localLink:9931BDE0-AAC3-4BAB-B838-909A7B47570E}\" title=\"world\">world</a></p><p><a href=\"/{localLink:7e21a725-b905-4c5f-86dc-8c41ec116e39}\" title=\"world\" type=\"media\">world</a></p>",
"<p><a href=\"/my-test-url\" title=\"world\">world</a></p><p><a href=\"/media/1001/my-image.jpg\" title=\"world\">world</a></p>")]
// attributes order should not matter
[TestCase(
"<a rel=\"noopener\" title=\"world\" type=\"document\" href=\"/{localLink:9931BDE0-AAC3-4BAB-B838-909A7B47570E}\">world</a>",
"<a rel=\"noopener\" title=\"world\" href=\"/my-test-url\">world</a>")]
[TestCase(
"<a rel=\"noopener\" title=\"world\" href=\"/{localLink:9931BDE0-AAC3-4BAB-B838-909A7B47570E}\" type=\"document\">world</a>",
"<a rel=\"noopener\" title=\"world\" href=\"/my-test-url\">world</a>")]
[TestCase(
"<a rel=\"noopener\" title=\"world\" href=\"/{localLink:9931BDE0-AAC3-4BAB-B838-909A7B47570E}#anchor\" type=\"document\">world</a>",
"<a rel=\"noopener\" title=\"world\" href=\"/my-test-url#anchor\">world</a>")]
// anchors and query strings
[TestCase(
"<a type=\"document\" href=\"/{localLink:9931BDE0-AAC3-4BAB-B838-909A7B47570E}#anchor\" title=\"world\">world</a>",
"<a href=\"/my-test-url#anchor\" title=\"world\">world</a>")]
[TestCase(
"<a type=\"document\" href=\"/{localLink:9931BDE0-AAC3-4BAB-B838-909A7B47570E}?v=1\" title=\"world\">world</a>",
"<a href=\"/my-test-url?v=1\" title=\"world\">world</a>")]
// custom type ignored
[TestCase(
"<a type=\"custom\" href=\"/{localLink:9931BDE0-AAC3-4BAB-B838-909A7B47570E}\" title=\"world\">world</a>",
"<a type=\"custom\" href=\"/{localLink:9931BDE0-AAC3-4BAB-B838-909A7B47570E}\" title=\"world\">world</a>")]
// legacy
[TestCase(
"hello href=\"{localLink:1234}\" world ",
@@ -127,9 +155,15 @@ public class HtmlLocalLinkParserTests
[TestCase(
"hello href=\"{localLink:umb://document/9931BDE0AAC34BABB838909A7B47570E}\" world ",
"hello href=\"/my-test-url\" world ")]
[TestCase(
"hello href=\"{localLink:umb://document/9931BDE0AAC34BABB838909A7B47570E}#anchor\" world ",
"hello href=\"/my-test-url#anchor\" world ")]
[TestCase(
"hello href=\"{localLink:umb://media/9931BDE0AAC34BABB838909A7B47570E}\" world ",
"hello href=\"/media/1001/my-image.jpg\" world ")]
[TestCase(
"hello href='{localLink:umb://media/9931BDE0AAC34BABB838909A7B47570E}' world ",
"hello href='/media/1001/my-image.jpg' world ")]
// This one has an invalid char so won't match.
[TestCase(
@@ -137,7 +171,7 @@ public class HtmlLocalLinkParserTests
"hello href=\"{localLink:umb^://document/9931BDE0-AAC3-4BAB-B838-909A7B47570E}\" world ")]
[TestCase(
"hello href=\"{localLink:umb://document-type/9931BDE0-AAC3-4BAB-B838-909A7B47570E}\" world ",
"hello href=\"#\" world ")]
"hello href=\"\" world ")]
public void ParseLocalLinks(string input, string result)
{
// setup a mock URL provider which we'll use for testing