redoc/src/utils/dom.ts

26 lines
734 B
TypeScript
Raw Normal View History

export const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
export function querySelector(selector: string): Element | null {
if (typeof document !== 'undefined') {
return document.querySelector(selector);
}
return null;
}
/**
* Drop everything inside <...> (i.e., tags/elements), and keep the text.
* Unlike browser innerText, this removes newlines; it also doesn't handle
* un-encoded `<` or `>` characters very well, so don't feed it malformed HTML
*/
export function html2Str(html: string): string {
return html
.split(/<[^>]+>/)
2018-01-22 21:30:53 +03:00
.map(chunk => {
return chunk.trim();
})
2018-01-22 21:30:53 +03:00
.filter(trimmedChunk => {
return trimmedChunk.length > 0;
})
.join(' ');
}