Exemple #1
0
 /// <summary>
 /// Used to retrieve the object representation of the value of a CSS property if it has been explicitly set within this declaration block. This method returns null if the property is a shorthand property. Shorthand property values can only be accessed and modified as strings, using the getPropertyValue and setProperty methods.
 /// </summary>
 /// <param name="propertyName">The name of the CSS property. See the CSS property index.</param>
 /// <returns>Returns the value of the property if it has been explicitly set for this declaration block. Returns null if the property has not been set.</returns>
 public virtual ICssValue GetPropertyCssValue(string propertyName)
 {
     if (_styles.ContainsKey(propertyName))
     {
         CssStyleBlock scs = _styles[propertyName];
         if (scs.CssValue == null)
         {
             scs.CssValue = CssValue.GetCssValue(scs.Value, ReadOnly);
         }
         return(scs.CssValue);
     }
     return(null);
 }
        /// <summary>
        /// Used to remove a CSS property if it has been explicitly set within this declaration block.
        /// </summary>
        /// <param name="propertyName">The name of the CSS property. See the CSS property index.</param>
        /// <returns>Returns the value of the property if it has been explicitly set for this declaration block.
        /// Returns the empty string if the property has not been set or the property name does not correspond
        /// to a known CSS property.</returns>
        /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this declaration is readonly
        /// or the property is readonly.</exception>
        public string RemoveProperty(string propertyName)
        {
            if (_readOnly)
            {
                throw new DomException(DomExceptionType.NoModificationAllowedErr);
            }

            if (_styles.ContainsKey(propertyName))
            {
                CssStyleBlock s = _styles[propertyName];
                _styles.Remove(propertyName);
                return(s.Value);
            }

            return(string.Empty);
        }
