Files
tstest/ts_tapbundle/webhelpers.ts

55 lines
1.7 KiB
TypeScript

import * as plugins from './tapbundle.plugins.js';
import { tap } from './tapbundle.classes.tap.js';
class WebHelpers {
html: any;
fixture: any;
constructor() {
const smartenv = new plugins.smartenv.Smartenv();
// Initialize HTML template tag function
this.html = (strings: TemplateStringsArray, ...values: any[]) => {
let result = '';
for (let i = 0; i < strings.length; i++) {
result += strings[i];
if (i < values.length) {
result += values[i];
}
}
return result;
};
// Initialize fixture function based on environment
if (smartenv.isBrowser) {
this.fixture = async <T extends HTMLElement>(htmlString: string): Promise<T> => {
const container = document.createElement('div');
container.innerHTML = htmlString.trim();
const element = container.firstElementChild as T;
// Append to document so custom elements upgrade and lifecycle hooks fire
document.body.appendChild(element);
// Wait for custom element definition if it's a custom element
if (element.localName.includes('-')) {
await customElements.whenDefined(element.localName).catch(() => {});
}
// Wait for Lit/async components to finish rendering
if ((element as any).updateComplete) {
await (element as any).updateComplete;
}
return element;
};
} else {
// Node.js environment - provide a stub or alternative implementation
this.fixture = async (htmlString: string): Promise<any> => {
throw new Error('WebHelpers.fixture is only available in browser environment');
};
}
}
}
export const webhelpers = new WebHelpers();