Skip to content

Commit

Permalink
Fix data attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-el committed Nov 22, 2023
1 parent 8faa301 commit 6f79b06
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
7 changes: 3 additions & 4 deletions plugins/browser-plugin-button-click-tracking/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ export function filterFunctionFromFilter(filter?: Filter): FilterFunction {
return () => true;
}

const SP_BUTTON_LABEL_ATTRIBUTE = 'sp-button-label';
export function createEventFromButton(
button: HTMLButtonElement | HTMLInputElement
): ButtonClickEvent & CommonEventProperties {
let label = '';
let label = button.dataset.spButtonLabel;
if (button.tagName === 'INPUT') {
label = button.getAttribute(SP_BUTTON_LABEL_ATTRIBUTE) || button.value;
label = button.dataset.spButtonLabel || button.value;
} else {
label = button.getAttribute(SP_BUTTON_LABEL_ATTRIBUTE) || button.innerText;
label = button.dataset.spButtonLabel || button.innerText;
}

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ describe('Button Click Tracking Plugin', () => {
expect(createEventFromButton(button)).toEqual(event);
});

it('should prefer the sp-button-label attribute over the innerText', () => {
it('should prefer the data-sp-button-label attribute over the innerText', () => {
const button = document.createElement('button');
button.innerText = 'testLabel';

button.setAttribute('sp-button-label', 'testLabelFromAttribute');
button.setAttribute('data-sp-button-label', 'testLabelFromAttribute');
expect(createEventFromButton(button).label).toEqual('testLabelFromAttribute');
});
});
Expand All @@ -179,12 +179,12 @@ describe('Button Click Tracking Plugin', () => {
expect(createEventFromButton(input)).toEqual(event);
});

it('should prefer the sp-button-label attribute over the value', () => {
it('should prefer the data-sp-button-label attribute over the value', () => {
const input = document.createElement('input');
input.type = 'button';
input.value = 'testLabel';

input.setAttribute('sp-button-label', 'testLabelFromAttribute');
input.setAttribute('data-sp-button-label', 'testLabelFromAttribute');
expect(createEventFromButton(input).label).toEqual('testLabelFromAttribute');
});
});
Expand Down

0 comments on commit 6f79b06

Please sign in to comment.