csscollection.cs
来自「浏览器端看到树型目录结构,用户可以完整地看到像windows资源管理器一样的效果」· CS 代码 · 共 757 行 · 第 1/2 页
CS
757 行
//------------------------------------------------------------------------------
// Copyright (c) 2000-2003 Microsoft Corporation. All Rights Reserved.
//------------------------------------------------------------------------------
namespace Microsoft.Web.UI.WebControls
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Collections.Specialized;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Reflection;
/// <summary>
/// Represents a collection of CSS attributes and their values.
/// </summary>
[
TypeConverterAttribute(typeof(CssCollectionConverter)),
Editor(typeof(Microsoft.Web.UI.WebControls.Design.CssCollectionEditor), typeof(UITypeEditor)),
]
public class CssCollection : NameObjectCollectionBase, ICloneable, IStateManager, IList
{
/// <summary>
/// Event called when an item is added.
/// </summary>
public event CssEventHandler ItemAdded;
/// <summary>
/// Event called when an item is removed.
/// </summary>
public event CssEventHandler ItemRemoved;
/// <summary>
/// Event called when the collection is cleared.
/// </summary>
public event EventHandler Cleared;
private bool _bRenderFontTag = false; // Tracks if a begin FONT tag was rendered
private bool _bRenderBoldTag = false; // Tracks if a begin B tag was rendered
private bool _bRenderItalicTag = false; // Trakcs if a begin I tag was rendered
private bool _IsTrackingViewState = false;
private bool _Dirty;
// Taken from System.Web.UI.CssStyleCollection
private static readonly Regex _StyleAttribRegex = new Regex(
"\\G(\\s*" + // any leading spaces
"(?(stylename);\\s*)" + // if stylename was already matched,
// match a semicolon and spaces
"(?<stylename>[^:]+?)" + // match stylename - chars up to the semicolon
"\\s*:\\s*" + // spaces, then the colon, then more spaces
"(?<styleval>[^;]+?)" + // now match styleval
")*\\s*;?\\s*$", // match a trailing semicolon and trailing spaces
RegexOptions.Singleline |
RegexOptions.Multiline);
/// <summary>
/// Initializes a new instance of a CssCollection.
/// </summary>
public CssCollection() : base()
{
}
/// <summary>
/// Initializes a new instance of a CssCollection.
/// </summary>
/// <param name="col">A collection to initialize this collection with.</param>
public CssCollection(CssCollection col) : base()
{
Merge(col, true);
}
/// <summary>
/// Initializes a new instance of a CssCollection with an CssStyleCollection.
/// </summary>
/// <param name="style">A CssStyleCollection to initialize the collection.</param>
public CssCollection(CssStyleCollection style) : base()
{
Merge(style, true);
}
/// <summary>
/// Initializes a new instance of a CssCollection.
/// </summary>
/// <param name="cssText">A CSS string to initialize this collection with.</param>
public CssCollection(string cssText) : base()
{
CssText = cssText;
}
/// <summary>
/// Creates a CssCollection from a string.
/// </summary>
/// <param name="cssText">The CSS text string.</param>
/// <returns>A new CssCollection.</returns>
public static CssCollection FromString(string cssText)
{
return new CssCollection(cssText);
}
/// <summary>
/// Clones this collection.
/// </summary>
/// <returns>A copy of this collection.</returns>
public virtual object Clone()
{
CssCollection col = (CssCollection)Activator.CreateInstance(this.GetType(), BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance, null, new object[] { this }, null);
col._bRenderFontTag = this._bRenderFontTag;
col._bRenderBoldTag = this._bRenderBoldTag;
col._bRenderItalicTag = this._bRenderItalicTag;
col._IsTrackingViewState = this._IsTrackingViewState;
col._Dirty = this._Dirty;
col.ItemAdded = this.ItemAdded;
col.ItemRemoved = this.ItemRemoved;
col.Cleared = this.Cleared;
return col;
}
/// <summary>
/// Adds a new CSS attribute/value pair.
/// </summary>
/// <param name="name">The CSS attribute name.</param>
/// <param name="value">The CSS value.</param>
public void Add(string name, string value)
{
name = name.ToLower().Trim();
if (this[name] != null)
BaseRemove(name);
BaseAdd((string)name.Clone(), value.Clone());
Dirty = true;
if (ItemAdded != null)
{
CssEventArgs e = new CssEventArgs(name, value);
ItemAdded(this, e);
}
}
/// <summary>
/// Clears this collection.
/// </summary>
public void Clear()
{
BaseClear();
Dirty = true;
if (Cleared != null)
{
Cleared(this, EventArgs.Empty);
}
}
/// <summary>
/// Removes a CSS attribute.
/// </summary>
/// <param name="name">The CSS attribute name.</param>
public void Remove(string name)
{
string val = this[name];
BaseRemove(name.ToLower());
Dirty = true;
if (ItemRemoved != null)
{
CssEventArgs e = new CssEventArgs(name, val);
ItemRemoved(this, e);
}
}
/// <summary>
/// Merges a CssCollection into this one.
/// </summary>
/// <param name="src">The source collection.</param>
/// <param name="overwrite">If true, will overwrite attributes of the same name.</param>
public void Merge(CssCollection src, bool overwrite)
{
foreach (string name in src)
{
if ((this[name] == null) || (overwrite))
{
Add(name, src[name]);
}
}
}
/// <summary>
/// Merges a CssStyleCollection into this one.
/// </summary>
/// <param name="src">The source collection.</param>
/// <param name="overwrite">If true, will overwrite attributes of the same name.</param>
public void Merge(CssStyleCollection src, bool overwrite)
{
foreach (string name in src.Keys)
{
if ((this[name] == null) || (overwrite))
{
Add(name, src[name]);
}
}
}
/// <summary>
/// Merges this collection into a CssStyleCollection.
/// </summary>
/// <param name="dest">The destination collection.</param>
/// <param name="overwrite">If true, will overwrite attributes of the same name.</param>
public void MergeInto(CssStyleCollection dest, bool overwrite)
{
foreach (string name in this)
{
if (dest[name] == null)
{
dest.Add(name, this[name]);
}
else if (overwrite)
{
dest[name] = this[name];
}
}
}
/// <summary>
/// Indexer into this collection.
/// </summary>
[Browsable(false)]
public string this[string name]
{
get { return (name == null) ? null : (string)BaseGet(name.ToLower()); }
set { Add(name, value); }
}
/// <summary>
/// Indexer into this collection.
/// </summary>
[Browsable(false)]
public string this[int index]
{
get { return this[Keys[index]]; }
set { this[Keys[index]] = value; }
}
/// <summary>
/// Gets the string version of this collection.
/// </summary>
[Browsable(false)]
public string CssText
{
get
{
string szCss = String.Empty;
foreach (string name in this)
{
szCss += name + ":" + this[name] + ";";
}
return szCss;
}
set
{
Clear();
AppendCssText(value);
}
}
/// <summary>
/// Appends a CSS text string to this collection.
/// </summary>
/// <param name="cssText">A CSS text string.</param>
public void AppendCssText(string cssText)
{
if (cssText != null)
{
Match match;
if ((match = _StyleAttribRegex.Match(cssText, 0)).Success)
{
CaptureCollection stylenames = match.Groups["stylename"].Captures;
CaptureCollection stylevalues = match.Groups["styleval"].Captures;
for (int i = 0; i < stylenames.Count; i++)
{
String styleName = stylenames[i].ToString();
String styleValue = stylevalues[i].ToString();
Add(styleName, styleValue);
}
}
}
}
/// <summary>
/// Converts this collection to a string.
/// </summary>
/// <returns>The string representation of this collection.</returns>
public override string ToString()
{
return CssText;
}
/// <summary>
/// Returns the color attribute.
/// </summary>
/// <returns>Color in Hex.</returns>
public string GetColor()
{
string color = this["color"];
if (color != null)
{
// Translate the string to a Color then translate it back
// to HTML to get it to convert to a hex number
try
{
return Util.ColorToHexString(color);
}
catch
{
// ignore
}
}
return null;
}
/// <summary>
/// Gets the font weight.
/// </summary>
/// <returns>The font weight.</returns>
public string GetFontWeight()
{
string szWeight = this["font-weight"];
if (szWeight != null)
{
return szWeight.ToLower();
}
return null;
}
/// <summary>
/// Gets the font style.
/// </summary>
/// <returns>The font style.</returns>
public string GetFontStyle()
{
string szStyle = this["font-style"];
if (szStyle != null)
{
return szStyle.ToLower();
}
return null;
}
/// <summary>
/// Gets the font face.
/// </summary>
/// <returns>The font face.</returns>
public string GetFontFace()
{
string szName = this["font-family"];
if (szName != null)
{
return szName;
}
return null;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?