Version 3.18.1
Show:

File: yui/js/features.js

  1. var feature_tests = {};
  2. /**
  3. Contains the core of YUI's feature test architecture.
  4. @module features
  5. */
  6. /**
  7. * Feature detection
  8. * @class Features
  9. * @static
  10. */
  11. Y.mix(Y.namespace('Features'), {
  12. /**
  13. * Object hash of all registered feature tests
  14. * @property tests
  15. * @type Object
  16. */
  17. tests: feature_tests,
  18. /**
  19. * Add a test to the system
  20. *
  21. * ```
  22. * Y.Features.add("load", "1", {});
  23. * ```
  24. *
  25. * @method add
  26. * @param {String} cat The category, right now only 'load' is supported
  27. * @param {String} name The number sequence of the test, how it's reported in the URL or config: 1, 2, 3
  28. * @param {Object} o Object containing test properties
  29. * @param {String} o.name The name of the test
  30. * @param {Function} o.test The test function to execute, the only argument to the function is the `Y` instance
  31. * @param {String} o.trigger The module that triggers this test.
  32. */
  33. add: function(cat, name, o) {
  34. feature_tests[cat] = feature_tests[cat] || {};
  35. feature_tests[cat][name] = o;
  36. },
  37. /**
  38. * Execute all tests of a given category and return the serialized results
  39. *
  40. * ```
  41. * caps=1:1;2:1;3:0
  42. * ```
  43. * @method all
  44. * @param {String} cat The category to execute
  45. * @param {Array} args The arguments to pass to the test function
  46. * @return {String} A semi-colon separated string of tests and their success/failure: 1:1;2:1;3:0
  47. */
  48. all: function(cat, args) {
  49. var cat_o = feature_tests[cat],
  50. // results = {};
  51. result = [];
  52. if (cat_o) {
  53. Y.Object.each(cat_o, function(v, k) {
  54. result.push(k + ':' + (Y.Features.test(cat, k, args) ? 1 : 0));
  55. });
  56. }
  57. return (result.length) ? result.join(';') : '';
  58. },
  59. /**
  60. * Run a specific test and return a Boolean response.
  61. *
  62. * ```
  63. * Y.Features.test("load", "1");
  64. * ```
  65. *
  66. * @method test
  67. * @param {String} cat The category of the test to run
  68. * @param {String} name The name of the test to run
  69. * @param {Array} args The arguments to pass to the test function
  70. * @return {Boolean} True or false if the test passed/failed.
  71. */
  72. test: function(cat, name, args) {
  73. args = args || [];
  74. var result, ua, test,
  75. cat_o = feature_tests[cat],
  76. feature = cat_o && cat_o[name];
  77. if (!feature) {
  78. Y.log('Feature test ' + cat + ', ' + name + ' not found');
  79. } else {
  80. result = feature.result;
  81. if (Y.Lang.isUndefined(result)) {
  82. ua = feature.ua;
  83. if (ua) {
  84. result = (Y.UA[ua]);
  85. }
  86. test = feature.test;
  87. if (test && ((!ua) || result)) {
  88. result = test.apply(Y, args);
  89. }
  90. feature.result = result;
  91. }
  92. }
  93. return result;
  94. }
  95. });
  96. // Y.Features.add("load", "1", {});
  97. // Y.Features.test("load", "1");
  98. // caps=1:1;2:0;3:1;