Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash if invalid character is inserted into the text. #578

Merged
merged 1 commit into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion manuskript/models/abstractItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtWidgets import QTextEdit, qApp
from lxml import etree as ET
import re

from manuskript import enums

Expand All @@ -21,6 +22,9 @@ class abstractItem():
# Used for XML export
name = "abstractItem"

# Regexp from https://stackoverflow.com/questions/8733233/filtering-out-certain-bytes-in-python
valid_xml_re = re.compile(u'[^\u0020-\uD7FF\u0009\u000A\u000D\uE000-\uFFFD\U00010000-\U0010FFFF]+')

def __init__(self, model=None, title="", _type="abstract", xml=None, parent=None, ID=None):

self._data = {}
Expand Down Expand Up @@ -255,6 +259,9 @@ def setData(self, column, data, role=Qt.DisplayRole):
# We want to force some data even if they're empty
XMLForce = []

def cleanTextForXML(self, text):
return self.valid_xml_re.sub('', text)

def toXML(self):
"""
Returns a string containing the item (and children) in XML.
Expand All @@ -269,7 +276,7 @@ def toXML(self):
continue
val = self.data(attrib)
if val or attrib in self.XMLForce:
item.set(attrib.name, str(val))
item.set(attrib.name, self.cleanTextForXML(str(val)))

# Saving lastPath
item.set("lastPath", self._lastPath)
Expand Down
2 changes: 1 addition & 1 deletion manuskript/models/outlineItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def toXMLProcessItem(self, item):
for r in rev:
revItem = ET.Element("revision")
revItem.set("timestamp", str(r[0]))
revItem.set("text", r[1])
revItem.set("text", self.cleanTextForXML(r[1]))
item.append(revItem)

return item
Expand Down