New tests.
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlHead.cs
1 //
2 // System.Web.UI.HtmlControls.HtmlHead
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // Copyright (C) 2004-2010 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.ComponentModel;
30 using System.Collections;
31 using System.Security.Permissions;
32 using System.Web.UI.WebControls;
33
34 namespace System.Web.UI.HtmlControls
35 {
36         // CAS - no InheritanceDemand here as the class is sealed
37         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
38         // attributes
39         [ControlBuilder (typeof(HtmlHeadBuilder))]
40         public sealed class HtmlHead: HtmlGenericControl, IParserAccessor
41         {
42 #if NET_4_0
43                 string descriptionText;
44                 string keywordsText;
45                 HtmlMeta descriptionMeta;
46                 HtmlMeta keywordsMeta;
47 #endif
48                 string titleText;
49                 HtmlTitle title;
50                 //Hashtable metadata;
51                 StyleSheetBag styleSheet;
52                 
53                 public HtmlHead(): base("head") {}
54
55                 public HtmlHead (string tag) : base (tag)
56                 {
57                 }
58                 
59                 protected internal override void OnInit (EventArgs e)
60                 {
61                         base.OnInit (e);
62                         Page page = Page;
63                         
64                         if (page == null)
65                                 throw new HttpException ("The <head runat=\"server\"> control requires a page.");
66                         
67                         //You can only have one <head runat="server"> control on a page.
68                         if(page.Header != null)
69                                 throw new HttpException ("You can only have one <head runat=\"server\"> control on a page.");
70                         page.SetHeader (this);
71                 }
72                 
73                 protected internal override void RenderChildren (HtmlTextWriter writer)
74                 {
75                         EnsureTitleControl ();
76
77                         base.RenderChildren (writer);
78 //                      if (metadata != null) {
79 //                              foreach (DictionaryEntry entry in metadata) {
80 //                                      writer.AddAttribute ("name", entry.Key.ToString ());
81 //                                      writer.AddAttribute ("content", entry.Value.ToString ());
82 //                                      writer.RenderBeginTag (HtmlTextWriterTag.Meta);
83 //                                      writer.RenderEndTag ();
84 //                              }
85 //                      }
86                         
87                         if (styleSheet != null)
88                                 styleSheet.Render (writer);
89                 }
90                 
91                 protected internal override void AddedControl (Control control, int index)
92                 {
93                         //You can only have one <title> element within the <head> element.
94                         HtmlTitle t = control as HtmlTitle;
95                         if (t != null) {
96                                 if (title != null)
97                                         throw new HttpException ("You can only have one <title> element within the <head> element.");
98                                 title = t;
99                         }
100
101 #if NET_4_0
102                         HtmlMeta meta = control as HtmlMeta;
103                         if (meta != null) {
104                                 if (String.Compare ("keywords", meta.Name, StringComparison.OrdinalIgnoreCase) == 0)
105                                         keywordsMeta = meta;
106                                 else if (String.Compare ("description", meta.Name, StringComparison.OrdinalIgnoreCase) == 0)
107                                         descriptionMeta = meta;
108                         }
109 #endif
110                         base.AddedControl (control, index);
111                 }
112
113                 protected internal override void RemovedControl (Control control)
114                 {
115                         if (title == control)
116                                 title = null;
117
118 #if NET_4_0
119                         if (keywordsMeta == control)
120                                 keywordsMeta = null;
121                         else if (descriptionMeta == control)
122                                 descriptionMeta = null;
123 #endif
124                         base.RemovedControl (control);
125                 }
126                 
127                 void EnsureTitleControl () {
128                         if (title != null)
129                                 return;
130
131                         HtmlTitle t = new HtmlTitle ();
132                         t.Text = titleText;
133                         Controls.Add (t);
134                 }
135
136 //              IList LinkedStyleSheets {
137 //                      get {
138 //                              if (styleSheets == null) styleSheets = new ArrayList ();
139 //                              return styleSheets;
140 //                      }
141 //              } 
142 //              
143 //              IDictionary Metadata {
144 //                      get {
145 //                              if (metadata == null) metadata = new Hashtable ();
146 //                              return metadata;
147 //                      }
148 //              }
149                 
150 #if NET_4_0
151                 public string Description {
152                         get {
153                                 if (descriptionMeta != null)
154                                         return descriptionMeta.Content;
155                                 return descriptionText;
156                         }
157                         
158                         set {
159                                 if (descriptionMeta != null)
160                                         descriptionMeta.Content = value;
161                                 else
162                                         descriptionText = value;
163                         }
164                 }
165
166                 public string Keywords {
167                         get {
168                                 if (keywordsMeta != null)
169                                         return keywordsMeta.Content;
170                                 return keywordsText;
171                         }
172                         
173                         set {
174                                 if (keywordsMeta != null)
175                                         keywordsMeta.Content = value;
176                                 else
177                                         keywordsText = value;
178                         }
179                 }
180 #endif
181                 
182                 public IStyleSheet StyleSheet {
183                         get {
184                                 if (styleSheet == null) styleSheet = new StyleSheetBag ();
185                                 return styleSheet;
186                         }
187                 }
188                 
189                 public string Title {
190                         get {
191                                 if (title != null)
192                                         return title.Text;
193                                 else
194                                         return titleText;
195                         }
196                         set {
197                                 if (title != null)
198                                         title.Text = value;
199                                 else
200                                         titleText = value;
201                         }
202                 }
203         }
204         
205         internal class StyleSheetBag: IStyleSheet
206         {
207                 ArrayList entries = new ArrayList ();
208                 
209                 internal class StyleEntry
210                 {
211                         public Style Style;
212                         public string Selection;
213                         public IUrlResolutionService UrlResolver;
214                 }
215                 
216                 public StyleSheetBag ()
217                 {
218                 }
219                 
220                 public void CreateStyleRule (Style style, IUrlResolutionService urlResolver, string selection)
221                 {
222                         StyleEntry entry = new StyleEntry ();
223                         entry.Style = style;
224                         entry.UrlResolver = urlResolver;
225                         entry.Selection = selection;
226                         entries.Add (entry);
227                 }
228                 
229                 public void RegisterStyle (Style style, IUrlResolutionService urlResolver)
230                 {
231                         for (int n=0; n<entries.Count; n++) {
232                                 if (((StyleEntry)entries[n]).Style == style)
233                                         return;
234                         }
235                         
236                         string name = "aspnet_" + entries.Count;
237                         style.SetRegisteredCssClass (name);
238                         CreateStyleRule (style, urlResolver, "." + name);
239                 }
240                 
241                 public void Render (HtmlTextWriter writer)
242                 {
243                         writer.AddAttribute ("type", "text/css", false);
244                         writer.RenderBeginTag (HtmlTextWriterTag.Style);
245
246                         foreach (StyleEntry entry in entries) {
247                                 CssStyleCollection sts = entry.Style.GetStyleAttributes (entry.UrlResolver);
248                                 writer.Write ("\n" + entry.Selection + " {" + sts.Value + "}");
249                         }
250
251                         writer.RenderEndTag ();
252                 }
253         }
254 }
255