Skip to content

Commit

Permalink
Merge pull request #1566 from sukima/meta-itemprop-fix
Browse files Browse the repository at this point in the history
Allow `itemprop` use in meta tags in `no-invalid-meta` rule
  • Loading branch information
bmish committed Oct 11, 2020
2 parents c39c292 + dff757a commit 8286a37
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
11 changes: 6 additions & 5 deletions lib/rules/no-invalid-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@ module.exports = class NoInvalidMeta extends Rule {
const hasContent = AstNodeInfo.hasAttribute(node, 'content');
const hasName = AstNodeInfo.hasAttribute(node, 'name');
const hasProperty = AstNodeInfo.hasAttribute(node, 'property');
const hasNameOrProperty = hasName || hasProperty;
const hasItemprop = AstNodeInfo.hasAttribute(node, 'itemprop');
const hasIdentifier = hasName || hasProperty || hasItemprop;

const contentAttrValue = AstNodeInfo.elementAttributeValue(node, 'content');

if ((hasNameOrProperty || hasHttpEquiv) && !hasContent) {
if ((hasIdentifier || hasHttpEquiv) && !hasContent) {
this.log({
message:
'a meta content attribute must be defined if the name, property or the http-equiv attribute is defined',
'a meta content attribute must be defined if the name, property, itemprop or the http-equiv attribute is defined',
line: node.loc && node.loc.start.line,
column: node.loc && node.loc.start.column,
source: this.sourceForNode(node),
});
} else if (hasContent && !hasNameOrProperty && !hasHttpEquiv) {
} else if (hasContent && !hasIdentifier && !hasHttpEquiv) {
this.log({
message:
'a meta content attribute cannot be defined if the name, property nor the http-equiv attributes are defined',
'a meta content attribute cannot be defined if the name, property, itemprop nor the http-equiv attributes are defined',
line: node.loc && node.loc.start.line,
column: node.loc && node.loc.start.column,
source: this.sourceForNode(node),
Expand Down
20 changes: 16 additions & 4 deletions test/unit/rules/no-invalid-meta-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ generateRuleTests({
'<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable= yes">',
'<meta name={{name}} content={{content}}>',
'<meta property="og:type" content="website">',
'<meta itemprop="type" content="website">',

// doesn't error on unrelated elements
'<div></div>',
Expand Down Expand Up @@ -92,7 +93,7 @@ generateRuleTests({

result: {
message:
'a meta content attribute must be defined if the name, property or the http-equiv attribute is defined',
'a meta content attribute must be defined if the name, property, itemprop or the http-equiv attribute is defined',
line: 1,
column: 0,
source: '<meta name="viewport">',
Expand All @@ -103,18 +104,29 @@ generateRuleTests({

result: {
message:
'a meta content attribute must be defined if the name, property or the http-equiv attribute is defined',
'a meta content attribute must be defined if the name, property, itemprop or the http-equiv attribute is defined',
line: 1,
column: 0,
source: '<meta property="og:type">',
},
},
{
template: '<meta itemprop="type">',

result: {
message:
'a meta content attribute must be defined if the name, property, itemprop or the http-equiv attribute is defined',
line: 1,
column: 0,
source: '<meta itemprop="type">',
},
},
{
template: '<meta http-equiv="refresh">',

result: {
message:
'a meta content attribute must be defined if the name, property or the http-equiv attribute is defined',
'a meta content attribute must be defined if the name, property, itemprop or the http-equiv attribute is defined',
line: 1,
column: 0,
source: '<meta http-equiv="refresh">',
Expand All @@ -125,7 +137,7 @@ generateRuleTests({

result: {
message:
'a meta content attribute cannot be defined if the name, property nor the http-equiv attributes are defined',
'a meta content attribute cannot be defined if the name, property, itemprop nor the http-equiv attributes are defined',
line: 1,
column: 0,
source: '<meta content="72001">',
Expand Down

0 comments on commit 8286a37

Please sign in to comment.