|
1 | 1 | import autocompleteMatches from './autocomplete-matches';
|
2 | 2 |
|
| 3 | +function nodeIsASearchFunctionality(actualNode, currLevel = 0, maxLevels = 4) { |
| 4 | + if (!actualNode) { |
| 5 | + return false; |
| 6 | + } |
| 7 | + |
| 8 | + function currentLevelSearch(node, currentLevel) { |
| 9 | + if (!node || currentLevel > maxLevels) { |
| 10 | + return false; |
| 11 | + } |
| 12 | + |
| 13 | + let details = `\nLevel ${currentLevel}:\n`; |
| 14 | + |
| 15 | + //collecting all the HTML attributes |
| 16 | + details += 'Attributes:\n'; |
| 17 | + if (node.hasAttributes()) { |
| 18 | + const attributes = axe.utils.getNodeAttributes(node); |
| 19 | + for (let i = 0; i < attributes.length; i++) { |
| 20 | + const attr = attributes[i]; |
| 21 | + details += ` ${attr.name}: ${attr.value}\n`; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // Collect any associated labels (if node is an input, select, textarea, etc.) |
| 26 | + if (node.labels) { |
| 27 | + details += 'Labels:\n'; |
| 28 | + for (let j = 0; j < node.labels.length; j++) { |
| 29 | + details += ` ${node.labels[j].innerText}\n`; |
| 30 | + } |
| 31 | + } else if ( |
| 32 | + node.nodeName.toLowerCase() === 'input' && |
| 33 | + node.type !== 'hidden' |
| 34 | + ) { |
| 35 | + const labels = document.querySelectorAll('label[for="' + node.id + '"]'); |
| 36 | + details += 'Labels:\n'; |
| 37 | + labels.forEach(label => { |
| 38 | + details += ` ${label.innerText}\n`; |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + // Collect the given id |
| 43 | + details += `ID: ${node.id}\n`; |
| 44 | + // Collect all class names |
| 45 | + details += `Class Names: ${node.className |
| 46 | + .split(' ') |
| 47 | + .filter(name => name) |
| 48 | + .join(', ')}\n`; |
| 49 | + |
| 50 | + const regex = new RegExp('search', 'i'); |
| 51 | + if (regex.test(details)) { |
| 52 | + return true; |
| 53 | + } else { |
| 54 | + return currentLevelSearch(node.parentElement, currentLevel + 1); |
| 55 | + } |
| 56 | + } |
| 57 | + return currentLevelSearch(actualNode, currLevel); |
| 58 | +} |
| 59 | + |
3 | 60 | function autocompleteA11yMatches(node, virtualNode) {
|
4 | 61 | const a11yEngineFlag = true;
|
5 |
| - // the flag is used to tell autocomplete matcher that it is being called |
6 |
| - // by a11y-engine and thus bypass an if block |
7 |
| - return autocompleteMatches(node, virtualNode, a11yEngineFlag); |
| 62 | + /* the flag is used to tell autocomplete matcher that it is being called |
| 63 | + by a11y-engine and thus bypass an if block |
| 64 | + The second condition is to check we are not matching with search functionality */ |
| 65 | + return ( |
| 66 | + autocompleteMatches(node, virtualNode, a11yEngineFlag) && |
| 67 | + !nodeIsASearchFunctionality(node) |
| 68 | + ); |
8 | 69 | }
|
9 | 70 |
|
10 | 71 | export default autocompleteA11yMatches;
|
0 commit comments