示例#1
0
        private void ProcessStartTagAttributes(HtmlDtd.TagDefinition tagDef)
        {
            switch (tagDef.tagIndex)
            {
            case HtmlTagIndex.A:


                if (outputAnchorLinks)
                {
                    foreach (var attr in token.Attributes)
                    {
                        if (attr.NameIndex == HtmlNameIndex.Href)
                        {
                            if (attr.IsAttrBegin)
                            {
                                urlScratch.Reset();
                            }

                            urlScratch.AppendHtmlAttributeValue(attr, HtmlSupport.MaxAttributeSize);
                            break;
                        }
                    }

                    if (token.IsTagEnd)
                    {
                        var url = urlScratch.BufferString;

                        url.TrimWhitespace();

                        if (url.Length != 0 && url[0] != '#' && url[0] != '?' && url[0] != ';')
                        {
                            if (!lineStarted)
                            {
                                StartParagraphOrLine();
                            }



                            var urlString = url.ToString();

                            if (urlString.IndexOf(' ') != -1)
                            {
                                urlString = urlString.Replace(" ", "%20");
                            }

                            output.OpenAnchor(urlString);
                            insideAnchor = true;

                            if (urlCompareSink == null)
                            {
                                urlCompareSink = new UrlCompareSink();
                            }

                            urlCompareSink.Initialize(urlString);
                        }

                        urlScratch.Reset();
                    }
                }
                break;

            case HtmlTagIndex.Image:
            case HtmlTagIndex.Img:



                if (outputImageLinks)
                {
                    foreach (var attr in token.Attributes)
                    {
                        if (attr.NameIndex == HtmlNameIndex.Src)
                        {
                            if (attr.IsAttrBegin)
                            {
                                urlScratch.Reset();
                            }

                            urlScratch.AppendHtmlAttributeValue(attr, HtmlSupport.MaxAttributeSize);
                        }
                        else if (attr.NameIndex == HtmlNameIndex.Alt)
                        {
                            if (attr.IsAttrBegin)
                            {
                                imageAltText.Reset();
                            }

                            imageAltText.AppendHtmlAttributeValue(attr, HtmlSupport.MaxAttributeSize);
                        }
                        else if (attr.NameIndex == HtmlNameIndex.Height)
                        {
                            if (!attr.Value.IsEmpty)
                            {
                                PropertyValue value;

                                if (attr.Value.IsContiguous)
                                {
                                    value = HtmlSupport.ParseNumber(attr.Value.ContiguousBufferString, HtmlSupport.NumberParseFlags.Length);
                                }
                                else
                                {
                                    scratch.Reset();
                                    scratch.AppendHtmlAttributeValue(attr, HtmlSupport.MaxAttributeSize);
                                    value = HtmlSupport.ParseNumber(scratch.BufferString, HtmlSupport.NumberParseFlags.Length);
                                }

                                if (value.IsAbsRelLength)
                                {
                                    imageHeightPixels = value.PixelsInteger;
                                    if (imageHeightPixels == 0)
                                    {
                                        imageHeightPixels = 1;
                                    }
                                }
                            }
                        }
                        else if (attr.NameIndex == HtmlNameIndex.Width)
                        {
                            if (!attr.Value.IsEmpty)
                            {
                                PropertyValue value;

                                if (attr.Value.IsContiguous)
                                {
                                    value = HtmlSupport.ParseNumber(attr.Value.ContiguousBufferString, HtmlSupport.NumberParseFlags.Length);
                                }
                                else
                                {
                                    scratch.Reset();
                                    scratch.AppendHtmlAttributeValue(attr, HtmlSupport.MaxAttributeSize);
                                    value = HtmlSupport.ParseNumber(scratch.BufferString, HtmlSupport.NumberParseFlags.Length);
                                }

                                if (value.IsAbsRelLength)
                                {
                                    imageWidthPixels = value.PixelsInteger;
                                    if (imageWidthPixels == 0)
                                    {
                                        imageWidthPixels = 1;
                                    }
                                }
                            }
                        }
                    }



                    if (token.IsTagEnd)
                    {
                        string urlString = null;
                        string altString = null;



                        var alt = imageAltText.BufferString;

                        alt.TrimWhitespace();

                        if (alt.Length != 0)
                        {
                            altString = alt.ToString();
                        }

                        if (altString == null || output.ImageRenderingCallbackDefined)
                        {
                            var url = urlScratch.BufferString;

                            url.TrimWhitespace();

                            if (url.Length != 0)
                            {
                                urlString = url.ToString();
                            }
                        }

                        if (!lineStarted)
                        {
                            StartParagraphOrLine();
                        }

                        output.OutputImage(urlString, altString, imageWidthPixels, imageHeightPixels);

                        urlScratch.Reset();
                        imageAltText.Reset();
                        imageHeightPixels = 0;
                        imageWidthPixels  = 0;
                    }
                }
                break;

            case HtmlTagIndex.P:



                if (token.Attributes.Find(HtmlNameIndex.Class) && token.Attributes.Current.Value.CaseInsensitiveCompareEqual("msonormal"))
                {
                    wideGap = false;
                    nextParagraphCloseWideGap = false;
                }
                break;

            case HtmlTagIndex.Font:

                foreach (var attr in token.Attributes)
                {
                    if (attr.NameIndex == HtmlNameIndex.Face)
                    {
                        scratch.Reset();
                        scratch.AppendHtmlAttributeValue(attr, HtmlSupport.MaxAttributeSize);

                        var fontRecognizer = new RecognizeInterestingFontName();

                        for (var i = 0; i < scratch.Length && !fontRecognizer.IsRejected; i++)
                        {
                            fontRecognizer.AddCharacter(scratch.Buffer[i]);
                        }

                        textMapping = fontRecognizer.TextMapping;
                        break;
                    }
                }

                break;


            case HtmlTagIndex.Span:

                foreach (var attr in token.Attributes)
                {
                    if (attr.NameIndex == HtmlNameIndex.Style)
                    {
                        scratch.Reset();
                        scratch.AppendHtmlAttributeValue(attr, HtmlSupport.MaxAttributeSize);

                        var fontRecognizer = new RecognizeInterestingFontNameInInlineStyle();

                        for (var i = 0; i < scratch.Length && !fontRecognizer.IsFinished; i++)
                        {
                            fontRecognizer.AddCharacter(scratch.Buffer[i]);
                        }

                        textMapping = fontRecognizer.TextMapping;
                        break;
                    }
                }

                break;
            }
        }