From 7b4645b54be2991d659aa4c6699132d1b1cd09a4 Mon Sep 17 00:00:00 2001 From: rr- Date: Thu, 23 Jun 2016 12:36:39 +0200 Subject: [PATCH] client/tags: improve tag regex handling --- client/js/views/tag_edit_view.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/client/js/views/tag_edit_view.js b/client/js/views/tag_edit_view.js index f9d0df36..5cf3ba14 100644 --- a/client/js/views/tag_edit_view.js +++ b/client/js/views/tag_edit_view.js @@ -17,12 +17,15 @@ class TagEditView extends events.EventTarget { this._tag = ctx.tag; this._hostNode = ctx.hostNode; - const baseRegex = config.tagNameRegex.replace(/[\^\$]/g, ''); - ctx.tagNamesPattern = '^((' + baseRegex + ')\\s+)*(' + baseRegex + ')$'; views.replaceContent(this._hostNode, template(ctx)); views.decorateValidator(this._formNode); + if (this._namesFieldNode) { + this._namesFieldNode.addEventListener( + 'input', e => this._evtNameInput(e)); + } + if (this._implicationsFieldNode) { new TagInputControl(this._implicationsFieldNode); } @@ -53,6 +56,27 @@ class TagEditView extends events.EventTarget { views.showError(this._hostNode, message); } + _evtNameInput(e) { + const regex = new RegExp(config.tagNameRegex); + const list = this._namesFieldNode.value.split(/\s+/).filter(t => t); + + if (!list.length) { + this._namesFieldNode.setCustomValidity( + 'Tags must have at least one name.'); + return; + } + + for (let item of list) { + if (!regex.test(item)) { + this._namesFieldNode.setCustomValidity( + `Tag name "${item}" contains invalid symbols.`); + return; + } + } + + this._namesFieldNode.setCustomValidity(''); + } + _evtSubmit(e) { e.preventDefault(); this.dispatchEvent(new CustomEvent('submit', {