HtmlHead.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // System.Web.UI.HtmlControls.HtmlHead
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) 2004-2009 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. {
  41. #if NET_4_0
  42. string descriptionText;
  43. string keywordsText;
  44. HtmlMeta descriptionMeta;
  45. HtmlMeta keywordsMeta;
  46. #endif
  47. string titleText;
  48. HtmlTitle title;
  49. //Hashtable metadata;
  50. StyleSheetBag styleSheet;
  51. public HtmlHead(): base("head") {}
  52. public HtmlHead (string tag) : base (tag)
  53. {
  54. }
  55. protected internal override void OnInit (EventArgs e)
  56. {
  57. base.OnInit (e);
  58. Page page = Page;
  59. if (page == null)
  60. throw new HttpException ("The <head runat=\"server\"> control requires a page.");
  61. //You can only have one <head runat="server"> control on a page.
  62. if(page.Header != null)
  63. throw new HttpException ("You can only have one <head runat=\"server\"> control on a page.");
  64. page.SetHeader (this);
  65. }
  66. protected internal override void RenderChildren (HtmlTextWriter writer)
  67. {
  68. EnsureTitleControl ();
  69. base.RenderChildren (writer);
  70. // if (metadata != null) {
  71. // foreach (DictionaryEntry entry in metadata) {
  72. // writer.AddAttribute ("name", entry.Key.ToString ());
  73. // writer.AddAttribute ("content", entry.Value.ToString ());
  74. // writer.RenderBeginTag (HtmlTextWriterTag.Meta);
  75. // writer.RenderEndTag ();
  76. // }
  77. // }
  78. if (styleSheet != null)
  79. styleSheet.Render (writer);
  80. }
  81. protected internal override void AddedControl (Control control, int index)
  82. {
  83. //You can only have one <title> element within the <head> element.
  84. HtmlTitle t = control as HtmlTitle;
  85. if (t != null) {
  86. if (title != null)
  87. throw new HttpException ("You can only have one <title> element within the <head> element.");
  88. title = t;
  89. }
  90. #if NET_4_0
  91. HtmlMeta meta = control as HtmlMeta;
  92. if (meta != null) {
  93. if (String.Compare ("keywords", meta.Name, StringComparison.OrdinalIgnoreCase) == 0)
  94. keywordsMeta = meta;
  95. else if (String.Compare ("description", meta.Name, StringComparison.OrdinalIgnoreCase) == 0)
  96. descriptionMeta = meta;
  97. }
  98. #endif
  99. base.AddedControl (control, index);
  100. }
  101. protected internal override void RemovedControl (Control control)
  102. {
  103. if (title == control)
  104. title = null;
  105. #if NET_4_0
  106. if (keywordsMeta == control)
  107. keywordsMeta = null;
  108. else if (descriptionMeta == control)
  109. descriptionMeta = null;
  110. #endif
  111. base.RemovedControl (control);
  112. }
  113. void EnsureTitleControl () {
  114. if (title != null)
  115. return;
  116. HtmlTitle t = new HtmlTitle ();
  117. t.Text = titleText;
  118. Controls.Add (t);
  119. }
  120. // IList LinkedStyleSheets {
  121. // get {
  122. // if (styleSheets == null) styleSheets = new ArrayList ();
  123. // return styleSheets;
  124. // }
  125. // }
  126. //
  127. // IDictionary Metadata {
  128. // get {
  129. // if (metadata == null) metadata = new Hashtable ();
  130. // return metadata;
  131. // }
  132. // }
  133. #if NET_4_0
  134. public string Description {
  135. get {
  136. if (descriptionMeta != null)
  137. return descriptionMeta.Content;
  138. return descriptionText;
  139. }
  140. set {
  141. if (descriptionMeta != null)
  142. descriptionMeta.Content = value;
  143. else
  144. descriptionText = value;
  145. }
  146. }
  147. public string Keywords {
  148. get {
  149. if (keywordsMeta != null)
  150. return keywordsMeta.Content;
  151. return keywordsText;
  152. }
  153. set {
  154. if (keywordsMeta != null)
  155. keywordsMeta.Content = value;
  156. else
  157. keywordsText = value;
  158. }
  159. }
  160. #endif
  161. public IStyleSheet StyleSheet {
  162. get {
  163. if (styleSheet == null) styleSheet = new StyleSheetBag ();
  164. return styleSheet;
  165. }
  166. }
  167. public string Title {
  168. get {
  169. if (title != null)
  170. return title.Text;
  171. else
  172. return titleText;
  173. }
  174. set {
  175. if (title != null)
  176. title.Text = value;
  177. else
  178. titleText = value;
  179. }
  180. }
  181. }
  182. internal class StyleSheetBag: IStyleSheet
  183. {
  184. ArrayList entries = new ArrayList ();
  185. internal class StyleEntry
  186. {
  187. public Style Style;
  188. public string Selection;
  189. public IUrlResolutionService UrlResolver;
  190. }
  191. public StyleSheetBag ()
  192. {
  193. }
  194. public void CreateStyleRule (Style style, IUrlResolutionService urlResolver, string selection)
  195. {
  196. StyleEntry entry = new StyleEntry ();
  197. entry.Style = style;
  198. entry.UrlResolver = urlResolver;
  199. entry.Selection = selection;
  200. entries.Add (entry);
  201. }
  202. public void RegisterStyle (Style style, IUrlResolutionService urlResolver)
  203. {
  204. for (int n=0; n<entries.Count; n++) {
  205. if (((StyleEntry)entries[n]).Style == style)
  206. return;
  207. }
  208. string name = "aspnet_" + entries.Count;
  209. style.SetRegisteredCssClass (name);
  210. CreateStyleRule (style, urlResolver, "." + name);
  211. }
  212. public void Render (HtmlTextWriter writer)
  213. {
  214. writer.AddAttribute ("type", "text/css", false);
  215. writer.RenderBeginTag (HtmlTextWriterTag.Style);
  216. foreach (StyleEntry entry in entries) {
  217. CssStyleCollection sts = entry.Style.GetStyleAttributes (entry.UrlResolver);
  218. writer.Write ("\n" + entry.Selection + " {" + sts.Value + "}");
  219. }
  220. writer.RenderEndTag ();
  221. }
  222. }
  223. }
  224. #endif