Skip to content

Commit

Permalink
isHtmlString helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
oyejorge committed Nov 30, 2021
1 parent eeec76c commit 2a116c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/tom-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {

import {
getDom,
isHtmlString,
escapeQuery,
triggerEvent,
applyCSS,
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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, '\\$&');
}
Expand Down

0 comments on commit 2a116c3

Please sign in to comment.