Skip to content

Commit a0325f6

Browse files
authored
Support elements that only contain textContent, remove commas from the end of addresses (#1440)
* Support textNodes, remove commas from the end of addresses * Restructure the textContent statement * Make code more testable, add tests
1 parent 0f9d508 commit a0325f6

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

injected/src/features/broker-protection/actions/extract.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,26 @@ export function createProfile(elementFactory, extractData) {
160160
}
161161

162162
/**
163-
* @param {{innerText: string}[]} elements
163+
* @param {({ textContent: string } | { innerText: string })[]} elements
164164
* @param {string} key
165165
* @param {ExtractProfileProperty} extractField
166166
* @return {string[]}
167167
*/
168-
function stringValuesFromElements(elements, key, extractField) {
168+
export function stringValuesFromElements(elements, key, extractField) {
169169
return elements.map((element) => {
170-
// todo: should we use textContent here?
171-
let elementValue = rules[key]?.(element) ?? element?.innerText ?? null;
170+
let elementValue;
171+
172+
if ('innerText' in element) {
173+
elementValue = rules[key]?.(element) ?? element?.innerText ?? null;
174+
175+
// In instances where we use the text() node test, innerText will be undefined, and we fall back to textContent
176+
} else if ('textContent' in element) {
177+
elementValue = rules[key]?.(element) ?? element?.textContent ?? null;
178+
}
179+
180+
if (!elementValue) {
181+
return elementValue;
182+
}
172183

173184
if (extractField?.afterText) {
174185
elementValue = elementValue?.split(extractField.afterText)[1]?.trim() || elementValue;

injected/src/features/broker-protection/extractors/address.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ function getCityStateCombos(inputList) {
5353
// Strip out the zip code since we're only interested in city/state here.
5454
item = item.replace(/,?\s*\d{5}(-\d{4})?/, '');
5555

56+
// Replace any commas at the end of the string that could confuse the city/state split.
57+
item = item.replace(/,$/, '');
58+
5659
if (item.includes(',')) {
5760
words = item.split(',').map((item) => item.trim());
5861
} else {

injected/unit-test/broker-protection-extract.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { aggregateFields, createProfile } from '../src/features/broker-protection/actions/extract.js';
1+
import { aggregateFields, createProfile, stringValuesFromElements } from '../src/features/broker-protection/actions/extract.js';
22
import { cleanArray } from '../src/features/broker-protection/utils.js';
33

44
describe('create profiles from extracted data', () => {
@@ -360,4 +360,19 @@ describe('create profiles from extracted data', () => {
360360

361361
expect(actual.alternativeNames).toEqual(['Fred Firth', 'Jerry Doug', 'Marvin Smith', 'Roger Star']);
362362
});
363+
364+
it('should extract innerText by default', () => {
365+
const element = {
366+
innerText: 'John Smith, 39',
367+
textContent: 'Ignore me',
368+
};
369+
expect(stringValuesFromElements([element], 'testKey', { selector: 'example' })).toEqual(['John Smith, 39']);
370+
});
371+
372+
it('should extract textElement if innerText is not present', () => {
373+
const element = {
374+
textContent: 'John Smith, 39',
375+
};
376+
expect(stringValuesFromElements([element], 'testKey', { selector: 'example' })).toEqual(['John Smith, 39']);
377+
});
363378
});

0 commit comments

Comments
 (0)