Esempio n. 1
0
 /// <summary>
 /// Raises the <see cref="E:RemovingStyle" /> event.
 /// </summary>
 /// <param name="e">The <see cref="RemovingStyleEventArgs"/> instance containing the event data.</param>
 protected virtual void OnRemovingStyle(RemovingStyleEventArgs e)
 {
     if (RemovingStyle != null)
     {
         RemovingStyle(this, e);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Sanitizes the style.
        /// </summary>
        /// <param name="styles">The styles.</param>
        /// <param name="baseUrl">The base URL.</param>
        protected void SanitizeStyle(CsQuery.Implementation.CSSStyleDeclaration styles, string baseUrl)
        {
            if (styles == null || !styles.Any())
            {
                return;
            }

            var removeStyles = new List <KeyValuePair <string, string> >();
            var setStyles    = new Dictionary <string, string>();

            foreach (var style in styles)
            {
                var key = DecodeCss(style.Key);
                var val = DecodeCss(style.Value);

                if (!AllowedCssPropertiesSet.Contains(key) || CssExpression.IsMatch(val) || DisallowCssPropertyValue.IsMatch(val))
                {
                    removeStyles.Add(style);
                }
                else
                {
                    var urls = CssUrl.Matches(val);

                    if (urls.Count > 0)
                    {
                        if (urls.Cast <Match>().Any(m => GetSafeUri(m.Groups[1].Value) == null))
                        {
                            removeStyles.Add(style);
                        }
                        else
                        {
                            var s = CssUrl.Replace(val, m => "url(" + SanitizeUrl(m.Groups[1].Value, baseUrl));
                            if (s != val)
                            {
                                if (key != style.Key)
                                {
                                    removeStyles.Add(style);
                                }
                                setStyles[key] = s;
                            }
                        }
                    }
                }
            }

            foreach (var style in removeStyles)
            {
                var e = new RemovingStyleEventArgs {
                    Style = style
                };
                OnRemovingStyle(e);
                if (!e.Cancel)
                {
                    styles.RemoveStyle(style.Key);
                }
            }

            foreach (var kvp in setStyles)
            {
                styles.SetStyle(kvp.Key, kvp.Value);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Sanitizes the style.
        /// </summary>
        /// <param name="styles">The styles.</param>
        /// <param name="baseUrl">The base URL.</param>
        protected void SanitizeStyle(CsQuery.Implementation.CSSStyleDeclaration styles, string baseUrl)
        {
            if (styles == null || !styles.Any()) return;

            var removeStyles = new List<KeyValuePair<string, string>>();
            var setStyles = new Dictionary<string, string>();

            foreach (var style in styles)
            {
                var key = DecodeCss(style.Key);
                var val = DecodeCss(style.Value);

                if (!AllowedCssPropertiesSet.Contains(key) || CssExpression.IsMatch(val) || DisallowCssPropertyValue.IsMatch(val))
                    removeStyles.Add(style);
                else
                {
                    var urls = CssUrl.Matches(val);

                    if (urls.Count > 0)
                    {
                        if (urls.Cast<Match>().Any(m => GetSafeUri(m.Groups[1].Value) == null))
                            removeStyles.Add(style);
                        else
                        {
                            var s = CssUrl.Replace(val, m => "url(" + SanitizeUrl(m.Groups[1].Value, baseUrl));
                            if (s != val)
                            {
                                if (key != style.Key) removeStyles.Add(style);
                                setStyles[key] = s;
                            }
                        }
                    }
                }
            }

            foreach (var style in removeStyles)
            {
                var e = new RemovingStyleEventArgs { Style = style };
                OnRemovingStyle(e);
                if (!e.Cancel) styles.RemoveStyle(style.Key);
            }

            foreach (var kvp in setStyles)
            {
                styles.SetStyle(kvp.Key, kvp.Value);
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Raises the <see cref="E:RemovingStyle" /> event.
 /// </summary>
 /// <param name="e">The <see cref="RemovingStyleEventArgs"/> instance containing the event data.</param>
 protected virtual void OnRemovingStyle(RemovingStyleEventArgs e)
 {
     if (RemovingStyle != null) RemovingStyle(this, e);
 }