Exemplo n.º 1
0
        protected override void OnTagProcessed(HtmlGenericControl tag)
        {
            HtmlImage image = new HtmlImage();

            image.Src = tag.Attributes["src"];
            if (BaseHref != null)
            {
                image.Src = HtmlUriExtractor.TryCreate(BaseHref, image.Src);
            }
            image.Alt = tag.Attributes["alt"];

            int width = 0;

            if (int.TryParse(tag.Attributes["width"], out width))
            {
                image.Width = width;
            }

            int height = 0;

            if (int.TryParse(tag.Attributes["height"], out height))
            {
                image.Height = height;
            }

            mImages.Add(image);
        }
Exemplo n.º 2
0
        public static List <Uri> Extract(string html, Uri root)
        {
            HtmlUriExtractor ex = new HtmlUriExtractor(html, root);

            while (!ex.EOF)
            {
                ex.Read();
            }
            return(ex.Uris);
        }
Exemplo n.º 3
0
        protected override bool OnAttributeFound(ref string name, ref string value)
        {
            switch (name.ToLower())
            {
            case "src":
            case "href":
                if (BaseHref != null)
                {
                    value = HtmlUriExtractor.TryCreate(BaseHref, value);
                }
                break;
            }

            return(base.OnAttributeFound(ref name, ref value));
        }
Exemplo n.º 4
0
        public override bool Read()
        {
            bool status = base.Read();

            if (status)
            {
                switch (base.NodeType)
                {
                case XmlNodeType.EndElement:

                    if (mObjectControls.Count < 1)
                    {
                        break;
                    }

                    if (mInsideObject && base.Name.ToLower() == mInsideObjectName)
                    {
                        mEmbeds.Add(mCurrent);
                        mInsideObject = false;
                        break;
                    }

                    HtmlControl top = mObjectControls.Pop();
                    break;

                case XmlNodeType.Element:

                    bool fObject = false;
                    bool fSize   = false;

                    switch (base.Name.ToLower())
                    {
                    case "object":
                    case "embed":
                        fSize = true;
                        if (!mInsideObject)
                        {
                            mInsideObjectName = base.Name;
                            mInsideObject     = fObject = true;
                        }
                        break;
                    }

                    if (!mInsideObject)
                    {
                        break;
                    }

                    HtmlGenericControl embed = new HtmlGenericControl(base.Name);

                    for (int i = 0; i < AttributeCount; i++)
                    {
                        string name  = GetAttributeName(i);
                        string value = GetAttribute(i);

                        switch (name.ToLower())
                        {
                        case "src":
                            if (BaseHref != null)
                            {
                                value = HtmlUriExtractor.TryCreate(BaseHref, value);
                            }
                            break;

                        case "height":
                        case "width":
                            if (!fSize)
                            {
                                continue;
                            }
                            break;

                        case "style":
                            continue;
                        }

                        embed.Attributes.Add(name, value);
                    }

                    if (fSize)
                    {
                        // width and height
                        Size size = new Size(242, 200);

                        //try
                        //{
                        //    Size current = new Size(int.Parse(GetAttribute("width")), int.Parse(GetAttribute("height")));
                        //    size = ThumbnailBitmap.GetNewSize(current, size);
                        //}
                        //catch
                        //{
                        //}

                        embed.Attributes["height"] = size.Height.ToString();
                        embed.Attributes["width"]  = size.Width.ToString();
                    }

                    if (fObject)
                    {
                        mCurrent = embed;
                    }
                    else if (mObjectControls.Count > 0)
                    {
                        mObjectControls.Peek().Controls.Add(embed);
                    }

                    mObjectControls.Push(embed);
                    break;
                }
            }
            return(status);
        }
Exemplo n.º 5
0
        public override void WriteAttributes(XmlReader reader, bool defattr)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            if ((reader.NodeType == XmlNodeType.Element) || (reader.NodeType == XmlNodeType.XmlDeclaration))
            {
                if (reader.MoveToFirstAttribute())
                {
                    this.WriteAttributes(reader, defattr);
                    reader.MoveToElement();
                }
            }
            else
            {
                if (reader.NodeType != XmlNodeType.Attribute)
                {
                    throw new XmlException("Xml_InvalidPosition");
                }
                do
                {
                    if (defattr || !reader.IsDefault)
                    {
                        string attributename = reader.LocalName.ToLower();

                        this.WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI);

                        while (reader.ReadAttributeValue())
                        {
                            if (reader.NodeType == XmlNodeType.EntityReference)
                            {
                                this.WriteEntityRef(reader.Name);
                                continue;
                            }

                            string value = reader.Value;

                            if (BaseHref != null &&
                                LastStartElement == "a" &&
                                attributename == "href")
                            {
                                value = HtmlUriExtractor.TryCreate(
                                    BaseHref, reader.Value, value);
                            }

                            if (BaseHref != null &&
                                (LastStartElement == "img" || LastStartElement == "embed") &&
                                attributename == "src")
                            {
                                value = HtmlUriExtractor.TryCreate(
                                    BaseHref, reader.Value, value);
                            }

                            this.WriteString(value);
                        }

                        this.WriteEndAttribute();
                    }
                } while (reader.MoveToNextAttribute());
            }
        }
