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 base.RenderChildren (writer);
77 writer.RenderBeginTag (HtmlTextWriterTag.Title);
78 if (!String.IsNullOrEmpty (titleText))
79 writer.Write (titleText);
80 writer.RenderEndTag ();
83 if (descriptionMeta == null && descriptionText != null) {
84 writer.AddAttribute ("name", "description");
85 writer.AddAttribute ("content", HttpUtility.HtmlAttributeEncode (descriptionText));
86 writer.RenderBeginTag (HtmlTextWriterTag.Meta);
87 writer.RenderEndTag ();
90 if (keywordsMeta == null && keywordsText != null) {
91 writer.AddAttribute ("name", "keywords");
92 writer.AddAttribute ("content", HttpUtility.HtmlAttributeEncode (keywordsText));
93 writer.RenderBeginTag (HtmlTextWriterTag.Meta);
94 writer.RenderEndTag ();
97 if (styleSheet != null)
98 styleSheet.Render (writer);
101 protected internal override void AddedControl (Control control, int index)
103 //You can only have one <title> element within the <head> element.
104 HtmlTitle t = control as HtmlTitle;
107 throw new HttpException ("You can only have one <title> element within the <head> element.");
112 HtmlMeta meta = control as HtmlMeta;
114 if (String.Compare ("keywords", meta.Name, StringComparison.OrdinalIgnoreCase) == 0)
116 else if (String.Compare ("description", meta.Name, StringComparison.OrdinalIgnoreCase) == 0)
117 descriptionMeta = meta;
120 base.AddedControl (control, index);
123 protected internal override void RemovedControl (Control control)
125 if (title == control)
129 if (keywordsMeta == control)
131 else if (descriptionMeta == control)
132 descriptionMeta = null;
134 base.RemovedControl (control);
137 public string Description {
139 if (descriptionMeta != null)
140 return descriptionMeta.Content;
141 return descriptionText;
145 if (descriptionMeta != null)
146 descriptionMeta.Content = value;
148 descriptionText = value;
152 public string Keywords {
154 if (keywordsMeta != null)
155 return keywordsMeta.Content;
160 if (keywordsMeta != null)
161 keywordsMeta.Content = value;
163 keywordsText = value;
168 public IStyleSheet StyleSheet {
170 if (styleSheet == null) styleSheet = new StyleSheetBag ();
175 public string Title {
191 internal class StyleSheetBag: IStyleSheet
193 ArrayList entries = new ArrayList ();
195 internal class StyleEntry
198 public string Selection;
199 public IUrlResolutionService UrlResolver;
202 public StyleSheetBag ()
206 public void CreateStyleRule (Style style, IUrlResolutionService urlResolver, string selection)
208 StyleEntry entry = new StyleEntry ();
210 entry.UrlResolver = urlResolver;
211 entry.Selection = selection;
215 public void RegisterStyle (Style style, IUrlResolutionService urlResolver)
217 for (int n=0; n<entries.Count; n++) {
218 if (((StyleEntry)entries[n]).Style == style)
222 string name = "aspnet_" + entries.Count;
223 style.SetRegisteredCssClass (name);
224 CreateStyleRule (style, urlResolver, "." + name);
227 public void Render (HtmlTextWriter writer)
229 writer.AddAttribute ("type", "text/css", false);
230 writer.RenderBeginTag (HtmlTextWriterTag.Style);
232 foreach (StyleEntry entry in entries) {
233 CssStyleCollection sts = entry.Style.GetStyleAttributes (entry.UrlResolver);
234 writer.Write ("\n" + entry.Selection + " {" + sts.Value + "}");
237 writer.RenderEndTag ();