weekly release 2.1.6+
[moodle.git] / lib / csshover.htc
blobf00536d491f4c42a8cd4206b1f772c3732644137
1 <attach event="ondocumentready" handler="parseStylesheets" />
2 <script type="text/javascript">
3 /**
4  *      Pseudos - V1.30.050121 - hover & active
5  *      ---------------------------------------------
6  *      Peterned - http://www.xs4all.nl/~peterned/
7  *      (c) 2005 - Peter Nederlof
8  *
9  *      Credits  - Arnoud Berendsen 
10  *               - Martin Reurings
11  *           - Robert Hanson
12  *
13  *      howto: body { behavior:url("csshover.htc"); }
14  *      ---------------------------------------------
15  */
17 var currentSheet, doc = window.document, activators = {
18         onhover:{on:'onmouseover', off:'onmouseout'},
19         onactive:{on:'onmousedown', off:'onmouseup'}
22 function parseStylesheets() {
23         var sheets = doc.styleSheets, l = sheets.length;
24         for(var i=0; i<l; i++) 
25                 parseStylesheet(sheets[i]);
27         function parseStylesheet(sheet) {
28                 if(sheet.imports) {
29                         try {
30                                 var imports = sheet.imports, l = imports.length;
31                                 for(var i=0; i<l; i++) parseStylesheet(sheet.imports[i]);
32                         } catch(securityException){}
33                 }
35                 try {
36                         var rules = (currentSheet = sheet).rules, l = rules.length;
37                         for(var j=0; j<l; j++) parseCSSRule(rules[j]);
38                 } catch(securityException){}
39         }
41         function parseCSSRule(rule) {
42                 var select = rule.selectorText, style = rule.style.cssText;
43                 if(!(/(^|\s)(([^a]([^ ]+)?)|(a([^#.][^ ]+)+)):(hover|active)/i).test(select) || !style) return;
44                 
45                 var pseudo = select.replace(/[^:]+:([a-z-]+).*/i, 'on$1');
46                 var newSelect = select.replace(/(\.([a-z0-9_-]+):[a-z]+)|(:[a-z]+)/gi, '.$2' + pseudo);
47                 var className = (/\.([a-z0-9_-]*on(hover|active))/i).exec(newSelect)[1];
48                 var affected = select.replace(/:hover.*$/, '');
49                 var elements = getElementsBySelect(affected);
51                 currentSheet.addRule(newSelect, style);
52                 for(var i=0; i<elements.length; i++)
53                         new HoverElement(elements[i], className, activators[pseudo]);
54         }
56 function HoverElement(node, className, events) {
57         if(!node.hovers) node.hovers = {};
58         if(node.hovers[className]) return;
59         node.hovers[className] = true;
60         node.attachEvent(events.on,
61                 function() { node.className += ' ' + className; });
62         node.attachEvent(events.off,
63                 function() { node.className = 
64                         node.className.replace(new RegExp('\\s+'+className, 'g'),''); });
67 function getElementsBySelect(rule) {
68         var parts, nodes = [doc];
69         parts = rule.split(' ');
70         for(var i=0; i<parts.length; i++) {
71                 nodes = getSelectedNodes(parts[i], nodes);
72         }       return nodes;
74         function getSelectedNodes(select, elements) {
75                 var result, node, nodes = [];
76                 var classname = (/\.([a-z0-9_-]+)/i).exec(select);
77                 var identify = (/\#([a-z0-9_-]+)/i).exec(select);
78                 var tagName = select.replace(/(\.|\#|\:)[a-z0-9_-]+/i, '');
79                 for(var i=0; i<elements.length; i++) {
80                         result = tagName? elements[i].all.tags(tagName):elements[i].all; 
81                         for(var j=0; j<result.length; j++) {
82                                 node = result[j];
83                                 if((identify && node.id != identify[1]) || (classname && !(new RegExp('\\b' +
84                                         classname[1] + '\\b').exec(node.className)))) continue;
85                                 nodes[nodes.length] = node;
86                         }
87                 }       return nodes;
88         }
89 </script>