Skip to content

[Fix] crossOrigin is wrong; and crossorigin is only valid on script/img/video #1643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions lib/rules/no-unknown-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ const DEFAULTS = {
};

const UNKNOWN_MESSAGE = 'Unknown property \'{{name}}\' found, use \'{{standardName}}\' instead';
const WRONG_TAG_MESSAGE = 'Invalid property \'{{name}}\' found on tag \'{{tagName}}\', but it is only allowed on: {{allowedTags}}';

const DOM_ATTRIBUTE_NAMES = {
'accept-charset': 'acceptCharset',
class: 'className',
for: 'htmlFor',
'http-equiv': 'httpEquiv'
'http-equiv': 'httpEquiv',
crossOrigin: 'crossorigin'
};

const ATTRIBUTE_TAGS_MAP = {
crossorigin: ['script', 'img', 'video']
};

const SVGDOM_ATTRIBUTE_NAMES = {
Expand Down Expand Up @@ -112,7 +118,7 @@ const DOM_PROPERTY_NAMES = [
// Standard
'acceptCharset', 'accessKey', 'allowFullScreen', 'allowTransparency', 'autoComplete', 'autoFocus', 'autoPlay',
'cellPadding', 'cellSpacing', 'charSet', 'classID', 'className', 'colSpan', 'contentEditable', 'contextMenu',
'crossOrigin', 'dateTime', 'encType', 'formAction', 'formEncType', 'formMethod', 'formNoValidate', 'formTarget',
'dateTime', 'encType', 'formAction', 'formEncType', 'formMethod', 'formNoValidate', 'formTarget',
'frameBorder', 'hrefLang', 'htmlFor', 'httpEquiv', 'inputMode', 'keyParams', 'keyType', 'marginHeight', 'marginWidth',
'maxLength', 'mediaGroup', 'minLength', 'noValidate', 'onAnimationEnd', 'onAnimationIteration', 'onAnimationStart',
'onBlur', 'onChange', 'onClick', 'onContextMenu', 'onCopy', 'onCompositionEnd', 'onCompositionStart',
Expand Down Expand Up @@ -149,6 +155,18 @@ function isTagName(node) {
return false;
}

/**
* Extracts the tag name for the JSXAttribute
* @param {Object} node - JSXAttribute being tested.
* @returns {String} tag name
*/
function getTagName(node) {
if (node && node.parent && node.parent.name && node.parent.name) {
return node.parent.name.name;
}
return null;
}

/**
* Get the standard name of the attribute.
* @param {String} name - Name of the attribute.
Expand Down Expand Up @@ -209,8 +227,26 @@ module.exports = {
JSXAttribute: function(node) {
const ignoreNames = getIgnoreConfig();
const name = sourceCode.getText(node.name);
if (ignoreNames.indexOf(name) >= 0) {
return;
}

const tagName = getTagName(node);
const allowedTags = ATTRIBUTE_TAGS_MAP[name];
if (tagName && allowedTags && /[^A-Z]/.test(tagName.charAt(0)) && allowedTags.indexOf(tagName) === -1) {
context.report({
node: node,
message: WRONG_TAG_MESSAGE,
data: {
name: name,
tagName: tagName,
allowedTags: allowedTags.join(', ')
}
});
}

const standardName = getStandardName(name);
if (!isTagName(node) || !standardName || ignoreNames.indexOf(name) >= 0) {
if (!isTagName(node) || !standardName) {
return;
}
context.report({
Expand Down
12 changes: 11 additions & 1 deletion tests/lib/rules/no-unknown-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ ruleTester.run('no-unknown-property', rule, {
{code: '<atom-panel class="foo"></atom-panel>;'}, {
code: '<div class="bar"></div>;',
options: [{ignore: ['class']}]
}
},
{code: '<script crossorigin />'}
],
invalid: [{
code: '<div class="bar"></div>;',
Expand Down Expand Up @@ -79,5 +80,14 @@ ruleTester.run('no-unknown-property', rule, {
code: '<rect clip-path="bar" />;',
output: '<rect clipPath="bar" />;',
errors: [{message: 'Unknown property \'clip-path\' found, use \'clipPath\' instead'}]
}, {
code: '<script crossOrigin />',
errors: [{message: 'Unknown property \'crossOrigin\' found, use \'crossorigin\' instead'}]
}, {
code: '<div crossOrigin />',
errors: [{message: 'Unknown property \'crossOrigin\' found, use \'crossorigin\' instead'}]
}, {
code: '<div crossorigin />',
errors: [{message: 'Invalid property \'crossorigin\' found on tag \'div\', but it is only allowed on: script, img, video'}]
}]
});