diff --git a/src/scripts/components/input.js b/src/scripts/components/input.js index cf21756..74dd755 100644 --- a/src/scripts/components/input.js +++ b/src/scripts/components/input.js @@ -1,4 +1,4 @@ -import { calcWidthOfInput, stripHTML } from '../lib/utils'; +import { calcWidthOfInput, escape } from '../lib/utils'; export default class Input { constructor({ element, type, classNames, placeholderValue }) { @@ -21,11 +21,11 @@ export default class Input { } set value(value) { - this.element.value = value; + this.element.value = `${value}`; } get value() { - return stripHTML(this.element.value); + return escape(this.element.value); } addEventListeners() { diff --git a/src/scripts/components/input.test.js b/src/scripts/components/input.test.js index 62a228d..45172fc 100644 --- a/src/scripts/components/input.test.js +++ b/src/scripts/components/input.test.js @@ -336,6 +336,12 @@ describe('components/input', () => { instance.value = value; expect(instance.element.value).to.equal(value); }); + + it('casts value to string', () => { + const value = 1234; + instance.value = value; + expect(instance.element.value).to.equal(`${value}`); + }); }); describe('value getter', () => { diff --git a/src/scripts/constants.js b/src/scripts/constants.js index ba09816..1bb189d 100644 --- a/src/scripts/constants.js +++ b/src/scripts/constants.js @@ -1,4 +1,4 @@ -import { stripHTML } from './lib/utils'; +import { escape } from './lib/utils'; export const DEFAULT_CLASSNAMES = { containerOuter: 'choices', @@ -63,7 +63,7 @@ export const DEFAULT_CONFIG = { noChoicesText: 'No choices to choose from', itemSelectText: 'Press to select', uniqueItemText: 'Only unique values can be added.', - addItemText: value => `Press Enter to add "${stripHTML(value)}"`, + addItemText: value => `Press Enter to add "${escape(value)}"`, maxItemText: maxItemCount => `Only ${maxItemCount} values can be added.`, itemComparer: (choice, item) => choice === item, fuseOptions: { diff --git a/src/scripts/lib/utils.js b/src/scripts/lib/utils.js index b12aec7..12926eb 100644 --- a/src/scripts/lib/utils.js +++ b/src/scripts/lib/utils.js @@ -185,11 +185,11 @@ export const isScrolledIntoView = (el, parent, direction = 1) => { }; /** - * Escape html in the string + * Escapes html in the string * @param {String} html Initial string/html * @return {String} Sanitised string */ -export const stripHTML = html => +export const escape = html => html.replace(/&/g, '&') .replace(/>/g, '&rt;') .replace(/ { let width = input.offsetWidth; if (value) { - const testEl = strToEl(`${stripHTML(value)}`); + const testEl = strToEl(`${escape(value)}`); testEl.style.position = 'absolute'; testEl.style.padding = '0'; testEl.style.top = '-9999px';