Exemple #3
0
 /// <summary>
 /// Used to retrieve the object representation of the value of a CSS property if it has been explicitly set
 /// within this declaration block. This method returns null if the property is a shorthand property.
 /// Shorthand property values can only be accessed and modified as strings, using the getPropertyValue and
 /// setProperty methods.
 /// </summary>
 /// <param name="propertyName">The name of the CSS property. See the CSS property index.</param>
 /// <returns>Returns the value of the property if it has been explicitly set for this declaration block.
 /// Returns null if the property has not been set.</returns>
 public virtual ICssValue GetPropertyCssValue(string propertyName)
 {
     if (string.IsNullOrWhiteSpace(propertyName))
     {
         return(null);
     }
     if (_styles.ContainsKey(propertyName))
     {
         CssStyleBlock scs = _styles[propertyName];
         if (propertyName.Equals(SvgConstants.AttrFontFamily, StringComparison.OrdinalIgnoreCase))
         {
             scs.CssValue = new CssValue(CssValueType.PrimitiveValue, scs.Value, ReadOnly);
         }
         else if (scs.CssValue == null)
         {
             scs.CssValue = CssValue.GetCssValue(scs.Value, ReadOnly);
         }
         return(scs.CssValue);
     }
     return(null);
 }
 public CssStyleBlock(CssStyleBlock style, int specificity, CssStyleSheetType origin)
     : this(style.Name, style.Value, style.Priority, origin)
 {
     _specificity = specificity;
 }
        private string ParseString(string cssText)
        {
            bool startedWithABracket = false;

            cssText = cssText.Trim();
            if (cssText.StartsWith("{", StringComparison.OrdinalIgnoreCase))
            {
                cssText             = cssText.Substring(1).Trim();
                startedWithABracket = true;
            }

            Match match = _styleRegex.Match(cssText);

            while (match.Success)
            {
                string name  = match.Groups["name"].Value;
                string value = match.Groups["value"].Value;
                if (_parentRule != null)
                {
                    value = ((CssRule)_parentRule).DeReplaceStrings(value);
                }
                string prio = match.Groups["priority"].Value;

                CssStyleBlock style = new CssStyleBlock(name, value, prio, _origin);

                bool addStyle = false;
                if (_styles.ContainsKey(name))
                {
                    string existingPrio = _styles[name].Priority;

                    if (existingPrio != "important" || prio == "important")
                    {
                        _styles.Remove(name);
                        addStyle = true;
                    }
                }
                else
                {
                    addStyle = true;
                }

                if (addStyle)
                {
                    _styles.Add(name, style);
                }

                cssText = cssText.Substring(match.Length).Trim();
                match   = _styleRegex.Match(cssText);
            }

            cssText = cssText.Trim();
            if (cssText.StartsWith("}", StringComparison.OrdinalIgnoreCase))
            {
                cssText = cssText.Substring(1);
            }
            else if (startedWithABracket)
            {
                throw new DomException(DomExceptionType.SyntaxErr, "Style declaration ending bracket missing");
            }
            return(cssText);
        }
        private string parseString(string cssText)
        {
            bool startedWithABracket = false;

            cssText = cssText.Trim();
            if (cssText.StartsWith("{"))
            {
                cssText = cssText.Substring(1).Trim();
                startedWithABracket = true;
            }

            Match match = styleRegex.Match(cssText);
            while (match.Success)
            {
                string name = match.Groups["name"].Value;
                string value = match.Groups["value"].Value;
                if (_parentRule != null)
                {
                    value = ((CssRule)_parentRule).DeReplaceStrings(value);
                }
                string prio = match.Groups["priority"].Value;

                CssStyleBlock style = new CssStyleBlock(name, value, prio, _origin);

                bool addStyle = false;
                if (styles.ContainsKey(name))
                {
                    string existingPrio = ((CssStyleBlock)styles[name]).Priority;

                    if (existingPrio != "important" || prio == "important")
                    {
                        styles.Remove(name);
                        addStyle = true;
                    }
                }
                else
                {
                    addStyle = true;

                }

                if (addStyle)
                {
                    styles.Add(name, style);
                }

                cssText = cssText.Substring(match.Length).Trim();
                match = styleRegex.Match(cssText);
            }

            cssText = cssText.Trim();
            if (cssText.StartsWith("}"))
            {
                cssText = cssText.Substring(1);
            }
            else if (startedWithABracket)
            {
                throw new DomException(DomExceptionType.SyntaxErr, "Style declaration ending bracket missing");
            }
            return cssText;
        }
        /// <summary>
        /// Used to set a property value and priority within this declaration block
        /// </summary>
        /// <param name="propertyName">The name of the CSS property. See the CSS property index.</param>
        /// <param name="value">The new value of the property.</param>
        /// <param name="priority">The new priority of the property (e.g. "important").</param>
        /// <exception cref="DomException">SYNTAX_ERR: Raised if the specified value has a syntax error and is unparsable.</exception>
        /// <exception cref="DomException">NO_MODIFICATION_ALLOWED_ERR: Raised if this declaration is readonly or the property is readonly.</exception>
        public void SetProperty(string propertyName, string value, string priority)
        {
            if (_readOnly)
                throw new DomException(DomExceptionType.NoModificationAllowedErr);

            styles[propertyName] = new CssStyleBlock(propertyName, value, priority, _origin);
        }
