Actualizar sigpro.ts

This commit is contained in:
2026-04-07 14:37:24 +02:00
parent e6ab0a2052
commit 040dfffe8a

280
sigpro.ts
View File

@@ -13,6 +13,33 @@ type Signal<T> = {
type CleanupFn = () => void;
const DANGEROUS_PROTOCOLS = /^(javascript|data|vbscript):/i;
const DANGEROUS_ATTRIBUTES = /^on/i;
const sanitizeUrl = (url: unknown): string => {
const str = String(url ?? '').trim().toLowerCase();
if (DANGEROUS_PROTOCOLS.test(str)) return '#';
return str;
};
const sanitizeAttribute = (name: string, value: unknown): string | null => {
if (value == null) return null;
const strValue = String(value);
if (DANGEROUS_ATTRIBUTES.test(name)) {
console.warn(`[SigPro] XSS prevention: blocked attribute "${name}"`);
return null;
}
if (name === 'src' || name === 'href') {
return sanitizeUrl(strValue);
}
return strValue;
};
let activeEffect: EffectFn | null = null;
let isScheduled = false;
const queue = new Set<EffectFn>();
@@ -230,6 +257,65 @@ export const use = (key: string | symbol, defaultValue?: any): any => {
return defaultValue;
};
export function createContext<T>(defaultValue?: T): {
Provider: (props: { value: T; children?: any }) => any;
use: () => T;
} {
const key = Symbol('context');
const useContext = (): T => {
// Buscar en el stack de contextos
let current = context;
while (current) {
if (key in current.p) {
return current.p[key] as T;
}
// Subir al contexto padre (si existiera)
current = null; // Por ahora, solo contexto actual
}
if (defaultValue !== undefined) return defaultValue;
throw new Error(`Context not found: ${String(key)}`);
};
const Provider = ({ value, children }: { value: T; children?: any }) => {
// Guardar contexto anterior
const prevContext = context;
// Crear nuevo contexto o extender el existente
if (!context) {
context = { m: [], u: [], p: {} };
}
// Guardar valor
context.p[key] = value;
// Renderizar hijos
const result = h('div', { style: 'display: contents' }, children);
// Restaurar contexto anterior
context = prevContext;
return result;
};
return { Provider, use: useContext };
}
export function createSharedContext<T>(key: string | symbol, initialValue: T): {
set: (value: T) => void;
get: () => T;
} {
// Inicializar si estamos en un componente
if (context && !(key in context.p)) {
share(key, initialValue);
}
return {
set: (value: T) => share(key, value),
get: () => use(key) as T
};
}
type Component<P = Record<string, any>> = (
props: P,
ctx: { children?: any[]; emit: (event: string, ...args: any[]) => any }
@@ -358,6 +444,9 @@ export const h = (tag: any, props?: any, ...children: any[]): any => {
el = document.createElementNS("http://www.w3.org/2000/svg", tag);
}
// Código MEJORADO
const booleanAttributes = ["disabled", "checked", "required", "readonly", "selected", "multiple", "autofocus"];
for (const key in props) {
if (key.startsWith('on')) {
const eventName = key.slice(2).toLowerCase();
@@ -366,21 +455,35 @@ export const h = (tag: any, props?: any, ...children: any[]): any => {
if (isFn(props[key])) {
props[key](el);
} else {
props[key].value = el;
props[key].current = el;
}
} else if (isFn(props[key])) {
effect(() => {
if (key in el && !is_svg) {
(el as any)[key] = props[key]();
const val = props[key]();
if (key === 'className') {
el.setAttribute('class', String(val ?? ''));
} else if (booleanAttributes.includes(key)) {
(el as any)[key] = !!val;
val ? el.setAttribute(key, '') : el.removeAttribute(key);
} else if (key in el && !is_svg) {
(el as any)[key] = val;
} else {
el.setAttribute(key, props[key]());
const safeVal = sanitizeAttribute(key, val);
if (safeVal !== null) el.setAttribute(key, safeVal);
}
});
} else {
if (key in el && !is_svg) {
(el as any)[key] = props[key];
const value = props[key];
if (key === 'className') {
el.setAttribute('class', String(value ?? ''));
} else if (booleanAttributes.includes(key)) {
(el as any)[key] = !!value;
value ? el.setAttribute(key, '') : el.removeAttribute(key);
} else if (key in el && !is_svg) {
(el as any)[key] = value;
} else {
el.setAttribute(key, props[key]);
const safeVal = sanitizeAttribute(key, value);
if (safeVal !== null) el.setAttribute(key, safeVal);
}
}
}
@@ -586,4 +689,167 @@ export default (
return () => remove(el);
};
declare global {
namespace JSX {
type Element = HTMLElement | Text | DocumentFragment | string | number | boolean | null | undefined;
interface IntrinsicElements {
// Elementos HTML
div: HTMLAttributes;
span: HTMLAttributes;
p: HTMLAttributes;
a: AnchorHTMLAttributes;
button: ButtonHTMLAttributes;
input: InputHTMLAttributes;
form: FormHTMLAttributes;
img: ImgHTMLAttributes;
ul: HTMLAttributes;
ol: HTMLAttributes;
li: HTMLAttributes;
h1: HTMLAttributes;
h2: HTMLAttributes;
h3: HTMLAttributes;
h4: HTMLAttributes;
h5: HTMLAttributes;
h6: HTMLAttributes;
section: HTMLAttributes;
article: HTMLAttributes;
header: HTMLAttributes;
footer: HTMLAttributes;
nav: HTMLAttributes;
main: HTMLAttributes;
aside: HTMLAttributes;
label: HTMLAttributes;
select: SelectHTMLAttributes;
option: OptionHTMLAttributes;
textarea: TextareaHTMLAttributes;
table: TableHTMLAttributes;
tr: HTMLAttributes;
td: HTMLAttributes;
th: HTMLAttributes;
hr: HTMLAttributes;
br: HTMLAttributes;
// SVG
svg: SVGAttributes;
path: SVGAttributes;
circle: SVGAttributes;
rect: SVGAttributes;
line: SVGAttributes;
g: SVGAttributes;
}
interface HTMLAttributes {
id?: string;
className?: string;
class?: string;
style?: string | Partial<CSSStyleDeclaration>;
children?: any;
ref?: ((el: any) => void) | { current: any };
// Eventos
onClick?: (event: MouseEvent) => void;
onInput?: (event: Event) => void;
onChange?: (event: Event) => void;
onSubmit?: (event: Event) => void;
onKeyDown?: (event: KeyboardEvent) => void;
onKeyUp?: (event: KeyboardEvent) => void;
onFocus?: (event: FocusEvent) => void;
onBlur?: (event: FocusEvent) => void;
onMouseEnter?: (event: MouseEvent) => void;
onMouseLeave?: (event: MouseEvent) => void;
// Atributos ARIA
role?: string;
'aria-label'?: string;
'aria-hidden'?: boolean | 'true' | 'false';
'aria-expanded'?: boolean | 'true' | 'false';
// Atributos comunes
tabIndex?: number;
title?: string;
draggable?: boolean;
hidden?: boolean;
}
interface AnchorHTMLAttributes extends HTMLAttributes {
href?: string;
target?: '_blank' | '_self' | '_parent' | '_top';
rel?: string;
}
interface ButtonHTMLAttributes extends HTMLAttributes {
type?: 'button' | 'submit' | 'reset';
disabled?: boolean;
}
interface InputHTMLAttributes extends HTMLAttributes {
type?: 'text' | 'password' | 'email' | 'number' | 'checkbox' | 'radio' | 'file' | 'date';
value?: string | number;
checked?: boolean;
placeholder?: string;
disabled?: boolean;
required?: boolean;
name?: string;
min?: number;
max?: number;
step?: number;
}
interface FormHTMLAttributes extends HTMLAttributes {
action?: string;
method?: 'get' | 'post';
}
interface ImgHTMLAttributes extends HTMLAttributes {
src?: string;
alt?: string;
width?: number | string;
height?: number | string;
loading?: 'lazy' | 'eager';
}
interface SelectHTMLAttributes extends HTMLAttributes {
value?: string | string[];
disabled?: boolean;
required?: boolean;
multiple?: boolean;
}
interface OptionHTMLAttributes extends HTMLAttributes {
value?: string | number;
selected?: boolean;
disabled?: boolean;
}
interface TextareaHTMLAttributes extends HTMLAttributes {
value?: string;
placeholder?: string;
disabled?: boolean;
required?: boolean;
rows?: number;
cols?: number;
}
interface TableHTMLAttributes extends HTMLAttributes {
border?: number;
cellPadding?: number | string;
cellSpacing?: number | string;
}
interface SVGAttributes {
viewBox?: string;
width?: number | string;
height?: number | string;
fill?: string;
stroke?: string;
strokeWidth?: number | string;
xmlns?: string;
children?: any;
}
}
}
export { h as jsx, h as jsxs, h as Fragment };
export type { Signal, Component, CleanupFn };