HtmlHead.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // System.Web.UI.HtmlControls.HtmlHead
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) 2004-2005 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. #if NET_2_0
  29. using System.ComponentModel;
  30. using System.Collections;
  31. using System.Security.Permissions;
  32. using System.Web.UI.WebControls;
  33. namespace System.Web.UI.HtmlControls
  34. {
  35. // CAS - no InheritanceDemand here as the class is sealed
  36. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. // attributes
  38. [ControlBuilder (typeof(HtmlHeadBuilder))]
  39. public sealed class HtmlHead: HtmlGenericControl, IParserAccessor {
  40. string titleText;
  41. HtmlTitle title;
  42. //Hashtable metadata;
  43. StyleSheetBag styleSheet;
  44. public HtmlHead(): base("head") {}
  45. public HtmlHead (string tag) : base (tag)
  46. {
  47. }
  48. protected internal override void OnInit (EventArgs e)
  49. {
  50. base.OnInit (e);
  51. Page page = Page;
  52. if (page == null)
  53. throw new HttpException ("The <head runat=\"server\"> control requires a page.");
  54. //You can only have one <head runat="server"> control on a page.
  55. if(page.Header != null)
  56. throw new HttpException ("You can only have one <head runat=\"server\"> control on a page.");
  57. page.SetHeader (this);
  58. }
  59. protected internal override void RenderChildren (HtmlTextWriter writer)
  60. {
  61. EnsureTitleControl ();
  62. base.RenderChildren (writer);
  63. // if (metadata != null) {
  64. // foreach (DictionaryEntry entry in metadata) {
  65. // writer.AddAttribute ("name", entry.Key.ToString ());
  66. // writer.AddAttribute ("content", entry.Value.ToString ());
  67. // writer.RenderBeginTag (HtmlTextWriterTag.Meta);
  68. // writer.RenderEndTag ();
  69. // }
  70. // }
  71. if (styleSheet != null)
  72. styleSheet.Render (writer);
  73. }
  74. protected internal override void AddedControl (Control control, int index)
  75. {
  76. //You can only have one <title> element within the <head> element.
  77. HtmlTitle t = control as HtmlTitle;
  78. if (t != null) {
  79. if (title != null)
  80. throw new HttpException ("You can only have one <title> element within the <head> element.");
  81. title = t;
  82. }
  83. base.AddedControl (control, index);
  84. }
  85. protected internal override void RemovedControl (Control control)
  86. {
  87. if (title == control)
  88. title = null;
  89. base.RemovedControl (control);
  90. }
  91. void EnsureTitleControl () {
  92. if (title != null)
  93. return;
  94. HtmlTitle t = new HtmlTitle ();
  95. t.Text = titleText;
  96. Controls.Add (t);
  97. }
  98. // IList LinkedStyleSheets {
  99. // get {
  100. // if (styleSheets == null) styleSheets = new ArrayList ();
  101. // return styleSheets;
  102. // }
  103. // }
  104. //
  105. // IDictionary Metadata {
  106. // get {
  107. // if (metadata == null) metadata = new Hashtable ();
  108. // return metadata;
  109. // }
  110. // }
  111. public IStyleSheet StyleSheet {
  112. get {
  113. if (styleSheet == null) styleSheet = new StyleSheetBag ();
  114. return styleSheet;
  115. }
  116. }
  117. public string Title {
  118. get {
  119. if (title != null)
  120. return title.Text;
  121. else
  122. return titleText;
  123. }
  124. set {
  125. if (title != null)
  126. title.Text = value;
  127. else
  128. titleText = value;
  129. }
  130. }
  131. }
  132. internal class StyleSheetBag: IStyleSheet
  133. {
  134. ArrayList entries = new ArrayList ();
  135. internal class StyleEntry
  136. {
  137. public Style Style;
  138. public string Selection;
  139. public IUrlResolutionService UrlResolver;
  140. }
  141. public StyleSheetBag ()
  142. {
  143. }
  144. public void CreateStyleRule (Style style, IUrlResolutionService urlResolver, string selection)
  145. {
  146. StyleEntry entry = new StyleEntry ();
  147. entry.Style = style;
  148. entry.UrlResolver = urlResolver;
  149. entry.Selection = selection;
  150. entries.Add (entry);
  151. }
  152. public void RegisterStyle (Style style, IUrlResolutionService urlResolver)
  153. {
  154. for (int n=0; n<entries.Count; n++) {
  155. if (((StyleEntry)entries[n]).Style == style)
  156. return;
  157. }
  158. string name = "aspnet_" + entries.Count;
  159. style.SetRegisteredCssClass (name);
  160. CreateStyleRule (style, urlResolver, "." + name);
  161. }
  162. public void Render (HtmlTextWriter writer)
  163. {
  164. writer.AddAttribute ("type", "text/css", false);
  165. writer.RenderBeginTag (HtmlTextWriterTag.Style);
  166. foreach (StyleEntry entry in entries) {
  167. CssStyleCollection sts = entry.Style.GetStyleAttributes (entry.UrlResolver);
  168. writer.Write ("\n" + entry.Selection + " {" + sts.Value + "}");
  169. }
  170. writer.RenderEndTag ();
  171. }
  172. }
  173. }
  174. #endif