Skip to content

Commit 7951250

Browse files
committed
version 3.1.0
1 parent fafb969 commit 7951250

File tree

10 files changed

+417
-243
lines changed

10 files changed

+417
-243
lines changed

dist/hmpl.js

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
const FORM_DATA = `formData`;
2828
const DISALLOWED_TAGS = `disallowedTags`;
2929
const SANITIZE = `sanitize`;
30+
const SANITIZE_CONFIG = "sanitizeConfig";
3031
const ALLOWED_CONTENT_TYPES = "allowedContentTypes";
3132
const REQUEST_INIT_GET = `get`;
3233
const INTERVAL = `interval`;
@@ -163,10 +164,12 @@
163164
* @param str - The string to parse.
164165
* @returns The first child node of the parsed template.
165166
*/
166-
const getTemplateWrapper = (str, sanitize = false) => {
167+
const getTemplateWrapper = (str, sanitize = false, sanitizeConfig) => {
167168
let sanitizedStr = str;
168169
if (sanitize) {
169-
sanitizedStr = DOMPurify.sanitize(str);
170+
sanitizedStr = sanitizeConfig
171+
? DOMPurify.sanitize(str, sanitizeConfig)
172+
: DOMPurify.sanitize(str);
170173
}
171174
const elementDocument = new DOMParser().parseFromString(
172175
`<template>${sanitizedStr}</template>`,
@@ -182,8 +185,13 @@
182185
* @param sanitize - Sanitize the response content, ensuring it is safe to render.
183186
* @returns The parsed template wrapper.
184187
*/
185-
const getResponseElements = (response, disallowedTags = [], sanitize) => {
186-
const elWrapper = getTemplateWrapper(response, sanitize);
188+
const getResponseElements = (
189+
response,
190+
disallowedTags = [],
191+
sanitize,
192+
sanitizeConfig
193+
) => {
194+
const elWrapper = getTemplateWrapper(response, sanitize, sanitizeConfig);
187195
const elContent = elWrapper["content"];
188196
for (let i = 0; i < disallowedTags.length; i++) {
189197
const tag = disallowedTags[i];
@@ -212,6 +220,22 @@
212220
}
213221
return !isContain;
214222
};
223+
/**
224+
* Creates parameters for HMPLRequestGet function
225+
* @param prop - The name of the updated property
226+
* @param value - The new property value
227+
* @param context - The context of the current request sent to the HMPLInstance
228+
* @param request - The associated request block helper (for multi-request templates)
229+
* @returns HMPLRequestGetParams object
230+
*/
231+
const createGetParams = (prop, value, context, request) => {
232+
return {
233+
prop,
234+
value,
235+
context,
236+
request
237+
};
238+
};
215239
/**
216240
* Makes an HTTP request and handles the response.
217241
* @param el - The element related to the request.
@@ -227,7 +251,8 @@
227251
* @param allowedContentTypes - Allowed Content-Types for response processing.
228252
* @param disallowedTags - A list of HTML tags that should be removed from the response.
229253
* @param sanitize - A function or method used to sanitize the response content, ensuring it is safe to render.
230-
* @param reqObject - The request object.
254+
* @param sanitizeConfig - Configuration object for the sanitize function from DOMPurify.
255+
* @param reqObject - The block helper.
231256
* @param indicators - Parsed indicators for the request.
232257
*/
233258
const makeRequest = (
@@ -244,6 +269,7 @@
244269
allowedContentTypes,
245270
disallowedTags,
246271
sanitize,
272+
sanitizeConfig,
247273
reqObject,
248274
indicators,
249275
currentClearInterval
@@ -350,9 +376,11 @@
350376
const callGetResponse = (reqResponse) => {
351377
if (isRequests) {
352378
reqObject.response = reqResponse;
353-
get?.("response", reqResponse, requestContext, reqObject);
379+
get?.(
380+
createGetParams("response", reqResponse, requestContext, reqObject)
381+
);
354382
}
355-
get?.("response", mainEl, requestContext);
383+
get?.(createGetParams("response", mainEl, requestContext));
356384
};
357385
/**
358386
* Updates the DOM nodes with new content.
@@ -363,7 +391,7 @@
363391
const updateNodes = (content, isClone = true, isNodes = false) => {
364392
if (isRequest) {
365393
templateObject.response = content.cloneNode(true);
366-
get?.("response", content, requestContext);
394+
get?.(createGetParams("response", content, requestContext));
367395
} else {
368396
let reqResponse = [];
369397
const newContent = isClone ? content.cloneNode(true) : content;
@@ -413,7 +441,7 @@
413441
const setComment = () => {
414442
if (isRequest) {
415443
templateObject.response = undefined;
416-
get?.("response", undefined, requestContext);
444+
get?.(createGetParams("response", undefined, requestContext));
417445
} else {
418446
if (dataObj?.nodes) {
419447
const parentNode = dataObj.parentNode;
@@ -429,9 +457,16 @@
429457
dataObj.parentNode = null;
430458
if (isRequests) {
431459
reqObject.response = undefined;
432-
get?.("response", undefined, requestContext, reqObject);
460+
get?.(
461+
createGetParams(
462+
"response",
463+
undefined,
464+
requestContext,
465+
reqObject
466+
)
467+
);
433468
}
434-
get?.("response", mainEl, requestContext);
469+
get?.(createGetParams("response", mainEl, requestContext));
435470
}
436471
}
437472
if (isRequestMemo) {
@@ -509,12 +544,12 @@
509544
if (isRequests) {
510545
if (reqObject.status !== status) {
511546
reqObject.status = status;
512-
get?.("status", status, requestContext, reqObject);
547+
get?.(createGetParams("status", status, requestContext, reqObject));
513548
}
514549
} else {
515550
if (templateObject.status !== status) {
516551
templateObject.status = status;
517-
get?.("status", status, requestContext);
552+
get?.(createGetParams("status", status, requestContext));
518553
}
519554
}
520555
if (isRequestMemo && getIsNotFullfilledStatus(status)) {
@@ -601,11 +636,14 @@
601636
const templateWrapper = getResponseElements(
602637
data,
603638
disallowedTags,
604-
sanitize
639+
sanitize,
640+
sanitizeConfig
605641
);
606642
if (isRequest) {
607643
templateObject.response = templateWrapper;
608-
get?.("response", templateWrapper, requestContext);
644+
get?.(
645+
createGetParams("response", templateWrapper, requestContext)
646+
);
609647
} else {
610648
const reqResponse = [];
611649
const nodes = [...templateWrapper.content.childNodes];
@@ -623,9 +661,16 @@
623661
parentNode.removeChild(el);
624662
if (isRequests) {
625663
reqObject.response = reqResponse;
626-
get?.("response", reqResponse, requestContext, reqObject);
664+
get?.(
665+
createGetParams(
666+
"response",
667+
reqResponse,
668+
requestContext,
669+
reqObject
670+
)
671+
);
627672
}
628-
get?.("response", mainEl, requestContext);
673+
get?.(createGetParams("response", mainEl, requestContext));
629674
}
630675
}
631676
}
@@ -678,7 +723,7 @@
678723
* Renders the template by processing requests and applying options.
679724
* @param currentEl - The current element or comment node.
680725
* @param fn - The render function.
681-
* @param requests - Array of request objects.
726+
* @param requests - Array of block helpers.
682727
* @param compileOptions - Options provided during compilation.
683728
* @param isMemoUndefined - Indicates if memoization is undefined.
684729
* @param isAutoBodyUndefined - Indicates if autoBody is undefined.
@@ -719,6 +764,7 @@
719764
const interval = req[INTERVAL];
720765
const isReqMemoUndefined = !req.hasOwnProperty(MEMO);
721766
const isReqIntervalUndefined = !req.hasOwnProperty(INTERVAL);
767+
const sanitizeConfig = compileOptions[SANITIZE_CONFIG];
722768
let isMemo = isMemoUndefined ? false : compileOptions[MEMO];
723769
if (!isReqMemoUndefined) {
724770
if (after) {
@@ -1016,6 +1062,7 @@
10161062
allowedContentTypes,
10171063
disallowedTags,
10181064
sanitize,
1065+
sanitizeConfig,
10191066
reqObject,
10201067
indicators,
10211068
currentClearInterval
@@ -1177,7 +1224,7 @@
11771224
const currentRequest = requests[currentIndex];
11781225
if (Number.isNaN(currentIndex) || currentRequest === undefined) {
11791226
createError(
1180-
`${PARSE_ERROR}: Request object with id "${currentIndex}" not found`
1227+
`${PARSE_ERROR}: Block helper with id "${currentIndex}" not found`
11811228
);
11821229
}
11831230
currentRequest.el = currrentElement;
@@ -1557,7 +1604,7 @@
15571604
};
15581605
const templateArr = parseTemplate(template);
15591606
if (requestsIndexes.length === 0)
1560-
createError(`${PARSE_ERROR}: Request object not found`);
1607+
createError(`${PARSE_ERROR}: Block helper not found`);
15611608
const setRequest = (text) => {
15621609
const parsedData = JSON5.parse(text);
15631610
for (const key in parsedData) {

0 commit comments

Comments
 (0)