2 * JavaScript helper for CSS 'subroutines'.
3 * Copyright (C) 2005 Jan Kratochvil <project-www.jankratochvil.net@jankratochvil.net>
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
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.
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
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.
27 function map_set(from,to)
32 map[from]=new Array();
39 function css_inherit_node(node)
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])
47 for (var target in map[classone])
48 node.className+=" "+target;
51 for (node=node.firstChild;node!=null;node=node.nextSibling)
52 css_inherit_node(node);
55 function css_inherit()
57 if (document.styleSheets)
58 for (var stylei in document.styleSheets) {
59 var style=document.styleSheets[stylei];
62 for (var rulei in style.cssRules) {
63 var rule=style.cssRules[rulei];
64 if (!rule.selectorText)
67 if (!(re=rule.selectorText.toLowerCase()
68 .match(/^\s*[.](\w+).*\[\s*-lace-inherit\s*=\s*"?([^"]*)"?\s*\]\s*/)))
70 var from_string=re[1];
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);
83 for (var to in map[from]) {
86 for (var filler in map[to])
87 if (!map_set(from,filler))
91 /* Gecko: document.createNodeIterator(...)==NS_ERROR_NOT_IMPLEMENTED */
92 css_inherit_node(document);
95 /* Origin: http://simon.incutio.com/archive/2004/05/26/addLoadEvent */
96 function addLoadEvent(func)
98 var onload_orig=window.onload;
99 if (typeof(onload_orig)!='function')
100 window.onload = func;
102 window.onload = function()
109 addLoadEvent(css_inherit);