Skip to content

Commit

Permalink
Merge pull request #880 from Mortal/convert-line-endings
Browse files Browse the repository at this point in the history
Convert UNIX line endings to DOS in add_revision()
  • Loading branch information
benjaoming committed Jun 30, 2018
2 parents 22a432d + db2fdad commit 928e0cb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/wiki/models/article.py
Expand Up @@ -153,6 +153,7 @@ def add_revision(self, new_revision, save=True):
new_revision.article = self
new_revision.previous_revision = self.current_revision
if save:
new_revision.clean()
new_revision.save()
self.current_revision = new_revision
if save:
Expand Down Expand Up @@ -372,6 +373,12 @@ class ArticleRevision(BaseRevisionMixin, models.Model):
def __str__(self):
return "%s (%d)" % (self.title, self.revision_number)

def clean(self):
# Enforce DOS line endings \r\n. It is the standard for web browsers,
# but when revisions are created programatically, they might
# have UNIX line endings \n instead.
self.content = self.content.replace('\r', '').replace('\n', '\r\n')

def inherit_predecessor(self, article):
"""
Inherit certain properties from predecessor because it's very
Expand Down
12 changes: 11 additions & 1 deletion tests/core/test_basic.py
@@ -1,7 +1,7 @@
from django.test import TestCase
from wiki.conf import settings as wiki_settings
from wiki.forms import Group
from wiki.models import URLPath
from wiki.models import Article, ArticleRevision, URLPath

from ..base import wiki_override_settings
from ..testdata.models import CustomGroup
Expand All @@ -26,3 +26,13 @@ def test_setting(self):
def test_custom(self):
self.assertEqual(Group, CustomGroup)
self.assertEqual(wiki_settings.GROUP_MODEL, 'testdata.CustomGroup')


class LineEndingsTests(TestCase):

def test_manager(self):

article = Article()
article.add_revision(ArticleRevision(title="Root", content="Hello\nworld"),
save=True)
self.assertEqual("Hello\r\nworld", article.current_revision.content)

0 comments on commit 928e0cb

Please sign in to comment.