HtmlHead.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // System.Web.UI.HtmlControls.HtmlHead
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2004 Novell, Inc.
  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;
  30. using System.ComponentModel;
  31. using System.Web;
  32. using System.Web.UI;
  33. using System.Web.UI.WebControls;
  34. using System.Collections;
  35. namespace System.Web.UI.HtmlControls
  36. {
  37. [ControlBuilder (typeof(HtmlHeadBuilder))]
  38. public class HtmlHead: HtmlContainerControl, IPageHeader
  39. {
  40. HtmlTitle title;
  41. Hashtable metadata;
  42. ArrayList styleSheets;
  43. StyleSheetBag styleSheet;
  44. public HtmlHead(): base("head") {}
  45. protected override void OnInit (EventArgs e)
  46. {
  47. Page.SetHeader (this);
  48. }
  49. protected override void RenderChildren (HtmlTextWriter writer)
  50. {
  51. base.RenderChildren (writer);
  52. if (metadata != null) {
  53. foreach (DictionaryEntry entry in metadata) {
  54. writer.AddAttribute ("name", entry.Key.ToString ());
  55. writer.AddAttribute ("content", entry.Value.ToString ());
  56. writer.RenderBeginTag (HtmlTextWriterTag.Meta);
  57. writer.RenderEndTag ();
  58. }
  59. }
  60. if (styleSheet != null)
  61. styleSheet.Render (writer);
  62. }
  63. protected override void AddParsedSubObject (object ob)
  64. {
  65. if (ob is HtmlTitle)
  66. title = (HtmlTitle) ob;
  67. base.AddParsedSubObject (ob);
  68. }
  69. protected internal override void AddedControl (Control control, int index)
  70. {
  71. base.AddedControl (control, index);
  72. }
  73. IList IPageHeader.LinkedStyleSheets {
  74. get {
  75. if (styleSheets == null) styleSheets = new ArrayList ();
  76. return styleSheets;
  77. }
  78. }
  79. IDictionary IPageHeader.Metadata {
  80. get {
  81. if (metadata == null) metadata = new Hashtable ();
  82. return metadata;
  83. }
  84. }
  85. IStyleSheet IPageHeader.StyleSheet {
  86. get {
  87. if (styleSheet == null) styleSheet = new StyleSheetBag ();
  88. return styleSheet;
  89. }
  90. }
  91. string IPageHeader.Title {
  92. get { return title.Text; }
  93. set { title.Text = value; }
  94. }
  95. }
  96. internal class StyleSheetBag: IStyleSheet
  97. {
  98. ArrayList entries = new ArrayList ();
  99. internal class StyleEntry
  100. {
  101. public Style Style;
  102. public string Selection;
  103. public IUrlResolutionService UrlResolver;
  104. }
  105. public StyleSheetBag ()
  106. {
  107. }
  108. public void CreateStyleRule (Style style, string selection, IUrlResolutionService urlResolver)
  109. {
  110. StyleEntry entry = new StyleEntry ();
  111. entry.Style = style;
  112. entry.UrlResolver = urlResolver;
  113. entry.Selection = selection;
  114. entries.Add (entry);
  115. }
  116. public void RegisterStyle (Style style, IUrlResolutionService urlResolver)
  117. {
  118. for (int n=0; n<entries.Count; n++) {
  119. if (((StyleEntry)entries[n]).Style == style)
  120. return;
  121. }
  122. string name = "aspnet_" + entries.Count;
  123. style.SetRegisteredCssClass (name);
  124. CreateStyleRule (style, "." + name, urlResolver);
  125. }
  126. public void Render (HtmlTextWriter writer)
  127. {
  128. writer.AddAttribute ("type", "text/css");
  129. writer.RenderBeginTag (HtmlTextWriterTag.Style);
  130. foreach (StyleEntry entry in entries) {
  131. CssStyleCollection sts = entry.Style.GetStyleAttributes (entry.UrlResolver);
  132. writer.Write ("\n" + entry.Selection + " {" + sts.BagToString () + "}");
  133. }
  134. writer.RenderEndTag ();
  135. }
  136. }
  137. }
  138. #endif