Exemplo n.º 6
0
 public static List<Uri> Extract(string html, Uri root)
 {
     HtmlUriExtractor ex = new HtmlUriExtractor(html, root);
     while (!ex.EOF) ex.Read();
     return ex.Uris;
 }
Exemplo n.º 7
0
        /// <summary>
        /// This method is overriden to filter out attributes which are not allowed
        /// </summary>
        public override void WriteAttributes(XmlReader reader, bool defattr)
        {
            if (Options.FilterOutput)
            {
                // The following code is copied from implementation of XmlWriter's
                // WriteAttributes method.
                if (reader == null)
                {
                    throw new ArgumentNullException("reader");
                }
                if ((reader.NodeType == XmlNodeType.Element) || (reader.NodeType == XmlNodeType.XmlDeclaration))
                {
                    if (reader.MoveToFirstAttribute())
                    {
                        WriteAttributes(reader, defattr);
                        reader.MoveToElement();
                    }
                }
                else
                {
                    if (reader.NodeType != XmlNodeType.Attribute)
                    {
                        throw new XmlException("Xml_InvalidPosition");
                    }
                    do
                    {
                        if (defattr || !reader.IsDefault)
                        {
                            string attributename = reader.LocalName.ToLower();

                            // Check if the attribute is allowed
                            bool canWrite = true;

                            switch (LastStartElement)
                            {
                            case "embed":
                                canWrite = true;
                                break;

                            case "img":
                                if (Options.RewriteImgSize.HasValue &&
                                    Options.RewriteImgSize.Value.Width <= 0 &&
                                    Options.RewriteImgSize.Value.Height <= 0 &&
                                    (attributename == "width" || attributename == "height"))
                                {
                                    canWrite = false;
                                }
                                break;

                            default:
                                canWrite = (Array.IndexOf(Options.AllowedAttributes, attributename) >= 0);
                                break;
                            }

                            // If allowed, write the attribute
                            if (canWrite)
                            {
                                WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                            }

                            while (reader.ReadAttributeValue())
                            {
                                if (reader.NodeType == XmlNodeType.EntityReference)
                                {
                                    if (canWrite)
                                    {
                                        WriteEntityRef(reader.Name);
                                    }

                                    continue;
                                }

                                if (canWrite)
                                {
                                    string value = reader.Value;

                                    if (Options.BaseHref != null &&
                                        LastStartElement == "a" &&
                                        attributename == "href")
                                    {
                                        value = HtmlUriExtractor.TryCreate(
                                            Options.BaseHref, reader.Value, value);
                                    }

                                    if (Options.BaseHref != null &&
                                        (LastStartElement == "img" || LastStartElement == "embed") &&
                                        attributename == "src")
                                    {
                                        value = HtmlUriExtractor.TryCreate(
                                            Options.BaseHref, reader.Value, value);
                                    }

                                    if (Options.RewriteImgSrc != null &&
                                        LastStartElement == "img" &&
                                        attributename == "src")
                                    {
                                        value = Options.RewriteImgSrc.ToString().Replace("{url}",
                                                                                         Renderer.UrlEncode(value));
                                    }
                                    else if (Options.RewriteImgSize.HasValue &&
                                             LastStartElement == "img" &&
                                             attributename == "width")
                                    {
                                        value = Options.RewriteImgSize.Value.Width.ToString();
                                    }
                                    else if (Options.RewriteImgSize.HasValue &&
                                             LastStartElement == "img" &&
                                             attributename == "height")
                                    {
                                        value = Options.RewriteImgSize.Value.Height.ToString();
                                    }

                                    if (LastStartElement == "link" &&
                                        attributename == "rel")
                                    {
                                        switch (value.ToLower())
                                        {
                                        case "stylesheet":
                                            value = "stylesheet-stripped";
                                            break;
                                        }
                                    }

                                    WriteString(value);
                                }
                            }

                            if (canWrite)
                            {
                                WriteEndAttribute();
                            }
                        }
                    } while (reader.MoveToNextAttribute());
                }
            }
            else
            {
                base.WriteAttributes(reader, defattr);
            }
        }