Skip to content

Commit

Permalink
[#1] Ensuring title and meta description are different
Browse files Browse the repository at this point in the history
  • Loading branch information
YAMINIPRIYA16 committed Apr 18, 2018
1 parent 1906bdc commit d132419
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
15 changes: 14 additions & 1 deletion app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ function validateHeader(headers, {header, errors, warnings}, url, outputLists) {
})
}

function getContent($, element, contentAttr) {
return (contentAttr == 'body' ? $(element).html() : $(element).attr(contentAttr)) || '';
}

function validateDom($, {selector, contentAttr, errors, warnings}, url, outputLists) {
const elements = $(selector);

Expand All @@ -94,7 +98,7 @@ function validateDom($, {selector, contentAttr, errors, warnings}, url, outputLi
return outputList.push(`Expected to find ${rules.count} elements with selector ${selector}, got ${elements.length}`);

elements.each((i, element) => {
const content = (contentAttr == 'body' ? $(element).html() : $(element).attr(contentAttr)) || '';
const content = getContent($, element, contentAttr);

// Reuse this?
if((rules.presence || rules.presence_if_node_exists) && (!content || content == ''))
Expand All @@ -106,6 +110,15 @@ function validateDom($, {selector, contentAttr, errors, warnings}, url, outputLi
if(rules.value == 'url' && content != url) {
return outputList.push(`Content in ${selector} should have value ${url} (got ${content})`);
}

if(rules.different_from) {
const otherElements = $(rules.different_from.selector);
otherElements.each((i, otherElement) => {
const otherContent = getContent($, otherElement, rules.different_from.contentAttr);
if(content == otherContent)
return outputList.push(`Content in ${selector} should not have the same value as ${rules.different_from.selector}`);
});
}
});
});
}
Expand Down
5 changes: 4 additions & 1 deletion config/rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ seo:
warnings:
length_le: 66

# Meta Description should be 160 chars or less
# Meta Description should be 160 chars or less, and be different from title
- type: dom
selector: meta[name=description]
contentAttr: content
errors:
presence: true
count: 1
different_from:
selector: head title
contentAttr: body
warnings:
length_le: 160

Expand Down

0 comments on commit d132419

Please sign in to comment.