Exemple #8
0
        private string ParseString(string cssText)
        {
            if (string.IsNullOrWhiteSpace(cssText))
            {
                return(string.Empty);
            }
            bool startedWithABracket = false;

            cssText = cssText.Trim();
            if (cssText.StartsWith("{", StringComparison.OrdinalIgnoreCase))
            {
                cssText             = cssText.Substring(1).Trim();
                startedWithABracket = true;
            }

            var quotes = new char[2] {
                '\'', '"'
            };

            Match match = _styleRegex.Match(cssText);

            while (match.Success)
            {
                string name  = match.Groups["name"].Value.Trim();
                string value = match.Groups["value"].Value.Trim();
                if (_parentRule != null)
                {
                    value = ((CssRule)_parentRule).DeReplaceStrings(value);
                }
                value = value.Trim(quotes);

                if (value.StartsWith("url(", StringComparison.OrdinalIgnoreCase))
                {
                    if (value.IndexOf("data:", StringComparison.OrdinalIgnoreCase) > 0)
                    {
                        var matchUrl = _reEmbeddedUrl.Match(cssText);
                        if (matchUrl != null && matchUrl.Groups != null && matchUrl.Groups.Count >= 3)
                        {
                            var nameUrl     = matchUrl.Groups["name"].Value;
                            var mimeUrl     = matchUrl.Groups["mime"].Value;
                            var encodingUrl = matchUrl.Groups["encoding"].Value;
                            var dataUrl     = matchUrl.Groups["data"].Value;

                            if (string.Equals(name, nameUrl, StringComparison.Ordinal) &&
                                !string.IsNullOrWhiteSpace(mimeUrl) &&
                                !string.IsNullOrWhiteSpace(encodingUrl) &&
                                !string.IsNullOrWhiteSpace(dataUrl))
                            {
                                value = matchUrl.Groups[0].Value.Remove(0, name.Length + 1).TrimEnd(';');
                                match = matchUrl;

                                int foundAt = dataUrl.IndexOf(")", StringComparison.Ordinal);

                                if (!_styles.ContainsKey(UrlName) && foundAt > 0)
                                {
                                    _styles.Add(UrlName, new CssStyleBlock(UrlName, nameUrl, string.Empty, _origin));
                                    _styles.Add(UrlMime, new CssStyleBlock(UrlMime, mimeUrl, string.Empty, _origin));
                                    _styles.Add(UrlData, new CssStyleBlock(UrlData, dataUrl.Substring(0, foundAt), string.Empty, _origin));
                                    _styles.Add(UrlEncoding, new CssStyleBlock(UrlEncoding, encodingUrl, string.Empty, _origin));
                                }
                            }
                        }
                    }
                }
                string prio = match.Groups["priority"].Value;

                CssStyleBlock style = new CssStyleBlock(name, value, prio, _origin);

                bool addStyle = false;
                if (_styles.ContainsKey(name))
                {
                    string existingPrio = _styles[name].Priority;

                    if (existingPrio != "important" || prio == "important")
                    {
                        _styles.Remove(name);
                        addStyle = true;
                    }
                }
                else
                {
                    addStyle = true;
                }

                if (addStyle)
                {
                    _styles.Add(name, style);
                }

                cssText = cssText.Substring(match.Length).Trim();
                match   = _styleRegex.Match(cssText);
            }

            cssText = cssText.Trim();
            if (cssText.StartsWith("}", StringComparison.OrdinalIgnoreCase))
            {
                cssText = cssText.Substring(1);
            }
            else if (startedWithABracket)
            {
                throw new DomException(DomExceptionType.SyntaxErr, "Style declaration ending bracket missing");
            }
            return(cssText);
        }
Exemple #9
0
 internal CssStyleBlock(CssStyleBlock style, int specificity, CssStyleSheetType origin)
     : this(style.Name, style.Value, style.Priority, origin)
 {
     Specificity = specificity;
 }
