@@ -14,10 +14,10 @@ export type CSSStyleDeclarationWritable = Omit<CSSStyleDeclaration, CSSStyleDecl
1414
1515/** Calculate available space for each side of the DOM element */
1616export function calculateAvailableSpace ( element : HTMLElement ) : { top : number ; bottom : number ; left : number ; right : number } {
17- const vh = window . innerHeight || 0 ;
18- const vw = window . innerWidth || 0 ;
17+ const vh = window . innerHeight ;
18+ const vw = window . innerWidth ;
1919 const { top : pageScrollTop , left : pageScrollLeft } = windowScrollPosition ( ) ;
20- const { top : elementOffsetTop = 0 , left : elementOffsetLeft = 0 } = getOffset ( element ) || { } ;
20+ const { top : elementOffsetTop = 0 , left : elementOffsetLeft = 0 } = getOffset ( element ) ?? { } ;
2121
2222 const top = elementOffsetTop - pageScrollTop ;
2323 const left = elementOffsetLeft - pageScrollLeft ;
@@ -131,27 +131,23 @@ export function getHtmlStringOutput(
131131) : string {
132132 if ( input instanceof DocumentFragment ) {
133133 // a DocumentFragment doesn't have innerHTML/outerHTML, but we can loop through all children and concatenate them all to an HTML string
134- return [ ] . map . call ( input . childNodes , ( x : HTMLElement ) => x [ type ] ) . join ( '' ) || input . textContent || '' ;
134+ return (
135+ Array . from ( input . childNodes )
136+ . map ( ( node : ChildNode ) => ( node as HTMLElement ) [ type ] )
137+ . join ( '' ) ||
138+ input . textContent ||
139+ ''
140+ ) ;
135141 } else if ( input instanceof HTMLElement ) {
136142 return input [ type ] ;
137143 }
138- return String ( input ?? '' ) ; // reaching this line means it's already a string (or number) so just return it as string
144+ return String ( input ?? '' ) ;
139145}
140146
141147/** Get offset of HTML element relative to a parent element */
142- export function getOffsetRelativeToParent (
143- parentElm : HTMLElement | null ,
144- childElm : HTMLElement | null
145- ) :
146- | {
147- top : number ;
148- right : number ;
149- bottom : number ;
150- left : number ;
151- }
152- | undefined {
148+ export function getOffsetRelativeToParent ( parentElm : HTMLElement | null , childElm : HTMLElement | null ) : HtmlElementPosition {
153149 if ( ! parentElm || ! childElm ) {
154- return undefined ;
150+ return { top : 0 , bottom : 0 , left : 0 , right : 0 } ;
155151 }
156152 const parentPos = parentElm . getBoundingClientRect ( ) ;
157153 const childPos = childElm . getBoundingClientRect ( ) ;
@@ -165,7 +161,7 @@ export function getOffsetRelativeToParent(
165161
166162/** Get HTML element offset with pure JS */
167163export function getOffset ( elm ?: HTMLElement | null ) : HtmlElementPosition {
168- if ( ! elm ?. getBoundingClientRect ) {
164+ if ( ! elm ) {
169165 return { top : 0 , bottom : 0 , left : 0 , right : 0 } ;
170166 }
171167
@@ -195,10 +191,10 @@ export function getInnerSize(elm: HTMLElement, type: 'height' | 'width'): number
195191
196192/** Get a DOM element style property value by calling getComputedStyle() on the element */
197193export function getStyleProp ( elm : HTMLElement , property : string ) : string | null {
198- if ( elm ) {
199- return getComputedStyle ( elm ) . getPropertyValue ( property ) ;
194+ if ( ! elm ) {
195+ return null ;
200196 }
201- return null ;
197+ return getComputedStyle ( elm ) . getPropertyValue ( property ) ;
202198}
203199
204200export function findFirstAttribute ( inputElm : Element | null | undefined , attributes : string [ ] ) : string | null {
@@ -223,24 +219,6 @@ export function findWidthOrDefault(inputWidth?: number | string | null, defaultV
223219 return ( / ^ [ 0 - 9 ] + $ / i. test ( `${ inputWidth } ` ) ? `${ + ( inputWidth as number ) } px` : ( inputWidth as string ) ) || defaultValue ;
224220}
225221
226- /**
227- * Simple function to encode the 5 most common HTML entities (`<`, `>`, `"`, `'`, `&`).
228- * For example: "<div>Hello</div>" => "<div>Hello</div>"
229- * @param {String } inputValue - input value to be encoded
230- * @return {String }
231- */
232- export function htmlEncode ( inputValue : string ) : string {
233- const val = typeof inputValue === 'string' ? inputValue : String ( inputValue ) ;
234- const entityMap : { [ char : string ] : string } = {
235- '&' : '&' ,
236- '<' : '<' ,
237- '>' : '>' ,
238- '"' : '"' ,
239- "'" : ''' ,
240- } ;
241- return ( val || '' ) . toString ( ) . replace ( / [ & < > " ' ] / g, ( s ) => entityMap [ s as keyof { [ char : string ] : string } ] ) ;
242- }
243-
244222/**
245223 * Simple function to decode the most common HTML entities.
246224 * For example: "<div>Hablar español? ��</div>" => "<div>Hablar español? 🦄</div>"
@@ -280,6 +258,24 @@ export function htmlDecode(input?: string | boolean | number): string {
280258 return '' ;
281259}
282260
261+ /**
262+ * Simple function to encode the 5 most common HTML entities (`<`, `>`, `"`, `'`, `&`).
263+ * For example: "<div>Hello</div>" => "<div>Hello</div>"
264+ * @param {String } inputValue - input value to be encoded
265+ * @return {String }
266+ */
267+ export function htmlEncode ( inputValue : string ) : string {
268+ const val = typeof inputValue === 'string' ? inputValue : String ( inputValue ) ;
269+ const entityMap : { [ char : string ] : string } = {
270+ '&' : '&' ,
271+ '<' : '<' ,
272+ '>' : '>' ,
273+ '"' : '"' ,
274+ "'" : ''' ,
275+ } ;
276+ return ( val || '' ) . toString ( ) . replace ( / [ & < > " ' ] / g, ( s ) => entityMap [ s as keyof { [ char : string ] : string } ] ) ;
277+ }
278+
283279/**
284280 * Encode string to html special char and add html space padding defined
285281 * @param {string } inputStr - input string
@@ -308,7 +304,7 @@ export function insertAfterElement(referenceNode: HTMLElement, newNode: HTMLElem
308304 */
309305export function windowScrollPosition ( ) : { left : number ; top : number } {
310306 return {
311- left : window . pageXOffset || document . documentElement . scrollLeft || 0 ,
312- top : window . pageYOffset || document . documentElement . scrollTop || 0 ,
307+ left : window . pageXOffset ,
308+ top : window . pageYOffset ,
313309 } ;
314310}
0 commit comments