Skip to content

Commit

Permalink
CU-2n66qf5: sanitise common html tags on data upload. Rest of the tag…
Browse files Browse the repository at this point in the history
…s are left as is, trim leading whitespace on doc summary previews
  • Loading branch information
tomolopolis committed Jul 27, 2022
1 parent 4839559 commit 10a5d98
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
13 changes: 12 additions & 1 deletion webapp/api/api/data_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import pandas as pd

from .models import *
Expand Down Expand Up @@ -29,12 +31,21 @@ def dataset_from_file(dataset: Dataset):
row = row[1]
document = Document()
document.name = row['name']
document.text = row['text']
document.text = sanitise_input(row['text'])
document.dataset = dataset
document.save()
else:
raise Exception("Please make sure the file is either a .csv or .xlsx format")


def sanitise_input(text: str):
tags = [('<br>', '\n'), ('</?p>', '\n'), ('<span(?:.*?)?>', ''),
('</span>', ''), ('<div (?:.*?)?>', '\n'), ('</div>', '\n'),
('</?html>', ''), ('</?body>', ''), ('</?head>', '')]
for tag, repl in tags:
text = re.sub(tag, repl, text)
return text


def delete_orphan_docs(dataset: Dataset):
Document.objects.filter(dataset__id=dataset.id).delete()
6 changes: 0 additions & 6 deletions webapp/frontend/src/components/common/ClinicalText.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,6 @@ export default {
formattedText += this.text.slice(start, this.text.length)
}
}
// escape '<' '>' that may be interpreted as start/end tags.
formattedText = formattedText
.replace(/<(?!span|br)/g, '&lt')
.replace(/&lt(?=\/span>)/g, '<')
.replace(/(?<!")>/g, '&gt')
.replace(/(?<=<\/span|br)&gt/g, '>')
formattedText = this.addAnnos ? `<div @contextmenu.prevent.stop="showCtxMenu($event)">${formattedText}</div>` : `<div>${formattedText}</div>`
this.scrollIntoView(timeout)
Expand Down
1 change: 1 addition & 0 deletions webapp/frontend/src/components/common/DocumentSummary.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export default {
},
filters: {
limitText (value) {
value = value.trim()
let splitText = value.split('\n')
if (splitText.length > 5) {
return splitText.slice(0, 5).join('\n')
Expand Down

0 comments on commit 10a5d98

Please sign in to comment.