wai.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dijit._base.wai"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dijit._base.wai"] = true;
  8. dojo.provide("dijit._base.wai");
  9. dijit.wai = {
  10. onload: function(){
  11. // summary:
  12. // Detects if we are in high-contrast mode or not
  13. // This must be a named function and not an anonymous
  14. // function, so that the widget parsing code can make sure it
  15. // registers its onload function after this function.
  16. // DO NOT USE "this" within this function.
  17. // create div for testing if high contrast mode is on or images are turned off
  18. var div = dojo.create("div",{
  19. id: "a11yTestNode",
  20. style:{
  21. cssText:'border: 1px solid;'
  22. + 'border-color:red green;'
  23. + 'position: absolute;'
  24. + 'height: 5px;'
  25. + 'top: -999px;'
  26. + 'background-image: url("' + (dojo.config.blankGif || dojo.moduleUrl("dojo", "resources/blank.gif")) + '");'
  27. }
  28. }, dojo.body());
  29. // test it
  30. var cs = dojo.getComputedStyle(div);
  31. if(cs){
  32. var bkImg = cs.backgroundImage;
  33. var needsA11y = (cs.borderTopColor == cs.borderRightColor) || (bkImg != null && (bkImg == "none" || bkImg == "url(invalid-url:)" ));
  34. dojo[needsA11y ? "addClass" : "removeClass"](dojo.body(), "dijit_a11y");
  35. if(dojo.isIE){
  36. div.outerHTML = ""; // prevent mixed-content warning, see http://support.microsoft.com/kb/925014
  37. }else{
  38. dojo.body().removeChild(div);
  39. }
  40. }
  41. }
  42. };
  43. // Test if computer is in high contrast mode.
  44. // Make sure the a11y test runs first, before widgets are instantiated.
  45. if(dojo.isIE || dojo.isMoz){ // NOTE: checking in Safari messes things up
  46. dojo._loaders.unshift(dijit.wai.onload);
  47. }
  48. dojo.mixin(dijit, {
  49. hasWaiRole: function(/*Element*/ elem, /*String?*/ role){
  50. // summary:
  51. // Determines if an element has a particular role.
  52. // returns:
  53. // True if elem has the specific role attribute and false if not.
  54. // For backwards compatibility if role parameter not provided,
  55. // returns true if has a role
  56. var waiRole = this.getWaiRole(elem);
  57. return role ? (waiRole.indexOf(role) > -1) : (waiRole.length > 0);
  58. },
  59. getWaiRole: function(/*Element*/ elem){
  60. // summary:
  61. // Gets the role for an element (which should be a wai role).
  62. // returns:
  63. // The role of elem or an empty string if elem
  64. // does not have a role.
  65. return dojo.trim((dojo.attr(elem, "role") || "").replace("wairole:",""));
  66. },
  67. setWaiRole: function(/*Element*/ elem, /*String*/ role){
  68. // summary:
  69. // Sets the role on an element.
  70. // description:
  71. // Replace existing role attribute with new role.
  72. dojo.attr(elem, "role", role);
  73. },
  74. removeWaiRole: function(/*Element*/ elem, /*String*/ role){
  75. // summary:
  76. // Removes the specified role from an element.
  77. // Removes role attribute if no specific role provided (for backwards compat.)
  78. var roleValue = dojo.attr(elem, "role");
  79. if(!roleValue){ return; }
  80. if(role){
  81. var t = dojo.trim((" " + roleValue + " ").replace(" " + role + " ", " "));
  82. dojo.attr(elem, "role", t);
  83. }else{
  84. elem.removeAttribute("role");
  85. }
  86. },
  87. hasWaiState: function(/*Element*/ elem, /*String*/ state){
  88. // summary:
  89. // Determines if an element has a given state.
  90. // description:
  91. // Checks for an attribute called "aria-"+state.
  92. // returns:
  93. // true if elem has a value for the given state and
  94. // false if it does not.
  95. return elem.hasAttribute ? elem.hasAttribute("aria-"+state) : !!elem.getAttribute("aria-"+state);
  96. },
  97. getWaiState: function(/*Element*/ elem, /*String*/ state){
  98. // summary:
  99. // Gets the value of a state on an element.
  100. // description:
  101. // Checks for an attribute called "aria-"+state.
  102. // returns:
  103. // The value of the requested state on elem
  104. // or an empty string if elem has no value for state.
  105. return elem.getAttribute("aria-"+state) || "";
  106. },
  107. setWaiState: function(/*Element*/ elem, /*String*/ state, /*String*/ value){
  108. // summary:
  109. // Sets a state on an element.
  110. // description:
  111. // Sets an attribute called "aria-"+state.
  112. elem.setAttribute("aria-"+state, value);
  113. },
  114. removeWaiState: function(/*Element*/ elem, /*String*/ state){
  115. // summary:
  116. // Removes a state from an element.
  117. // description:
  118. // Sets an attribute called "aria-"+state.
  119. elem.removeAttribute("aria-"+state);
  120. }
  121. });
  122. }