38 lines
1 KiB
JavaScript
38 lines
1 KiB
JavaScript
/**
|
|
* Utility functions for njsparser
|
|
*/
|
|
|
|
/**
|
|
* Parses HTML string using DOMParser and returns the document
|
|
* @param {string} html - HTML string to parse
|
|
* @param {DOMParser} DOMParser - DOMParser instance
|
|
* @returns {Document} Parsed DOM document
|
|
*/
|
|
export function makeTree(html, DOMParser) {
|
|
if (typeof html !== 'string') {
|
|
throw new TypeError(`Expected string, got ${typeof html}`);
|
|
}
|
|
const parser = new DOMParser();
|
|
return parser.parseFromString(html, 'text/html');
|
|
}
|
|
|
|
/**
|
|
* Joins URL parts, handling slashes correctly
|
|
* @param {...string} parts - URL parts to join
|
|
* @returns {string} Joined URL path
|
|
*/
|
|
export function join(...parts) {
|
|
const cleaned = [];
|
|
for (const part of parts) {
|
|
if (typeof part !== 'string') continue;
|
|
const trimmed = part.replace(/^\/+|\/+$/g, '');
|
|
if (trimmed) cleaned.push(trimmed);
|
|
}
|
|
if (cleaned.length === 0) return '/';
|
|
return `/${cleaned.join('/')}`;
|
|
}
|
|
|
|
/**
|
|
* Path to test data directory (relative to project root)
|
|
*/
|
|
export const TEST_DATA_PATH = '../test/src';
|