1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
//------------------------------------------------------------------------------
// <copyright file="HtmlHead.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI.HtmlControls {
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;
public class HtmlHeadBuilder : ControlBuilder {
public override Type GetChildControlType(string tagName, IDictionary attribs) {
if (String.Equals(tagName, "title", StringComparison.OrdinalIgnoreCase))
return typeof(HtmlTitle);
if (String.Equals(tagName, "link", StringComparison.OrdinalIgnoreCase))
return typeof(HtmlLink);
if (String.Equals(tagName, "meta", StringComparison.OrdinalIgnoreCase))
return typeof(HtmlMeta);
return null;
}
public override bool AllowWhitespaceLiterals() {
return false;
}
}
/// <devdoc>
/// Represents the HEAD element.
/// </devdoc>
[
ControlBuilderAttribute(typeof(HtmlHeadBuilder))
]
public sealed class HtmlHead : HtmlGenericControl {
private StyleSheetInternal _styleSheet;
private HtmlTitle _title;
private String _cachedTitleText;
private HtmlMeta _description;
private String _cachedDescription;
private HtmlMeta _keywords;
private String _cachedKeywords;
/// <devdoc>
/// Initializes an instance of an HtmlHead class.
/// </devdoc>
public HtmlHead() : base("head") {
}
public HtmlHead(string tag) : base(tag) {
if (tag == null) {
tag = String.Empty;
}
_tagName = tag;
}
public IStyleSheet StyleSheet {
get {
if (_styleSheet == null) {
_styleSheet = new StyleSheetInternal(this);
}
return _styleSheet;
}
}
public String Title {
get {
if (_title == null) {
return _cachedTitleText;
}
return _title.Text;
}
set {
if (_title == null) {
// Side effect of adding a title to the control assigns _title
_cachedTitleText = value;
}
else {
_title.Text = value;
}
}
}
public String Description {
get {
if (_description == null) {
return _cachedDescription;
}
return _description.Content;
}
set {
if (_description == null) {
// Side effect of adding a description to the control assigns _description
_cachedDescription = value;
}
else {
_description.Content = value;
}
}
}
public String Keywords {
get {
if (_keywords == null) {
return _cachedKeywords;
}
return _keywords.Content;
}
set {
if (_keywords == null) {
// Side effect of adding a title to the control assigns _title
_cachedKeywords = value;
}
else {
_keywords.Content = value;
}
}
}
protected internal override void AddedControl(Control control, int index) {
base.AddedControl(control, index);
if (control is HtmlTitle) {
if (_title != null) {
throw new HttpException(SR.GetString(SR.HtmlHead_OnlyOneTitleAllowed));
}
_title = (HtmlTitle)control;
}
else if (control is HtmlMeta) {
// We will only use the first matching meta tag per, and ignore any others
HtmlMeta meta = (HtmlMeta)control;
if (_description == null && string.Equals(meta.Name, "description", StringComparison.OrdinalIgnoreCase)) {
_description = meta;
}
else if (_keywords == null && string.Equals(meta.Name, "keywords", StringComparison.OrdinalIgnoreCase)) {
_keywords = meta;
}
}
}
/// <internalonly/>
/// <devdoc>
/// Allows the HEAD element to register itself with the page.
/// </devdoc>
protected internal override void OnInit(EventArgs e) {
base.OnInit(e);
Page p = Page;
if (p == null) {
throw new HttpException(SR.GetString(SR.Head_Needs_Page));
}
if (p.Header != null) {
throw new HttpException(SR.GetString(SR.HtmlHead_OnlyOneHeadAllowed));
}
p.SetHeader(this);
}
internal void RegisterCssStyleString(string outputString) {
((StyleSheetInternal)StyleSheet).CSSStyleString = outputString;
}
protected internal override void RemovedControl(Control control) {
base.RemovedControl(control);
if (control is HtmlTitle) {
_title = null;
}
// There can be many meta tags, so we only clear it if its the correct meta
else if (control == _description) {
_description = null;
}
else if (control == _keywords) {
_keywords = null;
}
}
/// <internalonly/>
/// <devdoc>
/// Notifies the Page when the HEAD is being rendered.
/// </devdoc>
protected internal override void RenderChildren(HtmlTextWriter writer) {
base.RenderChildren(writer);
if (_title == null) {
// Always render out a <title> tag since it is required for xhtml 1.1 compliance
writer.RenderBeginTag(HtmlTextWriterTag.Title);
if (_cachedTitleText != null) {
writer.Write(_cachedTitleText);
}
writer.RenderEndTag();
}
if (_description == null && !String.IsNullOrEmpty(_cachedDescription)) {
// Go ahead and render out a meta tag if they set description but don't have a meta tag
writer.AddAttribute(HtmlTextWriterAttribute.Name, "description");
writer.AddAttribute(HtmlTextWriterAttribute.Content, _cachedDescription);
writer.RenderBeginTag(HtmlTextWriterTag.Meta);
writer.RenderEndTag();
}
if (_keywords == null && !String.IsNullOrEmpty(_cachedKeywords)) {
// Go ahead and render out a meta tag if they set keywords but don't have a meta tag
writer.AddAttribute(HtmlTextWriterAttribute.Name, "keywords");
writer.AddAttribute(HtmlTextWriterAttribute.Content, _cachedKeywords);
writer.RenderBeginTag(HtmlTextWriterTag.Meta);
writer.RenderEndTag();
}
if ((string)Page.Request.Browser["requiresXhtmlCssSuppression"] != "true") {
RenderStyleSheet(writer);
}
}
internal void RenderStyleSheet(HtmlTextWriter writer) {
if(_styleSheet != null) {
_styleSheet.Render(writer);
}
}
internal static void RenderCssRule(CssTextWriter cssWriter, string selector,
Style style, IUrlResolutionService urlResolver) {
cssWriter.WriteBeginCssRule(selector);
CssStyleCollection attrs = style.GetStyleAttributes(urlResolver);
attrs.Render(cssWriter);
cssWriter.WriteEndCssRule();
}
/// <devdoc>
/// Implements the IStyleSheet interface to represent an embedded
/// style sheet within the HEAD element.
/// </devdoc>
private sealed class StyleSheetInternal : IStyleSheet, IUrlResolutionService {
private HtmlHead _owner;
private ArrayList _styles;
private ArrayList _selectorStyles;
private int _autoGenCount;
public StyleSheetInternal(HtmlHead owner) {
_owner = owner;
}
// CssStyleString registered by the PartialCachingControl
private string _cssStyleString;
internal string CSSStyleString {
get {
return _cssStyleString;
}
set {
_cssStyleString = value;
}
}
public void Render(HtmlTextWriter writer) {
if ((_styles == null) && (_selectorStyles == null) && CSSStyleString == null) {
return;
}
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
writer.RenderBeginTag(HtmlTextWriterTag.Style);
CssTextWriter cssWriter = new CssTextWriter(writer);
if (_styles != null) {
for (int i = 0; i < _styles.Count; i++) {
StyleInfo si = (StyleInfo)_styles[i];
string cssClass = si.style.RegisteredCssClass;
if (cssClass.Length != 0) {
RenderCssRule(cssWriter, "." + cssClass, si.style, si.urlResolver);
}
}
}
if (_selectorStyles != null) {
for (int i = 0; i < _selectorStyles.Count; i++) {
SelectorStyleInfo si = (SelectorStyleInfo)_selectorStyles[i];
RenderCssRule(cssWriter, si.selector, si.style, si.urlResolver);
}
}
if (CSSStyleString != null) {
writer.Write(CSSStyleString);
}
writer.RenderEndTag();
}
#region Implementation of IStyleSheet
void IStyleSheet.CreateStyleRule(Style style, IUrlResolutionService urlResolver, string selector) {
if (style == null) {
throw new ArgumentNullException("style");
}
if (selector.Length == 0) {
throw new ArgumentNullException("selector");
}
if (_selectorStyles == null) {
_selectorStyles = new ArrayList();
}
if (urlResolver == null) {
urlResolver = this;
}
SelectorStyleInfo styleInfo = new SelectorStyleInfo();
styleInfo.selector = selector;
styleInfo.style = style;
styleInfo.urlResolver = urlResolver;
_selectorStyles.Add(styleInfo);
Page page = _owner.Page;
// If there are any partial caching controls on the stack, forward the styleInfo to them
if (page.PartialCachingControlStack != null) {
foreach (BasePartialCachingControl c in page.PartialCachingControlStack) {
c.RegisterStyleInfo(styleInfo);
}
}
}
void IStyleSheet.RegisterStyle(Style style, IUrlResolutionService urlResolver) {
if (style == null) {
throw new ArgumentNullException("style");
}
if (_styles == null) {
_styles = new ArrayList();
}
else if (style.RegisteredCssClass.Length != 0) {
// if it's already registered, throw an exception
throw new InvalidOperationException(SR.GetString(SR.HtmlHead_StyleAlreadyRegistered));
}
if (urlResolver == null) {
urlResolver = this;
}
StyleInfo styleInfo = new StyleInfo();
styleInfo.style = style;
styleInfo.urlResolver = urlResolver;
int index = _autoGenCount++;
string name = "aspnet_s" + index.ToString(NumberFormatInfo.InvariantInfo);
style.SetRegisteredCssClass(name);
_styles.Add(styleInfo);
}
#endregion
#region Implementation of IUrlResolutionService
string IUrlResolutionService.ResolveClientUrl(string relativeUrl) {
return _owner.ResolveClientUrl(relativeUrl);
}
#endregion
private sealed class StyleInfo {
public Style style;
public IUrlResolutionService urlResolver;
}
}
}
internal sealed class SelectorStyleInfo {
public string selector;
public Style style;
public IUrlResolutionService urlResolver;
}
}
|