diff --git a/CHANGES.txt b/CHANGES.txt index 41e0355..649d2ff 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,6 +4,19 @@ Changelog 2.0b1 (Unreleased) ------------------ +- Use the cooked text of legacy comments when migrating. + [davisagli] + +- Make sure that comment text is transformed to plain text when indexing. + [davisagli] + +- Move logic for transforming comment text to the Comment class's getText + method. Use a comment instance's mime_type attribute in preference to the + global setting for the source mimetype. Use text/x-html-safe as the target + mimetype to make sure the safe HTML filter is applied, in case the source is + untrusted HTML. + [davisagli] + - Provide a filter_callback option to the migration view, so that a custom policy for which comments get migrated can be implemented. [davisagli] diff --git a/plone/app/discussion/browser/comments.pt b/plone/app/discussion/browser/comments.pt index 0ddca97..8cc2f3a 100644 --- a/plone/app/discussion/browser/comments.pt +++ b/plone/app/discussion/browser/comments.pt @@ -80,7 +80,7 @@
- +
- + 25: + text = join(object.getText(targetMimetype='text/plain').split()[:MAX_DESCRIPTION]) + if len(object.getText().split()) > 25: text += " [...]" return text @indexer(IComment) def searchable_text(object): - return object.text + return object.getText(targetMimetype='text/plain') @indexer(IComment) def in_response_to(object): diff --git a/plone/app/discussion/comment.py b/plone/app/discussion/comment.py index 4112efd..81e9cfc 100644 --- a/plone/app/discussion/comment.py +++ b/plone/app/discussion/comment.py @@ -79,7 +79,7 @@ class Comment(CatalogAware, WorkflowAware, DynamicType, Traversable, title = u"" - mime_type = "text/plain" + mime_type = None text = u"" creator = None @@ -113,10 +113,26 @@ class Comment(CatalogAware, WorkflowAware, DynamicType, Traversable, """ return self.id - def getText(self): + def getText(self, targetMimetype=None): """The body text of a comment. """ - return self.text + transforms = getToolByName(self, 'portal_transforms') + + if targetMimetype is None: + targetMimetype = 'text/x-html-safe' + + sourceMimetype = getattr(self, 'mime_type', None) + if sourceMimetype is None: + registry = queryUtility(IRegistry) + settings = registry.forInterface(IDiscussionSettings, check=False) + sourceMimetype = settings.text_transform + text = self.text + if isinstance(text, unicode): + text = text.encode('utf8') + return transforms.convertTo(targetMimetype, + text, + context=self, + mimetype=sourceMimetype).getData() def Title(self): """The title of the comment. diff --git a/plone/app/discussion/tests/test_comment.py b/plone/app/discussion/tests/test_comment.py index 401c9f9..c778e37 100644 --- a/plone/app/discussion/tests/test_comment.py +++ b/plone/app/discussion/tests/test_comment.py @@ -123,6 +123,44 @@ class CommentTest(PloneTestCase): comment1 = createObject('plone.Comment') self.assertEquals(comment1.Type(), 'Comment') + def test_getText(self): + comment1 = createObject('plone.Comment') + comment1.text = """First paragraph + + Second paragraph""" + self.assertEquals(comment1.getText(), + "

First paragraph

Second paragraph

") + + def test_getText_escapes_HTML(self): + comment1 = createObject('plone.Comment') + comment1.text = """Got HTML?""" + self.assertEquals(comment1.getText(), + "

<b>Got HTML?</b>

") + + def test_getText_with_non_ascii_characters(self): + comment1 = createObject('plone.Comment') + comment1.text = u"""Umlaute sind ä, ö und ü.""" + self.assertEquals(comment1.getText(), + '

Umlaute sind \xc3\xa4, \xc3\xb6 und \xc3\xbc.

') + + def test_getText_doesnt_link(self): + comment1 = createObject('plone.Comment') + comment1.text = "Go to http://www.plone.org" + self.assertEquals(comment1.getText(), + "

Go to http://www.plone.org

") + + def test_getText_uses_comment_mime_type(self): + comment1 = createObject('plone.Comment') + comment1.text = "Go to http://www.plone.org" + comment1.mime_type = 'text/x-web-intelligent' + self.assertEquals(comment1.getText(), + 'Go to
http://www.plone.org') + + def test_getText_w_custom_targetMimetype(self): + comment1 = createObject('plone.Comment') + comment1.text = 'para' + self.assertEquals(comment1.getText(targetMimetype='text/plain'), 'para') + def test_traversal(self): # make sure comments are traversable, have an id, absolute_url and # physical path diff --git a/plone/app/discussion/tests/test_comments_viewlet.py b/plone/app/discussion/tests/test_comments_viewlet.py index a7ef15d..d5a0f21 100644 --- a/plone/app/discussion/tests/test_comments_viewlet.py +++ b/plone/app/discussion/tests/test_comments_viewlet.py @@ -228,28 +228,6 @@ class TestCommentsViewlet(PloneTestCase): settings = registry.forInterface(IDiscussionSettings) settings.globally_enabled = True - def test_cook(self): - text = """First paragraph - - Second paragraph""" - self.assertEquals(self.viewlet.cook(text), - "

First paragraph

Second paragraph

") - - def test_cook_no_html(self): - text = """Got HTML?""" - self.assertEquals(self.viewlet.cook(text), - "

<b>Got HTML?</b>

") - - def test_cook_with_no_ascii_characters(self): - text = """Umlaute sind ä, ö und ü.""" - self.assertEquals(self.viewlet.cook(text), - "

Umlaute sind \xc3\xa4, \xc3\xb6 und \xc3\xbc.

") - - def test_cook_links(self): - text = "Go to http://www.plone.org" - self.assertEquals(self.viewlet.cook(text), - "

Go to http://www.plone.org

") - def test_can_reply(self): # Portal owner can reply self.failUnless(self.viewlet.can_reply()) diff --git a/plone/app/discussion/tests/test_migration.py b/plone/app/discussion/tests/test_migration.py index d9bd8d3..8df9e16 100644 --- a/plone/app/discussion/tests/test_migration.py +++ b/plone/app/discussion/tests/test_migration.py @@ -69,7 +69,8 @@ class MigrationTest(PloneTestCase): comment1 = conversation.values()[0] self.assert_(IComment.providedBy(comment1)) self.assertEquals(comment1.Title(), 'Jim on Document 1') - self.assertEquals(comment1.text, 'My Text') + self.assertEquals(comment1.text, '

My Text

\n') + self.assertEquals(comment1.mime_type, 'text/html') self.assertEquals(comment1.Creator(), 'Jim') self.assertEquals(comment1.creation_date, datetime(2003, 3, 11, 9, 28, 6))