Skip to content

Commit d6c80fa

Browse files
committed
Add errorMessageListItemWithHTML to parse an HTML string for rendering
* For cases where we need to render rich error markup (such as server- rendered errors), this method provides us with a (mostly) safe way of doing so by utilizing `DOMParser` * See: https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString
1 parent 2a864ad commit d6c80fa

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/rendering.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ export function errorMessageListItem(message, type) {
7070
return clone
7171
}
7272

73+
/**
74+
* Clones the {@link errorListItemTemplate}, parsing the given HTML string and inserting it into the element with `[data-pf-error-message]` to the message,
75+
* and setting the `data-pf-error-type` to the given type.
76+
*
77+
* @params {string} htmlString the HTML content to render in the string
78+
* @params {string} type the value that will be used for `data-pf-error-type``
79+
* @returns {Element} the cloned element
80+
*/
81+
export function errorMessageListItemWithHTML(htmlString, type) {
82+
const clone = errorListItemTemplate().content.cloneNode(true).querySelector(`*:first-child`)
83+
84+
const parsedDocument = new DOMParser().parseFromString(htmlString, "text/html")
85+
86+
clone.setAttribute(`data-pf-error-type`, type)
87+
clone.querySelector(`[data-pf-error-message]`).replaceChildren(parsedDocument.body.firstChild)
88+
return clone
89+
}
90+
7391
/**
7492
* Clears the error messages from the list, while keeping preserved errors.
7593
* Removes the `data-pf-error-visible` attribute from any preserved attributes

test/rendering.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,30 @@ suite('Rendering', async () => {
8787
assert.equal(true, itemElement.hasAttribute(`data-pf-error-preserve`))
8888
})
8989

90+
test(`errorMessageListItemWithHTML`, async() => {
91+
const container = await fixture(html`
92+
<div>
93+
<p>Some content</p>
94+
95+
<template id="pf-error-list-item-template">
96+
<li><span>‼️</span> <span data-pf-error-message></span></li>
97+
</template>
98+
</div>
99+
`)
100+
101+
const htmlString = `<strong>Richly <a href="https://example.com">rendered</a> errors!</strong>`
102+
103+
const itemElement = Rendering.errorMessageListItemWithHTML(htmlString, "a-custom-type")
104+
105+
assert.equal("a-custom-type", itemElement.getAttribute(`data-pf-error-type`))
106+
assert.equal(htmlString, itemElement.querySelector(`[data-pf-error-message]`).innerHTML)
107+
assert.equal("‼️ Richly rendered errors!", itemElement.textContent)
108+
assert.equal(false, itemElement.hasAttribute(`data-pf-error-preserve`))
109+
110+
itemElement.setAttribute(`data-pf-error-preserve`, true)
111+
assert.equal(true, itemElement.hasAttribute(`data-pf-error-preserve`))
112+
})
113+
90114
test(`renderConstraintValidationMessageForElement: the input is valid`, async () => {
91115
const container = await fixture(html`
92116
<div>

0 commit comments

Comments
 (0)