+Hash tied to provide additional keys/values than the original.
[MyWeb.git] / css_inherit.js
1 /* $Id$
2  * JavaScript helper for CSS 'subroutines'.
3  * Copyright (C) 2005 Jan Kratochvil <project-www.jankratochvil.net@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 // Required CSS rule syntax: .PARENTNAME XXX[-lace-inherit="CHILDNAME"] { }
21 // Be aware: CSS is case insensitive but HTML/XHTML is (sometimes?) sensitive.
22 // Be aware: Works only on CSS directly applied by the "class" attribute!
23 // TODO: Warns on invalid inheritance rules.
24
25 var map;
26
27 function map_set(from,to)
28 {
29         if (!map)
30                 map=new Array();
31         if (!map[from])
32                 map[from]=new Array();
33         if (map[from][to])
34                 return true;
35         map[from][to]=true;
36         return false;
37 }
38
39 function css_inherit_node(node)
40 {
41         if (node.className) {
42                 var classone_re=node.className.toLowerCase().match(/(\S+)/g);
43                 for (var classonei in classone_re) {
44                         var classone=classone_re[classonei];
45                         if (!map || !map[classone])
46                                 continue;
47                         for (var target in map[classone])
48                                 node.className+=" "+target;
49                         }
50                 }
51         for (node=node.firstChild;node!=null;node=node.nextSibling)
52                 css_inherit_node(node);
53 }
54
55 function css_inherit()
56 {
57         if (document.styleSheets)
58                 for (var stylei in document.styleSheets) {
59                         var style=document.styleSheets[stylei];
60                         if (!style.cssRules)
61                                 continue;
62                         for (var rulei in style.cssRules) {
63                                 var rule=style.cssRules[rulei];
64                                 if (!rule.selectorText)
65                                         continue;
66                                 var re;
67                                 if (!(re=rule.selectorText.toLowerCase()
68                                                 .match(/^\s*[.](\w+).*\[\s*-lace-inherit\s*=\s*"?([^"]*)"?\s*\]\s*/)))
69                                         continue;
70                                 var from_string=re[1];
71                                 var to_string=re[2];
72                                 var to_re=to_string.match(/\S+/g);
73                                 for (var to_rei in to_re) {
74                                         var to_item=to_re[to_rei];
75                                         map_set(from_string,to_item);
76                                         }
77                                 }
78                         }
79         var change;
80         do {
81                 change=false;
82                 for (var from in map)
83                         for (var to in map[from]) {
84                                 if (!map[to])
85                                         continue;
86                                 for (var filler in map[to])
87                                         if (!map_set(from,filler))
88                                                 change=true;
89                                 }
90                 } while (change);
91         /* Gecko: document.createNodeIterator(...)==NS_ERROR_NOT_IMPLEMENTED */
92         css_inherit_node(document);
93 }
94
95 /* Origin: http://simon.incutio.com/archive/2004/05/26/addLoadEvent */
96 function addLoadEvent(func)
97 {
98         var onload_orig=window.onload;
99         if (typeof(onload_orig)!='function')
100                 window.onload = func;
101         else
102                 window.onload = function()
103                 {
104                         onload_orig();
105                         func();
106                 };
107 }
108
109 addLoadEvent(css_inherit);