示例#1
0
        public IList <string> GetCssStyles()
        {
            var attrs  = _op.Attributes;
            var result = new List <string>();

            Action <InlineStyleType, string, string> convert =
                (converter, value, name) =>
            {
                if (!String.IsNullOrEmpty(value))
                {
                    if (converter != null)
                    {
                        var converted = converter(value, _op);
                        if (!String.IsNullOrEmpty(converted))
                        {
                            result.Add(converted);
                        }
                    }
                    else
                    {
                        result.Add($"{name}:{value}");
                    }
                }
            };

            var iss = _options.InlineStyles;

            convert(iss?.Color ?? DEFAULT_INLINE_STYLES.Color,
                    attrs.Color, "color");

            if (iss != null ||
                !_options.AllowBackgroundClasses.HasValue ||
                !_options.AllowBackgroundClasses.Value)
            {
                convert(iss?.Background ?? DEFAULT_INLINE_STYLES.Background,
                        attrs.Background, "background-color");
            }
            if (iss != null)
            {
                if (attrs.Indent.HasValue)
                {
                    convert(iss.Indent ?? DEFAULT_INLINE_STYLES.Indent,
                            attrs.Indent.Value.ToString(), "indent");
                }
                convert(iss.Align ?? DEFAULT_INLINE_STYLES.Align,
                        AlignConverter.GetStringValue(attrs.Align), "text-align");
                convert(iss.Direction ?? DEFAULT_INLINE_STYLES.Direction,
                        DirectionConverter.GetStringValue(attrs.Direction), "direction");
                convert(iss.Font ?? DEFAULT_INLINE_STYLES.Font,
                        attrs.Font, "font-family");
                convert(iss.Size ?? DEFAULT_INLINE_STYLES.Size,
                        attrs.Size, "size");
            }
            return(result);
        }
示例#2
0
        public IList <string> GetCssClasses()
        {
            var attrs = _op.Attributes;

            if (_options.InlineStyles != null)
            {
                return(new string[0]);
            }

            var classes = new List <string>();

            if (attrs.Indent > 0)
            {
                classes.Add(PrefixClass($"indent-{attrs.Indent.Value}"));
            }
            if (attrs.Align.HasValue)
            {
                classes.Add(PrefixClass($"align-{AlignConverter.GetStringValue(attrs.Align)}"));
            }
            if (attrs.Direction.HasValue)
            {
                string dirnValue = DirectionConverter.GetStringValue(attrs.Direction);
                classes.Add(PrefixClass($"direction-{dirnValue}"));
            }
            if (!String.IsNullOrEmpty(attrs.Font))
            {
                classes.Add(PrefixClass($"font-{attrs.Font}"));
            }
            if (!String.IsNullOrEmpty(attrs.Size))
            {
                classes.Add(PrefixClass($"size-{attrs.Size}"));
            }
            if (_options.AllowBackgroundClasses == true &&
                !String.IsNullOrEmpty(attrs.Background) &&
                OpAttributeSanitizer.IsValidColorLiteral(attrs.Background))
            {
                classes.Add(PrefixClass($"background-{attrs.Background}"));
            }
            if (_op.IsFormula())
            {
                classes.Add(PrefixClass("formula"));
            }
            if (_op.IsVideo())
            {
                classes.Add(PrefixClass("video"));
            }
            if (_op.IsImage())
            {
                classes.Add(PrefixClass("image"));
            }
            return(classes);
        }
示例#3
0
        public static OpAttributes Sanitize(JToken dirtyAttrs)
        {
            var cleanAttrs = new OpAttributes();

            if (dirtyAttrs == null || dirtyAttrs.Type != JTokenType.Object)
            {
                return(cleanAttrs);
            }

            var font = dirtyAttrs.GetStringValue("font");

            if (!String.IsNullOrEmpty(font) && IsValidFontName(font))
            {
                cleanAttrs.Font = font;
            }

            var size = dirtyAttrs.GetStringValue("size");

            if (!String.IsNullOrEmpty(size) && IsValidSize(size))
            {
                cleanAttrs.Size = size;
            }

            var link = dirtyAttrs.GetStringValue("link");

            if (!String.IsNullOrEmpty(link))
            {
                cleanAttrs.Link = UrlHelpers.Sanitize(link);
            }

            var target = dirtyAttrs.GetStringValue("target");

            if (!String.IsNullOrEmpty(target) && IsValidTarget(target))
            {
                cleanAttrs.Target = target;
            }

            cleanAttrs.Script = ScriptConverter.GetEnumValue(
                dirtyAttrs.GetStringValue("script"));

            cleanAttrs.List = ListConverter.GetEnumValue(
                dirtyAttrs.GetStringValue("list"));

            var header = dirtyAttrs.GetIntValue("header");

            if (header.HasValue && header.Value > 0)
            {
                cleanAttrs.Header = Math.Min(header.Value, 6);
            }

            cleanAttrs.Align = AlignConverter.GetEnumValue(
                dirtyAttrs.GetStringValue("align"));

            cleanAttrs.Direction = DirectionConverter.GetEnumValue(
                dirtyAttrs.GetStringValue("direction"));

            var indent = dirtyAttrs.GetIntValue("indent");

            if (indent.HasValue)
            {
                cleanAttrs.Indent = Math.Min((int)indent.Value, 30);
            }

            var width = dirtyAttrs.GetStringValue("width");

            if (!String.IsNullOrEmpty(width) && IsValidWidth(width))
            {
                cleanAttrs.Width = width;
            }

            cleanAttrs.Bold          = dirtyAttrs.GetBoolValue("bold");
            cleanAttrs.Italic        = dirtyAttrs.GetBoolValue("italic");
            cleanAttrs.Underline     = dirtyAttrs.GetBoolValue("underline");
            cleanAttrs.Strike        = dirtyAttrs.GetBoolValue("strike");
            cleanAttrs.Code          = dirtyAttrs.GetBoolValue("code");
            cleanAttrs.Blockquote    = dirtyAttrs.GetBoolValue("blockquote");
            cleanAttrs.CodeBlock     = dirtyAttrs.GetBoolValue("code-block");
            cleanAttrs.RenderAsBlock = dirtyAttrs.GetBoolValue("renderAsBlock");

            cleanAttrs.Background = GetColour(dirtyAttrs.GetStringValue("background"));
            cleanAttrs.Color      = GetColour(dirtyAttrs.GetStringValue("color"));

            var mentions     = dirtyAttrs.GetBoolValue("mentions");
            var mentionToken = dirtyAttrs["mention"];

            if (mentionToken != null)
            {
                var mention = mentionToken.Value <JObject>();
                if (mentions.HasValue && mentions.Value && mention != null)
                {
                    var sanitizedMention = MentionSanitizer.Sanitize(mention);
                    if (sanitizedMention.AnySet)
                    {
                        cleanAttrs.Mentions = true;
                        cleanAttrs.Mention  = sanitizedMention;
                    }
                }
            }

            foreach (var kv in (JObject)dirtyAttrs)
            {
                if (s_sanitizedAttributes.Contains(kv.Key))
                {
                    continue;
                }

                if (cleanAttrs.CustomAttributes == null)
                {
                    cleanAttrs.CustomAttributes = new Dictionary <string, JToken>();
                }
                cleanAttrs.CustomAttributes.Add(kv.Key, kv.Value);
            }

            return(cleanAttrs);
        }