 			event.stopPropagation()
 			return
 
-		if template.matchSelectorRegex.test template.input.value
-			template.textFilter.set(template.input.value.match(template.selectorRegex)[1])
+		value = template.input.value
+		value = value.substr 0, getCursorPosition(template.input)
+
+		if template.matchSelectorRegex.test value
+			template.textFilter.set(value.match(template.selectorRegex)[1])
 			template.open.set true
 		else
 			template.open.set false

val = (v, d) ->
	return if v? then v else d



		if event.which is 13
			template.open.set false

			value = template.input.value
			caret = getCursorPosition(template.input)
			firstPartValue = value.substr 0, caret
			lastPartValue = value.substr caret

			firstPartValue = firstPartValue.replace(template.selectorRegex, template.prefix + template.value.curValue + template.suffix)

			template.input.value = firstPartValue + lastPartValue

			setCursorPosition template.input, firstPartValue.length

			event.preventDefault()
			event.stopPropagation()

