From 2a116c38a315f9405f88d6ea20a49493b107eba8 Mon Sep 17 00:00:00 2001 From: Josh Schmidt Date: Tue, 30 Nov 2021 12:14:42 -0700 Subject: [PATCH] isHtmlString helper function --- src/tom-select.ts | 3 ++- src/vanilla.ts | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/tom-select.ts b/src/tom-select.ts index 7f0475c8..cd95a4d5 100644 --- a/src/tom-select.ts +++ b/src/tom-select.ts @@ -24,6 +24,7 @@ import { import { getDom, + isHtmlString, escapeQuery, triggerEvent, applyCSS, @@ -184,7 +185,7 @@ export default class TomSelect extends MicroPlugin(MicroEvent){ // default controlInput - if( typeof settings.controlInput === 'string' && settings.controlInput.indexOf('<') > -1 ){ + if( isHtmlString(settings.controlInput) ){ control_input = getDom(settings.controlInput ) as HTMLInputElement; // set attributes diff --git a/src/vanilla.ts b/src/vanilla.ts index 9ef1ab58..20e82825 100644 --- a/src/vanilla.ts +++ b/src/vanilla.ts @@ -17,7 +17,7 @@ export const getDom = ( query:any ):HTMLElement => { return query; } - if( query.indexOf('<') > -1 ){ + if( isHtmlString(query) ){ let div = document.createElement('div'); div.innerHTML = query.trim(); // Never return a text node of whitespace as the result return div.firstChild as HTMLElement; @@ -26,6 +26,13 @@ export const getDom = ( query:any ):HTMLElement => { return document.querySelector(query); }; +export const isHtmlString = (arg:any): boolean => { + if( typeof arg === 'string' && arg.indexOf('<') > -1 ){ + return true; + } + return false; +} + export const escapeQuery = (query:string):string => { return query.replace(/['"\\]/g, '\\$&'); }