Created
March 2, 2023 18:52
-
-
Save alexbezhan/f7f0b3b3df986c9d692aa51081ec34f6 to your computer and use it in GitHub Desktop.
How to unwrap TRs if they got wrapped in a Tbody by the DOMParser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// unwrap TRs if they got wrapped in a tbody by the parser | |
{ | |
let trsCountInResponseText = 0; | |
let trsCountInResponseFragment = 0; | |
if (responseText.indexOf('<tr') !== -1) { | |
var regExp = new RegExp('<tr', 'gim'); | |
trsCountInResponseText = (responseText.match(regExp) || []).length; | |
for (const elt of responseElts) { | |
if (elt.tagName === 'TR') { | |
trsCountInResponseFragment += 1; | |
} | |
} | |
} | |
if (trsCountInResponseText > 0 && trsCountInResponseFragment === 0) { | |
// parser wrapped rows in a tbody | |
// we didn't ask for that, so we want to unwrap the rows, to be able to apply their swap attributes | |
console.log('trsCountInResponse', trsCountInResponseText, 'trsCountInResponseFragment', trsCountInResponseFragment); | |
let tBodyInResponseFragment = undefined; | |
let tBodyIndexInResponseFragment = -1; | |
for (let i = 0; i < responseElts.length; i++) { | |
const elt = responseElts[i]; | |
if (elt.tagName === 'TBODY') { | |
tBodyInResponseFragment = elt; | |
tBodyIndexInResponseFragment = i; | |
break; | |
} | |
} | |
if (tBodyInResponseFragment !== undefined) { | |
const trs = tBodyInResponseFragment.children; | |
// delete tbody and insert trs instead | |
console.log(`replacing TBODY in response fragment with ${trs.length} TRs`); | |
responseElts.splice(tBodyIndexInResponseFragment, 1, ...trs); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment