Beispiel #1
0
        static Attr DecodeAttr(HtmlTokenizer.Attr attrRaw, bool sanitize)
        {
            var name    = attrRaw.Name;
            var isValid = HtmlAttrs.AttrNames.ContainsKey(name);

            if (isValid && sanitize)
            {
                name = name.ToLower();
            }
            var val = attrRaw.Val != null?HtmlCharCoding.Decode(attrRaw.Val) : null;

            return(new Attr(isValid, name, val));
        }
Beispiel #2
0
        static string EncodeTag(Tag tag, Encoding encoding)
        {
            var sb      = new StringBuilder();
            var padAttr = false;

            foreach (var attr in tag.Attrs)
            {
                if (padAttr)
                {
                    padAttr = false;
                    sb.Append(' ');
                }
                if (attr.Val == null)
                {
                    sb.Append(attr.Name);
                    padAttr = true;
                    continue;
                }
                bool leftWsesTrimmed;
                bool rightWsesTrimmed;
                if (attr.Val != "" && attr.Val[0] != '"' && attr.Val[0] != '\'' && !attr.Val.HasHtmlWhiteSpaceOrGt())
                {
                    sb.AppendFormat("{0}={1}", attr.Name, HtmlCharCoding.Encode(attr.Val, encoding, '>', false, out leftWsesTrimmed, out rightWsesTrimmed));
                    padAttr = true;
                    continue;
                }
                sb.AppendFormat("{0}={1}", attr.Name, (new[] {
                    string.Format("\"{0}\"", HtmlCharCoding.Encode(attr.Val, encoding, '"', false, out leftWsesTrimmed, out rightWsesTrimmed)),
                    attr.Val.IndexOf('"') >= 0 ? string.Format("'{0}'", HtmlCharCoding.Encode(attr.Val, encoding, '\'', false, out leftWsesTrimmed, out rightWsesTrimmed)) : null
                })
                                .Where(x => x != null).OrderBy(x => x.Length).First());
                padAttr = false;
            }
            var attrsHtml = sb.ToString();

            return(string.Format("<{0}{1}{2}>", tag.Name, attrsHtml != "" && attrsHtml != "/" ? " " : "", attrsHtml));
        }
Beispiel #3
0
 static string EncodeText(Text text, Encoding encoding, bool trimWses, out bool leftWsesTrimmed, out bool rightWsesTrimmed)
 {
     return(HtmlCharCoding.Encode(text.Val, encoding, '<', trimWses, out leftWsesTrimmed, out rightWsesTrimmed));
 }
Beispiel #4
0
 static Text DecodeText(string textRaw)
 {
     return(new Text(HtmlCharCoding.Decode(textRaw)));
 }