Merge pull request #801 from abdnh/strip-quotes

Strip double quotes from names
This commit is contained in:
Damien Elmes 2020-10-22 21:47:30 +10:00 committed by GitHub
commit 87770db7f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 24 additions and 7 deletions

View File

@ -581,7 +581,7 @@ class CardLayout(QDialog):
def onRename(self):
template = self.current_template()
name = getOnlyText(_("New name:"), default=template["name"])
name = getOnlyText(_("New name:"), default=template["name"]).strip('"')
if not name.strip():
return

View File

@ -80,7 +80,7 @@ class FieldDialog(QDialog):
self.loadField(idx)
def _uniqueName(self, prompt, ignoreOrd=None, old=""):
txt = getOnlyText(prompt, default=old)
txt = getOnlyText(prompt, default=old).strip('"')
if not txt:
return
for f in self.model["flds"]:

View File

@ -85,8 +85,9 @@ class Models(QDialog):
def onRename(self) -> None:
nt = self.current_notetype()
txt = getText(_("New name:"), default=nt["name"])
if txt[1] and txt[0]:
nt["name"] = txt[0]
name = txt[0].strip('"')
if txt[1] and name:
nt["name"] = name
self.saveAndRefresh(nt)
def saveAndRefresh(self, nt: NoteType) -> None:
@ -119,7 +120,7 @@ class Models(QDialog):
def onAdd(self) -> None:
m = AddModel(self.mw, self).get()
if m:
txt = getText(_("Name:"), default=m["name"])[0]
txt = getText(_("Name:"), default=m["name"])[0].strip('"')
if txt:
m["name"] = txt
self.saveAndRefresh(m)

View File

@ -37,7 +37,7 @@ impl NoteField {
pub(crate) fn fix_name(&mut self) {
// remove special characters
let bad_chars = |c| c == ':' || c == '{' || c == '}';
let bad_chars = |c| c == ':' || c == '{' || c == '}' || c == '"';
if self.name.contains(bad_chars) {
self.name = self.name.replace(bad_chars, "");
}

View File

@ -223,8 +223,13 @@ impl NoteType {
if self.templates.is_empty() {
return Err(AnkiError::invalid_input("1 template required"));
}
let bad_chars = |c| c == '"';
if self.name.contains(bad_chars) {
self.name = self.name.replace(bad_chars, "");
}
self.normalize_names();
self.fix_field_names();
self.fix_template_names();
self.ensure_names_unique();
self.reposition_sort_idx();
@ -322,6 +327,10 @@ impl NoteType {
self.fields.iter_mut().for_each(NoteField::fix_name);
}
fn fix_template_names(&mut self) {
self.templates.iter_mut().for_each(CardTemplate::fix_name);
}
/// Find the field index of the provided field name.
pub(crate) fn get_field_ord(&self, field_name: &str) -> Option<usize> {
let field_name = UniCase::new(field_name);

View File

@ -88,4 +88,11 @@ impl CardTemplate {
},
}
}
pub(crate) fn fix_name(&mut self) {
let bad_chars = |c| c == '"';
if self.name.contains(bad_chars) {
self.name = self.name.replace(bad_chars, "");
}
}
}

View File

@ -29,7 +29,7 @@ fn is_tag_separator(c: char) -> bool {
}
fn invalid_char_for_tag(c: char) -> bool {
c.is_ascii_control() || is_tag_separator(c)
c.is_ascii_control() || is_tag_separator(c) || c == '"'
}
impl Collection {