Exemple #10
0
        private string ParseString(string cssText)
        {
            if (string.IsNullOrWhiteSpace(cssText))
            {
                return(string.Empty);
            }
            bool startedWithABracket = false;

            cssText = cssText.Trim();
            if (cssText.StartsWith("{", StringComparison.OrdinalIgnoreCase))
            {
                cssText             = cssText.Substring(1).Trim();
                startedWithABracket = true;
            }

            var quotes = new char[2] {
                '\'', '"'
            };

            Match match = _styleRegex.Match(cssText);

            while (match.Success)
            {
                string name  = match.Groups["name"].Value.Trim();
                string value = match.Groups["value"].Value.Trim();
                if (_parentRule != null)
                {
                    value = ((CssRule)_parentRule).DeReplaceStrings(value);
                }
                value = value.Trim(quotes);

                if (value.StartsWith("url(", StringComparison.OrdinalIgnoreCase))
                {
                    if (value.IndexOf("data:", StringComparison.OrdinalIgnoreCase) > 0)
                    {
                        var matchUrl = _reEmbeddedUrl.Match(cssText);
                        if (matchUrl != null && matchUrl.Groups != null && matchUrl.Groups.Count >= 3)
                        {
                            var nameUrl     = matchUrl.Groups["name"].Value;
                            var mimeUrl     = matchUrl.Groups["mime"].Value;
                            var encodingUrl = matchUrl.Groups["encoding"].Value;
                            var dataUrl     = matchUrl.Groups["data"].Value;

                            if (string.Equals(name, nameUrl, StringComparison.Ordinal) &&
                                !string.IsNullOrWhiteSpace(mimeUrl) &&
                                !string.IsNullOrWhiteSpace(encodingUrl) &&
                                !string.IsNullOrWhiteSpace(dataUrl))
                            {
                                value = matchUrl.Groups[0].Value.Remove(0, name.Length + 1).TrimEnd(';');
                                match = matchUrl;

                                int foundAt = dataUrl.IndexOf(")", StringComparison.Ordinal);

                                if (!_styles.ContainsKey(UrlName) && foundAt > 0)
                                {
                                    _styles.Add(UrlName, new CssStyleBlock(UrlName, nameUrl, string.Empty, _origin));
                                    _styles.Add(UrlMime, new CssStyleBlock(UrlMime, mimeUrl, string.Empty, _origin));
                                    _styles.Add(UrlData, new CssStyleBlock(UrlData, dataUrl.Substring(0, foundAt), string.Empty, _origin));
                                    _styles.Add(UrlEncoding, new CssStyleBlock(UrlEncoding, encodingUrl, string.Empty, _origin));
                                }
                            }
                        }
                    }
                }
                else if (string.Equals(name, "font"))
                {
                    var fontParts = new string[] {
                        "font", "font-style", "font-variant", "font-weight",
                        "font-size", "line-height", "font-family", "font-family", "font-stretch"
                    };
                    int count = 0;

                    // The font CSS shorthand property sets all the different properties of an element's font.
                    var matchFont = _reFont.Match(value);
                    if (!matchFont.Success)
                    {
                        var fontValue = value.Trim();
                        var indexSel  = fontValue.IndexOf(' ');
                        if (indexSel > 0)
                        {
                            var preText = fontValue.Substring(0, indexSel).TrimStart();
                            fontValue = fontValue.Substring(indexSel).TrimStart();

                            var tempMatch = _reFontStretch.Match(preText);
                            if (tempMatch.Success)
                            {
                                var fontStretch = fontParts[8];
                                _styles.Add(fontStretch, new CssStyleBlock(fontStretch, tempMatch.Value, string.Empty, _origin));
                            }
                            else
                            {
                                tempMatch = _reFontWeight.Match(preText);
                                if (tempMatch.Success)
                                {
                                    var fontWeight = fontParts[3];
                                    _styles.Add(fontWeight, new CssStyleBlock(fontWeight, tempMatch.Value, string.Empty, _origin));
                                }
                            }
                        }
                        matchFont = _reFont.Match(fontValue);
                    }
                    if (matchFont.Success)
                    {
                        foreach (Group group in matchFont.Groups)
                        {
                            if (group.Success)
                            {
                                var fontPart = fontParts[count];
                                _styles.Add(fontPart, new CssStyleBlock(fontPart, group.Value, string.Empty, _origin));
                            }
                            count++;
                        }
                    }
                }

                string prio = match.Groups["priority"].Value;

                CssStyleBlock style = new CssStyleBlock(name, value, prio, _origin);

                bool addStyle = false;
                if (_styles.ContainsKey(name))
                {
                    string existingPrio = _styles[name].Priority;

                    if (existingPrio != "important" || prio == "important")
                    {
                        _styles.Remove(name);
                        addStyle = true;
                    }
                }
                else
                {
                    addStyle = true;
                }

                if (addStyle)
                {
                    _styles.Add(name, style);
                }

                cssText = cssText.Substring(match.Length).Trim();
                match   = _styleRegex.Match(cssText);
            }

            cssText = cssText.Trim();
            if (cssText.StartsWith("}", StringComparison.OrdinalIgnoreCase))
            {
                cssText = cssText.Substring(1);
            }
            else if (startedWithABracket)
            {
                throw new DomException(DomExceptionType.SyntaxErr, "Style declaration ending bracket missing");
            }
            return(cssText);
        }