pulse-js-framework 1.4.10 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,255 @@
1
+ /**
2
+ * Pulse Framework - Utility Functions Type Definitions
3
+ * @module pulse-js-framework/runtime/utils
4
+ */
5
+
6
+ // =============================================================================
7
+ // XSS Prevention
8
+ // =============================================================================
9
+
10
+ /**
11
+ * Escape HTML special characters to prevent XSS attacks.
12
+ * Use this when inserting untrusted content into HTML.
13
+ *
14
+ * Escapes: & < > " '
15
+ *
16
+ * @param str - Value to escape (will be converted to string)
17
+ * @returns Escaped string safe for HTML insertion
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * const safe = escapeHtml('<script>alert("xss")</script>');
22
+ * // Returns: '&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;'
23
+ *
24
+ * element.innerHTML = `<div>${escapeHtml(userInput)}</div>`;
25
+ * ```
26
+ */
27
+ export declare function escapeHtml(str: unknown): string;
28
+
29
+ /**
30
+ * Unescape HTML entities back to their original characters.
31
+ *
32
+ * @param str - HTML-escaped string
33
+ * @returns Unescaped string
34
+ *
35
+ * @example
36
+ * ```typescript
37
+ * const original = unescapeHtml('&lt;div&gt;');
38
+ * // Returns: '<div>'
39
+ * ```
40
+ */
41
+ export declare function unescapeHtml(str: string | null | undefined): string;
42
+
43
+ /**
44
+ * Explicitly set innerHTML with a warning.
45
+ * This function is intentionally named to make it obvious that
46
+ * it bypasses XSS protection.
47
+ *
48
+ * @security Only use this with trusted, sanitized HTML.
49
+ * Never use with user-provided content without proper sanitization.
50
+ *
51
+ * @param element - Target element
52
+ * @param html - HTML string to insert (must be trusted)
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * // Only use with trusted HTML!
57
+ * dangerouslySetInnerHTML(container, sanitizedHtml);
58
+ * ```
59
+ */
60
+ export declare function dangerouslySetInnerHTML(element: HTMLElement, html: string): void;
61
+
62
+ /**
63
+ * Create a text node from a value, safely escaping it.
64
+ * This is the recommended way to insert dynamic text content.
65
+ *
66
+ * Note: DOM textContent is already safe from XSS, but this function
67
+ * provides a consistent API and handles null/undefined values.
68
+ *
69
+ * @param value - Value to convert to text node
70
+ * @returns Safe text node
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * const node = createSafeTextNode(userInput);
75
+ * container.appendChild(node);
76
+ * ```
77
+ */
78
+ export declare function createSafeTextNode(value: unknown): Text;
79
+
80
+ // =============================================================================
81
+ // Attribute Handling
82
+ // =============================================================================
83
+
84
+ /**
85
+ * Escape a value for use in an HTML attribute.
86
+ * Escapes quotes and special characters.
87
+ *
88
+ * @param value - Value to escape
89
+ * @returns Escaped string safe for attribute values
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * const safe = escapeAttribute(userInput);
94
+ * element.setAttribute('data-value', safe);
95
+ * ```
96
+ */
97
+ export declare function escapeAttribute(value: unknown): string;
98
+
99
+ /**
100
+ * Safely set an attribute on an element.
101
+ * Validates attribute names to prevent attribute injection.
102
+ * Blocks dangerous event handler attributes (onclick, onerror, etc.).
103
+ *
104
+ * @param element - Target element
105
+ * @param name - Attribute name
106
+ * @param value - Attribute value
107
+ * @returns true if attribute was set, false if blocked
108
+ *
109
+ * @example
110
+ * ```typescript
111
+ * if (!safeSetAttribute(element, attrName, attrValue)) {
112
+ * console.warn('Attribute was blocked');
113
+ * }
114
+ * ```
115
+ */
116
+ export declare function safeSetAttribute(
117
+ element: HTMLElement,
118
+ name: string,
119
+ value: unknown
120
+ ): boolean;
121
+
122
+ // =============================================================================
123
+ // URL Validation
124
+ // =============================================================================
125
+
126
+ /** Options for URL sanitization */
127
+ export interface SanitizeUrlOptions {
128
+ /** Allow data: URLs (default: false) */
129
+ allowData?: boolean;
130
+ /** Allow relative URLs (default: true) */
131
+ allowRelative?: boolean;
132
+ }
133
+
134
+ /**
135
+ * Validate and sanitize a URL to prevent javascript: and data: XSS.
136
+ * Only allows http:, https:, and optionally relative URLs.
137
+ *
138
+ * @param url - URL to validate
139
+ * @param options - Validation options
140
+ * @returns Sanitized URL or null if invalid/dangerous
141
+ *
142
+ * @example
143
+ * ```typescript
144
+ * const safeUrl = sanitizeUrl(userProvidedUrl);
145
+ * if (safeUrl) {
146
+ * link.href = safeUrl;
147
+ * } else {
148
+ * console.warn('Invalid or dangerous URL');
149
+ * }
150
+ *
151
+ * // Allow data: URLs for images
152
+ * const imageUrl = sanitizeUrl(dataUrl, { allowData: true });
153
+ * ```
154
+ */
155
+ export declare function sanitizeUrl(
156
+ url: string | null | undefined,
157
+ options?: SanitizeUrlOptions
158
+ ): string | null;
159
+
160
+ // =============================================================================
161
+ // Deep Clone
162
+ // =============================================================================
163
+
164
+ /**
165
+ * Deep clone an object or array.
166
+ * Handles nested objects, arrays, dates, and primitive values.
167
+ *
168
+ * Note: Does not clone functions, symbols, or circular references.
169
+ *
170
+ * @template T - Type of the value to clone
171
+ * @param obj - Object to clone
172
+ * @returns Deep clone of the object
173
+ *
174
+ * @example
175
+ * ```typescript
176
+ * const clone = deepClone({ a: { b: [1, 2, 3] } });
177
+ * clone.a.b.push(4); // Doesn't affect original
178
+ * ```
179
+ */
180
+ export declare function deepClone<T>(obj: T): T;
181
+
182
+ // =============================================================================
183
+ // Debounce / Throttle
184
+ // =============================================================================
185
+
186
+ /** Function with cancel method */
187
+ export interface CancellableFunction<T extends (...args: unknown[]) => unknown> {
188
+ (...args: Parameters<T>): void;
189
+ /** Cancel any pending execution */
190
+ cancel(): void;
191
+ }
192
+
193
+ /**
194
+ * Create a debounced version of a function.
195
+ * The function will only be called after the specified delay
196
+ * has passed without any new calls.
197
+ *
198
+ * @param fn - Function to debounce
199
+ * @param delay - Delay in milliseconds
200
+ * @returns Debounced function with cancel method
201
+ *
202
+ * @example
203
+ * ```typescript
204
+ * const debouncedSearch = debounce(search, 300);
205
+ * input.addEventListener('input', debouncedSearch);
206
+ *
207
+ * // Cancel pending call if needed
208
+ * debouncedSearch.cancel();
209
+ * ```
210
+ */
211
+ export declare function debounce<T extends (...args: unknown[]) => unknown>(
212
+ fn: T,
213
+ delay: number
214
+ ): CancellableFunction<T>;
215
+
216
+ /**
217
+ * Create a throttled version of a function.
218
+ * The function will be called at most once per specified interval.
219
+ *
220
+ * @param fn - Function to throttle
221
+ * @param interval - Minimum interval between calls in milliseconds
222
+ * @returns Throttled function with cancel method
223
+ *
224
+ * @example
225
+ * ```typescript
226
+ * const throttledScroll = throttle(handleScroll, 100);
227
+ * window.addEventListener('scroll', throttledScroll);
228
+ *
229
+ * // Cancel pending call if needed
230
+ * throttledScroll.cancel();
231
+ * ```
232
+ */
233
+ export declare function throttle<T extends (...args: unknown[]) => unknown>(
234
+ fn: T,
235
+ interval: number
236
+ ): CancellableFunction<T>;
237
+
238
+ // =============================================================================
239
+ // Default Export
240
+ // =============================================================================
241
+
242
+ declare const utils: {
243
+ escapeHtml: typeof escapeHtml;
244
+ unescapeHtml: typeof unescapeHtml;
245
+ dangerouslySetInnerHTML: typeof dangerouslySetInnerHTML;
246
+ createSafeTextNode: typeof createSafeTextNode;
247
+ escapeAttribute: typeof escapeAttribute;
248
+ safeSetAttribute: typeof safeSetAttribute;
249
+ sanitizeUrl: typeof sanitizeUrl;
250
+ deepClone: typeof deepClone;
251
+ debounce: typeof debounce;
252
+ throttle: typeof throttle;
253
+ };
254
+
255
+ export default utils;