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
42 string descriptionText;
44 HtmlMeta descriptionMeta;
45 HtmlMeta keywordsMeta;
49 StyleSheetBag styleSheet;
51 public HtmlHead(): base("head") {}
53 public HtmlHead (string tag) : base (tag)
57 protected internal override void OnInit (EventArgs e)
63 throw new HttpException ("The <head runat=\"server\"> control requires a page.");
65 //You can only have one <head runat="server"> control on a page.
66 if(page.Header != null)
67 throw new HttpException ("You can only have one <head runat=\"server\"> control on a page.");
68 page.SetHeader (this);
71 protected internal override void RenderChildren (HtmlTextWriter writer)
73 base.RenderChildren (writer);
75 writer.RenderBeginTag (HtmlTextWriterTag.Title);
76 if (!String.IsNullOrEmpty (titleText))
77 writer.Write (titleText);
78 writer.RenderEndTag ();
80 if (descriptionMeta == null && descriptionText != null) {
81 writer.AddAttribute ("name", "description");
82 writer.AddAttribute ("content", HttpUtility.HtmlAttributeEncode (descriptionText));
83 writer.RenderBeginTag (HtmlTextWriterTag.Meta);
84 writer.RenderEndTag ();
87 if (keywordsMeta == null && keywordsText != null) {
88 writer.AddAttribute ("name", "keywords");
89 writer.AddAttribute ("content", HttpUtility.HtmlAttributeEncode (keywordsText));
90 writer.RenderBeginTag (HtmlTextWriterTag.Meta);
91 writer.RenderEndTag ();
93 if (styleSheet != null)
94 styleSheet.Render (writer);
97 protected internal override void AddedControl (Control control, int index)
99 //You can only have one <title> element within the <head> element.
100 HtmlTitle t = control as HtmlTitle;
103 throw new HttpException ("You can only have one <title> element within the <head> element.");
107 HtmlMeta meta = control as HtmlMeta;
109 if (String.Compare ("keywords", meta.Name, StringComparison.OrdinalIgnoreCase) == 0)
111 else if (String.Compare ("description", meta.Name, StringComparison.OrdinalIgnoreCase) == 0)
112 descriptionMeta = meta;
114 base.AddedControl (control, index);
117 protected internal override void RemovedControl (Control control)
119 if (title == control)
122 if (keywordsMeta == control)
124 else if (descriptionMeta == control)
125 descriptionMeta = null;
126 base.RemovedControl (control);
128 public string Description {
130 if (descriptionMeta != null)
131 return descriptionMeta.Content;
132 return descriptionText;
136 if (descriptionMeta != null)
137 descriptionMeta.Content = value;
139 descriptionText = value;
143 public string Keywords {
145 if (keywordsMeta != null)
146 return keywordsMeta.Content;
151 if (keywordsMeta != null)
152 keywordsMeta.Content = value;
154 keywordsText = value;
158 public IStyleSheet StyleSheet {
160 if (styleSheet == null) styleSheet = new StyleSheetBag ();
165 public string Title {
181 internal class StyleSheetBag: IStyleSheet
183 ArrayList entries = new ArrayList ();
185 internal class StyleEntry
188 public string Selection;
189 public IUrlResolutionService UrlResolver;
192 public StyleSheetBag ()
196 public void CreateStyleRule (Style style, IUrlResolutionService urlResolver, string selection)
198 StyleEntry entry = new StyleEntry ();
200 entry.UrlResolver = urlResolver;
201 entry.Selection = selection;
205 public void RegisterStyle (Style style, IUrlResolutionService urlResolver)
207 for (int n=0; n<entries.Count; n++) {
208 if (((StyleEntry)entries[n]).Style == style)
212 string name = "aspnet_" + entries.Count;
213 style.SetRegisteredCssClass (name);
214 CreateStyleRule (style, urlResolver, "." + name);
217 public void Render (HtmlTextWriter writer)
219 writer.AddAttribute ("type", "text/css", false);
220 writer.RenderBeginTag (HtmlTextWriterTag.Style);
222 foreach (StyleEntry entry in entries) {
223 CssStyleCollection sts = entry.Style.GetStyleAttributes (entry.UrlResolver);
224 writer.Write ("\n" + entry.Selection + " {" + sts.Value + "}");
227 writer.RenderEndTag ();