UNPKG

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