@medyll/idae-be
Version:
A modern, lightweight, and extensible DOM manipulation library built with TypeScript. Designed for precise element targeting and manipulation using a callback-based approach. Features include advanced DOM traversal, event handling, style management, attri
125 lines (124 loc) • 4.58 kB
JavaScript
import { createBe } from './be.js';
export class BeUtils {
static isHTML(str, options) {
const result = {
isHtml: false,
tag: '',
attributes: {},
styles: {},
node: undefined,
beElem: undefined
};
if (str instanceof HTMLElement)
result.node = str;
if (typeof str !== 'string')
return result;
const trimmed = str.trim();
if (options.transformTextToHtml ||
(trimmed.startsWith('<') && trimmed.endsWith('>') && trimmed.includes('</'))) {
result.isHtml = true;
// extract tag
const tagMatch = trimmed.match(/<(\w+)/);
result.tag = tagMatch?.[1] ?? 'span';
// extract attributes
const attributesMatch = trimmed.match(/<\w+\s+([^>]+)>/);
if (attributesMatch) {
const attributesString = attributesMatch[1];
const attributeRegex = /(\w+)(?:="([^"]*)")?/g;
let match;
while ((match = attributeRegex.exec(attributesString)) !== null) {
const [, key, value] = match;
if (key === 'style') {
// Traiter les styles séparément
const styleRegex = /(\w+-?\w+)\s*:\s*([^;]+)/g;
let styleMatch;
while ((styleMatch = styleRegex.exec(value)) !== null) {
const [, styleName, styleValue] = styleMatch;
result.styles[styleName] = styleValue.trim();
}
}
else {
result.attributes[key] = value || '';
}
}
}
if (result.isHtml && options.returnHTMLelement) {
const html = str.replace(/<[^>]+>/, '').replace(/<\/[^>]+>$/, '');
result.beElem = createBe(result.tag);
result.beElem.update(html);
if (result.styles)
result.beElem.setStyle(result.styles);
// if (result?.attributes) result.beElem.setAttr(result?.attributes);
const newElement = document.createElement(result.tag);
newElement.innerHTML = html;
result.node = newElement;
}
}
if (options.transformTextToHtml && !result.isHtml) {
const newElement = document.createElement('span');
newElement.innerHTML = str;
result.node = newElement;
}
return result;
}
static calculateAnchorPoint(rect, anchor) {
let x = rect.left; // Valeur par défaut pour x
let y = rect.top; // Valeur par défaut pour y
if (typeof anchor === 'string') {
const [vertical, horizontal] = anchor.split(' ');
switch (vertical) {
case 'top':
y = rect.top;
break;
case 'bottom':
y = rect.bottom;
break;
case 'center':
y = rect.top + rect.height / 2;
x = rect.left + rect.width / 2;
break;
}
switch (horizontal) {
case 'left':
x = rect.left;
break;
case 'right':
x = rect.right;
break;
case 'center':
x = rect.left + rect.width / 2;
break;
}
}
else {
throw new Error('Invalid anchor type. Expected a string.');
}
return [x, y];
}
static applyStyle(beElement, property, value) {
beElement.eachNode((el) => {
el.style.setProperty(property, value);
});
}
static applyCallback(el, callback) {
if (el instanceof HTMLCollection) {
return Array.from(el).forEach((ss) => {
BeUtils.applyCallback(ss, callback);
});
}
else {
return callback(el);
}
}
static resolveIndirection(classHandler, actions) {
let method;
let props;
Object.keys(actions).forEach((action) => {
if (classHandler.methods.includes(action)) {
method = action;
props = actions[action];
}
});
return { method, props };
}
}