UNPKG

3.02 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 */
8
9'use strict';
10
11var DOMProperty = require('./DOMProperty');
12var ReactComponentTreeHook = require('react/lib/ReactComponentTreeHook');
13
14var warning = require('fbjs/lib/warning');
15
16var warnedProperties = {};
17var rARIA = new RegExp('^(aria)-[' + DOMProperty.ATTRIBUTE_NAME_CHAR + ']*$');
18
19function validateProperty(tagName, name, debugID) {
20 if (warnedProperties.hasOwnProperty(name) && warnedProperties[name]) {
21 return true;
22 }
23
24 if (rARIA.test(name)) {
25 var lowerCasedName = name.toLowerCase();
26 var standardName = DOMProperty.getPossibleStandardName.hasOwnProperty(lowerCasedName) ? DOMProperty.getPossibleStandardName[lowerCasedName] : null;
27
28 // If this is an aria-* attribute, but is not listed in the known DOM
29 // DOM properties, then it is an invalid aria-* attribute.
30 if (standardName == null) {
31 warnedProperties[name] = true;
32 return false;
33 }
34 // aria-* attributes should be lowercase; suggest the lowercase version.
35 if (name !== standardName) {
36 process.env.NODE_ENV !== 'production' ? warning(false, 'Unknown ARIA attribute %s. Did you mean %s?%s', name, standardName, ReactComponentTreeHook.getStackAddendumByID(debugID)) : void 0;
37 warnedProperties[name] = true;
38 return true;
39 }
40 }
41
42 return true;
43}
44
45function warnInvalidARIAProps(debugID, element) {
46 var invalidProps = [];
47
48 for (var key in element.props) {
49 var isValid = validateProperty(element.type, key, debugID);
50 if (!isValid) {
51 invalidProps.push(key);
52 }
53 }
54
55 var unknownPropString = invalidProps.map(function (prop) {
56 return '`' + prop + '`';
57 }).join(', ');
58
59 if (invalidProps.length === 1) {
60 process.env.NODE_ENV !== 'production' ? warning(false, 'Invalid aria prop %s on <%s> tag. ' + 'For details, see https://fb.me/invalid-aria-prop%s', unknownPropString, element.type, ReactComponentTreeHook.getStackAddendumByID(debugID)) : void 0;
61 } else if (invalidProps.length > 1) {
62 process.env.NODE_ENV !== 'production' ? warning(false, 'Invalid aria props %s on <%s> tag. ' + 'For details, see https://fb.me/invalid-aria-prop%s', unknownPropString, element.type, ReactComponentTreeHook.getStackAddendumByID(debugID)) : void 0;
63 }
64}
65
66function handleElement(debugID, element) {
67 if (element == null || typeof element.type !== 'string') {
68 return;
69 }
70 if (element.type.indexOf('-') >= 0 || element.props.is) {
71 return;
72 }
73
74 warnInvalidARIAProps(debugID, element);
75}
76
77var ReactDOMInvalidARIAHook = {
78 onBeforeMountComponent: function (debugID, element) {
79 if (process.env.NODE_ENV !== 'production') {
80 handleElement(debugID, element);
81 }
82 },
83 onBeforeUpdateComponent: function (debugID, element) {
84 if (process.env.NODE_ENV !== 'production') {
85 handleElement(debugID, element);
86 }
87 }
88};
89
90module.exports = ReactDOMInvalidARIAHook;
\No newline at end of file