Add reference extraction to document rich text link handler

pull/9304/head
Matt Westcott 2022-10-04 12:42:12 +01:00
rodzic d0fed42492
commit cf467cf7b3
2 zmienionych plików z 16 dodań i 0 usunięć

Wyświetl plik

@ -21,3 +21,7 @@ class DocumentLinkHandler(LinkHandler):
return '<a href="%s">' % escape(doc.url)
except (ObjectDoesNotExist, KeyError):
return "<a>"
@classmethod
def extract_references(cls, attrs):
yield cls.get_model(), attrs["id"], "", ""

Wyświetl plik

@ -1,12 +1,14 @@
from bs4 import BeautifulSoup
from django.test import TestCase
from wagtail.documents import get_document_model
from wagtail.documents.rich_text import (
DocumentLinkHandler as FrontendDocumentLinkHandler,
)
from wagtail.documents.rich_text.editor_html import (
DocumentLinkHandler as EditorHtmlDocumentLinkHandler,
)
from wagtail.fields import RichTextField
class TestEditorHtmlDocumentLinkHandler(TestCase):
@ -48,3 +50,13 @@ class TestFrontendDocumentLinkHandler(TestCase):
def test_expand_db_attributes_with_missing_id(self):
result = FrontendDocumentLinkHandler.expand_db_attributes({})
self.assertEqual(result, "<a>")
def test_extract_references(self):
self.assertEqual(
list(
RichTextField().extract_references(
'<a linktype="document" id="1">Link to a document</a>'
)
),
[(get_document_model(), "1", "", "")],
)