]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/has.js.uncompressed.js
1 define("dojo/has", ["require", "module"], function(require
, module
){
5 // Defines the has.js API and several feature tests used by dojo.
7 // This module defines the has API as described by the project has.js with the following additional features:
9 // - the has test cache is exposed at has.cache.
10 // - the method has.add includes a forth parameter that controls whether or not existing tests are replaced
11 // - the loader's has cache may be optionally copied into this module's has cahce.
13 // This module adopted from https://github.com/phiggins42/has.js; thanks has.js team!
15 // try to pull the has implementation from the loader; both the dojo loader and bdLoad provide one
16 // if using a foreign loader, then the has cache may be initialized via the config object for this module
17 // WARNING: if a foreign loader defines require.has to be something other than the has.js API, then this implementation fail
18 var has
= require
.has
|| function(){};
22 // the most fundamental decision: are we in the browser?
23 typeof window
!= "undefined" &&
24 typeof location
!= "undefined" &&
25 typeof document
!= "undefined" &&
26 window
.location
== location
&& window
.document
== document
,
30 doc
= isBrowser
&& document
,
31 element
= doc
&& doc
.createElement("DiV"),
32 cache
= (module
.config
&& module
.config()) || {};
36 // Return the current value of the named feature.
38 // name: String|Integer
39 // The name (if a string) or identifier (if an integer) of the feature to test.
42 // Returns the value of the feature named by name. The feature must have been
43 // previously added to the cache by has.add.
45 return typeof cache
[name
] == "function" ? (cache
[name
] = cache
[name
](global
, doc
, element
)) : cache
[name
]; // Boolean
50 has
.add = function(name
, test
, now
, force
){
52 // Register a new feature test for some named feature.
53 // name: String|Integer
54 // The name (if a string) or identifier (if an integer) of the feature to test.
56 // A test function to register. If a function, queued for testing until actually
57 // needed. The test function should return a boolean indicating
58 // the presence of a feature or bug.
60 // Optional. Omit if `test` is not a function. Provides a way to immediately
61 // run the test and cache the result.
63 // Optional. If the test already exists and force is truthy, then the existing
64 // test will be replaced; otherwise, add does not replace an existing test (that
65 // is, by default, the first test advice wins).
67 // A redundant test, testFn with immediate execution:
68 // | has.add("javascript", function(){ return true; }, true);
71 // Again with the redundantness. You can do this in your tests, but we should
72 // not be doing this in any internal has.js tests
73 // | has.add("javascript", true);
76 // Three things are passed to the testFunction. `global`, `document`, and a generic element
77 // from which to work your test should the need arise.
78 // | has.add("bug-byid", function(g, d, el){
79 // | // g == global, typically window, yadda yadda
80 // | // d == document object
81 // | // el == the generic element. a `has` element.
82 // | return false; // fake test, byid-when-form-has-name-matching-an-id is slightly longer
85 (typeof cache
[name
]=="undefined" || force
) && (cache
[name
]= test
);
86 return now
&& has(name
);
89 // since we're operating under a loader that doesn't provide a has API, we must explicitly initialize
90 // has as it would have otherwise been initialized by the dojo loader; use has.add to the builder
91 // can optimize these away iff desired
92 1 || has
.add("host-browser", isBrowser
);
93 1 || has
.add("dom", isBrowser
);
94 1 || has
.add("dojo-dom-ready-api", 1);
95 1 || has
.add("dojo-sniff", 1);
99 // Common application level tests
100 has
.add("dom-addeventlistener", !!document
.addEventListener
);
101 has
.add("touch", "ontouchstart" in document
);
102 // I don't know if any of these tests are really correct, just a rough guess
103 has
.add("device-width", screen
.availWidth
|| innerWidth
);
105 // Tests for DOMNode.attributes[] behavior:
106 // - dom-attributes-explicit - attributes[] only lists explicitly user specified attributes
107 // - dom-attributes-specified-flag (IE8) - need to check attr.specified flag to skip attributes user didn't specify
108 // - Otherwise, in IE6-7. attributes[] will list hundreds of values, so need to do outerHTML to get attrs instead.
109 var form
= document
.createElement("form");
110 has
.add("dom-attributes-explicit", form
.attributes
.length
== 0); // W3C
111 has
.add("dom-attributes-specified-flag", form
.attributes
.length
> 0 && form
.attributes
.length
< 40); // IE8
114 has
.clearElement = function(element
){
116 // Deletes the contents of the element passed to test functions.
117 element
.innerHTML
= "";
121 has
.normalize = function(id
, toAbsMid
){
123 // Resolves id into a module id based on possibly-nested tenary expression that branches on has feature test value(s).
125 // toAbsMid: Function
126 // Resolves a relative module id into an absolute module id
128 tokens
= id
.match(/[\?:]|[^:\?]*/g), i
= 0,
129 get = function(skip
){
130 var term
= tokens
[i
++];
132 // empty string module name, resolves to 0
135 // postfixed with a ? means it is a feature to branch on, the term is the name of the feature
136 if(tokens
[i
++] == "?"){
137 if(!skip
&& has(term
)){
138 // matched the feature, get the first value from the options
141 // did not match, get the second value, passing over the first
151 return id
&& toAbsMid(id
);
154 has
.load = function(id
, parentRequire
, loaded
){
156 // Conditional loading of AMD modules based on a has feature test value.
158 // Gives the resolved module id to load.
159 // parentRequire: Function
160 // The loader require function with respect to the module that contained the plugin resource in it's
163 // Callback to loader that consumes result of plugin demand.
166 parentRequire([id
], loaded
);