Skip to content

Commit

Permalink
🐛 remove child with action-names innerText instead of replacing them
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Jan 6, 2022
1 parent 1212ecb commit 870a700
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,17 @@ describe('getActionNameFromElement', () => {
).toBe('bar')
})

it('replaces the programmatic action name in textual content', () => {
it('remove children with programmatic action name in textual content', () => {
expect(getActionNameFromElement(element`<div>Foo <div data-dd-action-name="custom action">bar<div></div>`)).toBe(
'Foo custom action'
'Foo'
)
})

it('replaces the programmatic action name in textual content based on the user-configured attribute', () => {
// eslint-disable-next-line max-len
it('remove children with programmatic action name in textual content based on the user-configured attribute', () => {
expect(
getActionNameFromElement(element`<div>Foo <div data-test-id="custom action">bar<div></div>`, 'data-test-id')
).toBe('Foo custom action')
).toBe('Foo')
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ function getTextualContent(element: Element | HTMLElement, userProgrammaticAttri
if ('innerText' in element) {
let text = element.innerText

const replaceTextFromElements = (query: string, replacer: (element: Element) => string) => {
const removeTextFromElements = (query: string) => {
const list = element.querySelectorAll<Element | HTMLElement>(query)
for (let index = 0; index < list.length; index += 1) {
const element = list[index]
if ('innerText' in element) {
const textToReplace = element.innerText
if (textToReplace && textToReplace.trim().length > 0) {
text = text.replace(textToReplace, replacer(element))
text = text.replace(textToReplace, '')
}
}
}
Expand All @@ -189,20 +189,14 @@ function getTextualContent(element: Element | HTMLElement, userProgrammaticAttri
if (!supportsInnerTextScriptAndStyleRemoval()) {
// remove the inner text of SCRIPT and STYLES from the result. This is a bit dirty, but should
// be relatively fast and work in most cases.
replaceTextFromElements('script, style', () => '')
removeTextFromElements('script, style')
}

// replace the text of elements with their programmatic attribute value
replaceTextFromElements(
`[${DEFAULT_PROGRAMMATIC_ATTRIBUTE}]`,
(element) => element.getAttribute(DEFAULT_PROGRAMMATIC_ATTRIBUTE)!
)
// remove the text of elements with programmatic attribute value
removeTextFromElements(`[${DEFAULT_PROGRAMMATIC_ATTRIBUTE}]`)

if (userProgrammaticAttribute) {
replaceTextFromElements(
`[${userProgrammaticAttribute}]`,
(element) => element.getAttribute(userProgrammaticAttribute)!
)
removeTextFromElements(`[${userProgrammaticAttribute}]`)
}

return text
Expand Down

0 comments on commit 870a700

Please sign in to comment.