HtmlHead.cs 6.4 KB

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