HtmlHead.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 sealed class HtmlHead: HtmlGenericControl, IPageHeader, IParserAccessor
  39. {
  40. HtmlTitle title;
  41. Hashtable metadata;
  42. ArrayList styleSheets;
  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. Page.SetHeader (this);
  51. }
  52. protected internal override void RenderChildren (HtmlTextWriter writer)
  53. {
  54. base.RenderChildren (writer);
  55. if (metadata != null) {
  56. foreach (DictionaryEntry entry in metadata) {
  57. writer.AddAttribute ("name", entry.Key.ToString ());
  58. writer.AddAttribute ("content", entry.Value.ToString ());
  59. writer.RenderBeginTag (HtmlTextWriterTag.Meta);
  60. writer.RenderEndTag ();
  61. }
  62. }
  63. if (styleSheet != null)
  64. styleSheet.Render (writer);
  65. }
  66. protected override void AddParsedSubObject (object ob)
  67. {
  68. if (ob is HtmlTitle)
  69. title = (HtmlTitle) ob;
  70. base.AddParsedSubObject (ob);
  71. }
  72. protected internal override void AddedControl (Control control, int index)
  73. {
  74. base.AddedControl (control, index);
  75. }
  76. /* Shows up in corcompare */
  77. [MonoTODO]
  78. protected internal override void RemovedControl (Control control)
  79. {
  80. throw new NotImplementedException ();
  81. }
  82. IList IPageHeader.LinkedStyleSheets {
  83. get {
  84. if (styleSheets == null) styleSheets = new ArrayList ();
  85. return styleSheets;
  86. }
  87. }
  88. IDictionary IPageHeader.Metadata {
  89. get {
  90. if (metadata == null) metadata = new Hashtable ();
  91. return metadata;
  92. }
  93. }
  94. IStyleSheet IPageHeader.StyleSheet {
  95. get {
  96. if (styleSheet == null) styleSheet = new StyleSheetBag ();
  97. return styleSheet;
  98. }
  99. }
  100. string IPageHeader.Title {
  101. get { return title.Text; }
  102. set { title.Text = value; }
  103. }
  104. }
  105. internal class StyleSheetBag: IStyleSheet
  106. {
  107. ArrayList entries = new ArrayList ();
  108. internal class StyleEntry
  109. {
  110. public Style Style;
  111. public string Selection;
  112. public IUrlResolutionService UrlResolver;
  113. }
  114. public StyleSheetBag ()
  115. {
  116. }
  117. public void CreateStyleRule (Style style, string selection, IUrlResolutionService urlResolver)
  118. {
  119. StyleEntry entry = new StyleEntry ();
  120. entry.Style = style;
  121. entry.UrlResolver = urlResolver;
  122. entry.Selection = selection;
  123. entries.Add (entry);
  124. }
  125. public void RegisterStyle (Style style, IUrlResolutionService urlResolver)
  126. {
  127. for (int n=0; n<entries.Count; n++) {
  128. if (((StyleEntry)entries[n]).Style == style)
  129. return;
  130. }
  131. string name = "aspnet_" + entries.Count;
  132. style.SetRegisteredCssClass (name);
  133. CreateStyleRule (style, "." + name, urlResolver);
  134. }
  135. public void Render (HtmlTextWriter writer)
  136. {
  137. writer.AddAttribute ("type", "text/css");
  138. writer.RenderBeginTag (HtmlTextWriterTag.Style);
  139. foreach (StyleEntry entry in entries) {
  140. CssStyleCollection sts = entry.Style.GetStyleAttributes (entry.UrlResolver);
  141. writer.Write ("\n" + entry.Selection + " {" + sts.BagToString () + "}");
  142. }
  143. writer.RenderEndTag ();
  144. }
  145. }
  146. }
  147. #endif