2 // System.Web.UI.HtmlControls.HtmlHead
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // Copyright (C) 2004-2010 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.ComponentModel;
30 using System.Collections;
31 using System.Security.Permissions;
32 using System.Web.UI.WebControls;
34 namespace System.Web.UI.HtmlControls
36 // CAS - no InheritanceDemand here as the class is sealed
37 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 [ControlBuilder (typeof(HtmlHeadBuilder))]
40 public sealed class HtmlHead: HtmlGenericControl, IParserAccessor
43 string descriptionText;
45 HtmlMeta descriptionMeta;
46 HtmlMeta keywordsMeta;
51 StyleSheetBag styleSheet;
53 public HtmlHead(): base("head") {}
55 public HtmlHead (string tag) : base (tag)
59 protected internal override void OnInit (EventArgs e)
65 throw new HttpException ("The <head runat=\"server\"> control requires a page.");
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);
73 protected internal override void RenderChildren (HtmlTextWriter writer)
75 EnsureTitleControl ();
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 ();
87 if (styleSheet != null)
88 styleSheet.Render (writer);
91 protected internal override void AddedControl (Control control, int index)
93 //You can only have one <title> element within the <head> element.
94 HtmlTitle t = control as HtmlTitle;
97 throw new HttpException ("You can only have one <title> element within the <head> element.");
102 HtmlMeta meta = control as HtmlMeta;
104 if (String.Compare ("keywords", meta.Name, StringComparison.OrdinalIgnoreCase) == 0)
106 else if (String.Compare ("description", meta.Name, StringComparison.OrdinalIgnoreCase) == 0)
107 descriptionMeta = meta;
110 base.AddedControl (control, index);
113 protected internal override void RemovedControl (Control control)
115 if (title == control)
119 if (keywordsMeta == control)
121 else if (descriptionMeta == control)
122 descriptionMeta = null;
124 base.RemovedControl (control);
127 void EnsureTitleControl () {
131 HtmlTitle t = new HtmlTitle ();
136 // IList LinkedStyleSheets {
138 // if (styleSheets == null) styleSheets = new ArrayList ();
139 // return styleSheets;
143 // IDictionary Metadata {
145 // if (metadata == null) metadata = new Hashtable ();
151 public string Description {
153 if (descriptionMeta != null)
154 return descriptionMeta.Content;
155 return descriptionText;
159 if (descriptionMeta != null)
160 descriptionMeta.Content = value;
162 descriptionText = value;
166 public string Keywords {
168 if (keywordsMeta != null)
169 return keywordsMeta.Content;
174 if (keywordsMeta != null)
175 keywordsMeta.Content = value;
177 keywordsText = value;
182 public IStyleSheet StyleSheet {
184 if (styleSheet == null) styleSheet = new StyleSheetBag ();
189 public string Title {
205 internal class StyleSheetBag: IStyleSheet
207 ArrayList entries = new ArrayList ();
209 internal class StyleEntry
212 public string Selection;
213 public IUrlResolutionService UrlResolver;
216 public StyleSheetBag ()
220 public void CreateStyleRule (Style style, IUrlResolutionService urlResolver, string selection)
222 StyleEntry entry = new StyleEntry ();
224 entry.UrlResolver = urlResolver;
225 entry.Selection = selection;
229 public void RegisterStyle (Style style, IUrlResolutionService urlResolver)
231 for (int n=0; n<entries.Count; n++) {
232 if (((StyleEntry)entries[n]).Style == style)
236 string name = "aspnet_" + entries.Count;
237 style.SetRegisteredCssClass (name);
238 CreateStyleRule (style, urlResolver, "." + name);
241 public void Render (HtmlTextWriter writer)
243 writer.AddAttribute ("type", "text/css", false);
244 writer.RenderBeginTag (HtmlTextWriterTag.Style);
246 foreach (StyleEntry entry in entries) {
247 CssStyleCollection sts = entry.Style.GetStyleAttributes (entry.UrlResolver);
248 writer.Write ("\n" + entry.Selection + " {" + sts.Value + "}");
251 writer.RenderEndTag ();