Skip to content

Commit

Permalink
When scoring text for similarity always trim and lower case
Browse files Browse the repository at this point in the history
  • Loading branch information
sunesimonsen committed Feb 27, 2019
1 parent 7297335 commit 681eb67
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1454,13 +1454,14 @@ module.exports = {
);

function scoreElementAgainstSpec(element, spec) {
const isTextMatching = (value, valueSpec) => {
const isTextSimilar = (value, valueSpec) => {
const actual = (value || '').trim().toLowerCase();
if (typeof valueSpec === 'string') {
if (value === valueSpec) {
if (actual === valueSpec.trim().toLowerCase()) {
return true;
}
} else if (valueSpec instanceof RegExp) {
if (valueSpec.test(value)) {
if (valueSpec.test(actual)) {
return true;
}
} else if (typeof valueSpec === 'function') {
Expand All @@ -1469,6 +1470,7 @@ module.exports = {

return false;
};

const isHtml = isInsideHtmlDocument(element);

let score = 0;
Expand All @@ -1477,11 +1479,11 @@ module.exports = {
? element.nodeName.toLowerCase()
: element.nodeName;

if (isTextMatching(nodeName, spec.name)) {
if (isTextSimilar(nodeName, spec.name)) {
score++;
}

if (isTextMatching(element.textContent, spec.textContent)) {
if (isTextSimilar(element.textContent, spec.textContent)) {
score++;
}

Expand All @@ -1502,7 +1504,7 @@ module.exports = {
score++;
}
});
} else if (isTextMatching(element.getAttribute('class'), className)) {
} else if (isTextSimilar(element.getAttribute('class'), className)) {
score++;
}
}
Expand All @@ -1522,7 +1524,7 @@ module.exports = {
score++;
}

if (isTextMatching(actualStyle, expectedStyle)) {
if (isTextSimilar(actualStyle, expectedStyle)) {
score++;
}
});
Expand Down Expand Up @@ -1583,7 +1585,7 @@ module.exports = {
score += scoreElementAgainstSpec(element.childNodes[i], childSpec);
} else if (
childType.is('DOMTextNode') &&
isTextMatching(child.nodeValue, childSpec)
isTextSimilar(child.nodeValue, childSpec)
) {
score++;
}
Expand Down

0 comments on commit 681eb67

Please sign in to comment.