blob: b022b98fd162373012e11226bffb673e256f41c8 [file] [log] [blame]
Felix Meschberger0704b052008-06-25 08:44:16 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19/* Element */ function clearChildren( /* Element */ element )
20{
21 while (element.firstChild)
22 {
23 element.removeChild(element.firstChild);
24 }
25
26 return element;
27}
28
29/* String */ function serialize( /* Element */ element )
30{
31 var result = "";
32
33 if (element)
34 {
35 if (element.nodeValue)
36 {
37 result = element.nodeValue;
38 }
39 else {
40 result += "<" + element.tagName;
41
42 var attrs = element.attributes;
43 for (var i=0; i < attrs.length; i++)
44 {
45 if (attrs[i].nodeValue)
46 {
47 result += " " + attrs[i].nodeName + "='" + attrs[i].nodeValue + "'";
48 }
49 }
50
51 var children = element.childNodes;
52 if (children && children.length)
53 {
54 result += ">";
55
56 for (var i=0; i < children.length; i++)
57 {
58 result += serialize( children[i] );
59 }
60 result += "</" + element.tagName + ">";
61 }
62 else
63 {
64 result += "/>";
65 }
66 }
67 }
68
69 return result;
70}
71
Felix Meschbergerc07350a2008-06-25 20:26:13 +000072/* Element */ function tr( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children )
Felix Meschberger0704b052008-06-25 08:44:16 +000073{
Felix Meschbergerc07350a2008-06-25 20:26:13 +000074 return createElement( "tr", cssClass, attrs, children );
Felix Meschberger0704b052008-06-25 08:44:16 +000075}
76
77
Felix Meschbergerc07350a2008-06-25 20:26:13 +000078/* Element */ function td( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children )
Felix Meschberger0704b052008-06-25 08:44:16 +000079{
Felix Meschbergerc07350a2008-06-25 20:26:13 +000080 return createElement( "td", cssClass, attrs, children );
Felix Meschberger0704b052008-06-25 08:44:16 +000081}
82
83
Felix Meschbergerc07350a2008-06-25 20:26:13 +000084/* Element */ function text( /* String */ textValue )
85{
86 return document.createTextNode( textValue );
87}
88
89
90/* Element */ function createElement( /* String */ name, /* String */ cssClass, /* Map */ attrs, /* Element[] */ children )
Felix Meschberger0704b052008-06-25 08:44:16 +000091{
92 var element = document.createElement( name );
93
94 if (cssClass)
95 {
Felix Meschbergerfb550db2008-06-25 23:09:26 +000096 element.setAttribute( "class", cssClass ); // non-IE
97 element.setAttribute( "className", cssClass ); // IE
Felix Meschberger0704b052008-06-25 08:44:16 +000098 }
99
100 if (attrs)
101 {
102 for (var lab in attrs)
103 {
Felix Meschbergerfb550db2008-06-25 23:09:26 +0000104 if ("style" == lab)
105 {
106 var styles = attrs[lab];
107 for (var styleName in styles)
108 {
109 element.style[styleName] = styles[styleName];
110 }
111 }
112 else
113 {
114 element.setAttribute( lab, attrs[lab] );
115 }
Felix Meschberger0704b052008-06-25 08:44:16 +0000116 }
117 }
118
Felix Meschbergerc07350a2008-06-25 20:26:13 +0000119 if (children && children.length)
120 {
121 for (var i=0; i < children.length; i++)
122 {
123 element.appendChild( children[i] );
124 }
125 }
126
Felix Meschberger0704b052008-06-25 08:44:16 +0000127 return element;
128}
129
130
Felix Meschbergerc07350a2008-06-25 20:26:13 +0000131/* Element */ function addText( /* Element */ element, /* String */ textValue )
Felix Meschberger0704b052008-06-25 08:44:16 +0000132{
Felix Meschbergerc07350a2008-06-25 20:26:13 +0000133 if (element && textValue)
Felix Meschberger0704b052008-06-25 08:44:16 +0000134 {
Felix Meschbergerc07350a2008-06-25 20:26:13 +0000135 element.appendChild( text( textValue ) );
Felix Meschberger0704b052008-06-25 08:44:16 +0000136 }
137
138 return element;
139}