Fixed XF2.3 XF.setupHtmlInsert() does not handle strings arguments as XF2.3

Xon

Well-known member
Affected version
XenForo 2.30 Beta 3+
This javacsript works in XF2.2 (it replaces children with the word "ignore") but fails in XF2.3, as XF.createElementFromString returns null instead of the expected text node.
JavaScript:
this.field = this.target || this.$target.get(0);
...
const html = "ignore";
XF.setupHtmlInsert(html, (html) =>
{
    if (html.jquery) {
        html = html.get(0);
    }
    this.field.replaceChildren(html);
});
 
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.3.0 Beta 5).

Change log:
Allow `XF.createElementFromString` to return text nodes
There may be a delay before changes are rolled out to the XenForo Community.
 
XF.createElementFromString looks to drop text nodes if there is 1 HTML element.


Code:
	XF.createElementFromString = (value, parent = null) =>
	{
		const wrappedValue = `<body>${ value }</body>`
		const DOM = new window.DOMParser().parseFromString(wrappedValue, 'text/html').body

		let element

		if (DOM.children.length > 1)
		{
			const container = document.createElement('div')
			container.classList.add('js-createdContainer')
			container.append(...DOM.children)
			element = container
		}
		else
		{
			element = DOM.firstChild
		}

DOM.children is `HtmlElement`s which excludes text nodes. While firstChild is the first text/html element.
 
Back
Top Bottom