|
27 | 27 | const FORM_DATA = `formData`; |
28 | 28 | const DISALLOWED_TAGS = `disallowedTags`; |
29 | 29 | const SANITIZE = `sanitize`; |
| 30 | + const SANITIZE_CONFIG = "sanitizeConfig"; |
30 | 31 | const ALLOWED_CONTENT_TYPES = "allowedContentTypes"; |
31 | 32 | const REQUEST_INIT_GET = `get`; |
32 | 33 | const INTERVAL = `interval`; |
|
163 | 164 | * @param str - The string to parse. |
164 | 165 | * @returns The first child node of the parsed template. |
165 | 166 | */ |
166 | | - const getTemplateWrapper = (str, sanitize = false) => { |
| 167 | + const getTemplateWrapper = (str, sanitize = false, sanitizeConfig) => { |
167 | 168 | let sanitizedStr = str; |
168 | 169 | if (sanitize) { |
169 | | - sanitizedStr = DOMPurify.sanitize(str); |
| 170 | + sanitizedStr = sanitizeConfig |
| 171 | + ? DOMPurify.sanitize(str, sanitizeConfig) |
| 172 | + : DOMPurify.sanitize(str); |
170 | 173 | } |
171 | 174 | const elementDocument = new DOMParser().parseFromString( |
172 | 175 | `<template>${sanitizedStr}</template>`, |
|
182 | 185 | * @param sanitize - Sanitize the response content, ensuring it is safe to render. |
183 | 186 | * @returns The parsed template wrapper. |
184 | 187 | */ |
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); |
187 | 195 | const elContent = elWrapper["content"]; |
188 | 196 | for (let i = 0; i < disallowedTags.length; i++) { |
189 | 197 | const tag = disallowedTags[i]; |
|
212 | 220 | } |
213 | 221 | return !isContain; |
214 | 222 | }; |
| 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 | + }; |
215 | 239 | /** |
216 | 240 | * Makes an HTTP request and handles the response. |
217 | 241 | * @param el - The element related to the request. |
|
227 | 251 | * @param allowedContentTypes - Allowed Content-Types for response processing. |
228 | 252 | * @param disallowedTags - A list of HTML tags that should be removed from the response. |
229 | 253 | * @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. |
231 | 256 | * @param indicators - Parsed indicators for the request. |
232 | 257 | */ |
233 | 258 | const makeRequest = ( |
|
244 | 269 | allowedContentTypes, |
245 | 270 | disallowedTags, |
246 | 271 | sanitize, |
| 272 | + sanitizeConfig, |
247 | 273 | reqObject, |
248 | 274 | indicators, |
249 | 275 | currentClearInterval |
|
350 | 376 | const callGetResponse = (reqResponse) => { |
351 | 377 | if (isRequests) { |
352 | 378 | reqObject.response = reqResponse; |
353 | | - get?.("response", reqResponse, requestContext, reqObject); |
| 379 | + get?.( |
| 380 | + createGetParams("response", reqResponse, requestContext, reqObject) |
| 381 | + ); |
354 | 382 | } |
355 | | - get?.("response", mainEl, requestContext); |
| 383 | + get?.(createGetParams("response", mainEl, requestContext)); |
356 | 384 | }; |
357 | 385 | /** |
358 | 386 | * Updates the DOM nodes with new content. |
|
363 | 391 | const updateNodes = (content, isClone = true, isNodes = false) => { |
364 | 392 | if (isRequest) { |
365 | 393 | templateObject.response = content.cloneNode(true); |
366 | | - get?.("response", content, requestContext); |
| 394 | + get?.(createGetParams("response", content, requestContext)); |
367 | 395 | } else { |
368 | 396 | let reqResponse = []; |
369 | 397 | const newContent = isClone ? content.cloneNode(true) : content; |
|
413 | 441 | const setComment = () => { |
414 | 442 | if (isRequest) { |
415 | 443 | templateObject.response = undefined; |
416 | | - get?.("response", undefined, requestContext); |
| 444 | + get?.(createGetParams("response", undefined, requestContext)); |
417 | 445 | } else { |
418 | 446 | if (dataObj?.nodes) { |
419 | 447 | const parentNode = dataObj.parentNode; |
|
429 | 457 | dataObj.parentNode = null; |
430 | 458 | if (isRequests) { |
431 | 459 | reqObject.response = undefined; |
432 | | - get?.("response", undefined, requestContext, reqObject); |
| 460 | + get?.( |
| 461 | + createGetParams( |
| 462 | + "response", |
| 463 | + undefined, |
| 464 | + requestContext, |
| 465 | + reqObject |
| 466 | + ) |
| 467 | + ); |
433 | 468 | } |
434 | | - get?.("response", mainEl, requestContext); |
| 469 | + get?.(createGetParams("response", mainEl, requestContext)); |
435 | 470 | } |
436 | 471 | } |
437 | 472 | if (isRequestMemo) { |
|
509 | 544 | if (isRequests) { |
510 | 545 | if (reqObject.status !== status) { |
511 | 546 | reqObject.status = status; |
512 | | - get?.("status", status, requestContext, reqObject); |
| 547 | + get?.(createGetParams("status", status, requestContext, reqObject)); |
513 | 548 | } |
514 | 549 | } else { |
515 | 550 | if (templateObject.status !== status) { |
516 | 551 | templateObject.status = status; |
517 | | - get?.("status", status, requestContext); |
| 552 | + get?.(createGetParams("status", status, requestContext)); |
518 | 553 | } |
519 | 554 | } |
520 | 555 | if (isRequestMemo && getIsNotFullfilledStatus(status)) { |
|
601 | 636 | const templateWrapper = getResponseElements( |
602 | 637 | data, |
603 | 638 | disallowedTags, |
604 | | - sanitize |
| 639 | + sanitize, |
| 640 | + sanitizeConfig |
605 | 641 | ); |
606 | 642 | if (isRequest) { |
607 | 643 | templateObject.response = templateWrapper; |
608 | | - get?.("response", templateWrapper, requestContext); |
| 644 | + get?.( |
| 645 | + createGetParams("response", templateWrapper, requestContext) |
| 646 | + ); |
609 | 647 | } else { |
610 | 648 | const reqResponse = []; |
611 | 649 | const nodes = [...templateWrapper.content.childNodes]; |
|
623 | 661 | parentNode.removeChild(el); |
624 | 662 | if (isRequests) { |
625 | 663 | reqObject.response = reqResponse; |
626 | | - get?.("response", reqResponse, requestContext, reqObject); |
| 664 | + get?.( |
| 665 | + createGetParams( |
| 666 | + "response", |
| 667 | + reqResponse, |
| 668 | + requestContext, |
| 669 | + reqObject |
| 670 | + ) |
| 671 | + ); |
627 | 672 | } |
628 | | - get?.("response", mainEl, requestContext); |
| 673 | + get?.(createGetParams("response", mainEl, requestContext)); |
629 | 674 | } |
630 | 675 | } |
631 | 676 | } |
|
678 | 723 | * Renders the template by processing requests and applying options. |
679 | 724 | * @param currentEl - The current element or comment node. |
680 | 725 | * @param fn - The render function. |
681 | | - * @param requests - Array of request objects. |
| 726 | + * @param requests - Array of block helpers. |
682 | 727 | * @param compileOptions - Options provided during compilation. |
683 | 728 | * @param isMemoUndefined - Indicates if memoization is undefined. |
684 | 729 | * @param isAutoBodyUndefined - Indicates if autoBody is undefined. |
|
719 | 764 | const interval = req[INTERVAL]; |
720 | 765 | const isReqMemoUndefined = !req.hasOwnProperty(MEMO); |
721 | 766 | const isReqIntervalUndefined = !req.hasOwnProperty(INTERVAL); |
| 767 | + const sanitizeConfig = compileOptions[SANITIZE_CONFIG]; |
722 | 768 | let isMemo = isMemoUndefined ? false : compileOptions[MEMO]; |
723 | 769 | if (!isReqMemoUndefined) { |
724 | 770 | if (after) { |
|
1016 | 1062 | allowedContentTypes, |
1017 | 1063 | disallowedTags, |
1018 | 1064 | sanitize, |
| 1065 | + sanitizeConfig, |
1019 | 1066 | reqObject, |
1020 | 1067 | indicators, |
1021 | 1068 | currentClearInterval |
|
1177 | 1224 | const currentRequest = requests[currentIndex]; |
1178 | 1225 | if (Number.isNaN(currentIndex) || currentRequest === undefined) { |
1179 | 1226 | createError( |
1180 | | - `${PARSE_ERROR}: Request object with id "${currentIndex}" not found` |
| 1227 | + `${PARSE_ERROR}: Block helper with id "${currentIndex}" not found` |
1181 | 1228 | ); |
1182 | 1229 | } |
1183 | 1230 | currentRequest.el = currrentElement; |
|
1557 | 1604 | }; |
1558 | 1605 | const templateArr = parseTemplate(template); |
1559 | 1606 | if (requestsIndexes.length === 0) |
1560 | | - createError(`${PARSE_ERROR}: Request object not found`); |
| 1607 | + createError(`${PARSE_ERROR}: Block helper not found`); |
1561 | 1608 | const setRequest = (text) => { |
1562 | 1609 | const parsedData = JSON5.parse(text); |
1563 | 1610 | for (const key in parsedData) { |
|
0 commit comments