Пример #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;
            }
        }
Пример #2
0
        private void ProcessStartTagAttributes(HtmlDtd.TagDefinition tagDef)
        {
            HtmlTagIndex tagIndex = tagDef.tagIndex;

            if (tagIndex <= HtmlTagIndex.Font)
            {
                if (tagIndex != HtmlTagIndex.A)
                {
                    if (tagIndex != HtmlTagIndex.Font)
                    {
                        return;
                    }
                    HtmlToken.AttributeEnumerator enumerator = token.Attributes.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        HtmlAttribute current = enumerator.Current;
                        if (current.NameIndex == HtmlNameIndex.Face)
                        {
                            scratch.Reset();
                            scratch.AppendHtmlAttributeValue(current, 4096);
                            RecognizeInterestingFontName recognizeInterestingFontName = default(RecognizeInterestingFontName);
                            int num = 0;
                            while (num < scratch.Length && !recognizeInterestingFontName.IsRejected)
                            {
                                recognizeInterestingFontName.AddCharacter(scratch.Buffer[num]);
                                num++;
                            }
                            textMapping = recognizeInterestingFontName.TextMapping;
                            return;
                        }
                    }
                    return;
                }
                else if (outputAnchorLinks)
                {
                    HtmlToken.AttributeEnumerator enumerator2 = token.Attributes.GetEnumerator();
                    while (enumerator2.MoveNext())
                    {
                        HtmlAttribute current2 = enumerator2.Current;
                        if (current2.NameIndex == HtmlNameIndex.Href)
                        {
                            if (current2.IsAttrBegin)
                            {
                                urlScratch.Reset();
                            }
                            urlScratch.AppendHtmlAttributeValue(current2, 4096);
                            break;
                        }
                    }
                    if (token.IsTagEnd)
                    {
                        BufferString bufferString = urlScratch.BufferString;
                        bufferString.TrimWhitespace();
                        if (bufferString.Length != 0 && bufferString[0] != '#' && bufferString[0] != '?' && bufferString[0] != ';')
                        {
                            if (!lineStarted)
                            {
                                StartParagraphOrLine();
                            }
                            string text = bufferString.ToString();
                            if (text.IndexOf(' ') != -1)
                            {
                                text = text.Replace(" ", "%20");
                            }
                            output.AnchorUrl = text;
                            insideAnchor     = true;
                            if (urlCompareSink == null)
                            {
                                urlCompareSink = new UrlCompareSink();
                            }
                            urlCompareSink.Initialize(text);
                        }
                        urlScratch.Reset();
                        return;
                    }
                }
            }
            else
            {
                switch (tagIndex)
                {
                case HtmlTagIndex.Image:
                case HtmlTagIndex.Img:
                    if (outputImageLinks)
                    {
                        HtmlToken.AttributeEnumerator enumerator3 = token.Attributes.GetEnumerator();
                        while (enumerator3.MoveNext())
                        {
                            HtmlAttribute current3 = enumerator3.Current;
                            if (current3.NameIndex == HtmlNameIndex.Src)
                            {
                                if (current3.IsAttrBegin)
                                {
                                    urlScratch.Reset();
                                }
                                urlScratch.AppendHtmlAttributeValue(current3, 4096);
                            }
                            else if (current3.NameIndex == HtmlNameIndex.Alt)
                            {
                                if (current3.IsAttrBegin)
                                {
                                    imageAltText.Reset();
                                }
                                imageAltText.AppendHtmlAttributeValue(current3, 4096);
                            }
                            else if (current3.NameIndex == HtmlNameIndex.Height)
                            {
                                if (!current3.Value.IsEmpty)
                                {
                                    PropertyValue propertyValue;
                                    if (current3.Value.IsContiguous)
                                    {
                                        propertyValue = HtmlSupport.ParseNumber(current3.Value.ContiguousBufferString, HtmlSupport.NumberParseFlags.Length);
                                    }
                                    else
                                    {
                                        scratch.Reset();
                                        scratch.AppendHtmlAttributeValue(current3, 4096);
                                        propertyValue = HtmlSupport.ParseNumber(scratch.BufferString, HtmlSupport.NumberParseFlags.Length);
                                    }
                                    if (propertyValue.IsAbsRelLength)
                                    {
                                        imageHeightPixels = propertyValue.PixelsInteger;
                                        if (imageHeightPixels == 0)
                                        {
                                            imageHeightPixels = 1;
                                        }
                                    }
                                }
                            }
                            else if (current3.NameIndex == HtmlNameIndex.Width && !current3.Value.IsEmpty)
                            {
                                PropertyValue propertyValue2;
                                if (current3.Value.IsContiguous)
                                {
                                    propertyValue2 = HtmlSupport.ParseNumber(current3.Value.ContiguousBufferString, HtmlSupport.NumberParseFlags.Length);
                                }
                                else
                                {
                                    scratch.Reset();
                                    scratch.AppendHtmlAttributeValue(current3, 4096);
                                    propertyValue2 = HtmlSupport.ParseNumber(scratch.BufferString, HtmlSupport.NumberParseFlags.Length);
                                }
                                if (propertyValue2.IsAbsRelLength)
                                {
                                    imageWidthPixels = propertyValue2.PixelsInteger;
                                    if (imageWidthPixels == 0)
                                    {
                                        imageWidthPixels = 1;
                                    }
                                }
                            }
                        }
                        if (token.IsTagEnd)
                        {
                            string       imageUrl      = null;
                            string       text2         = null;
                            BufferString bufferString2 = imageAltText.BufferString;
                            bufferString2.TrimWhitespace();
                            if (bufferString2.Length != 0)
                            {
                                text2 = bufferString2.ToString();
                            }
                            if (text2 == null || output.ImageRenderingCallbackDefined)
                            {
                                BufferString bufferString3 = urlScratch.BufferString;
                                bufferString3.TrimWhitespace();
                                if (bufferString3.Length != 0)
                                {
                                    imageUrl = bufferString3.ToString();
                                }
                            }
                            if (!lineStarted)
                            {
                                StartParagraphOrLine();
                            }
                            output.OutputImage(imageUrl, text2, imageWidthPixels, imageHeightPixels);
                            urlScratch.Reset();
                            imageAltText.Reset();
                            imageHeightPixels = 0;
                            imageWidthPixels  = 0;
                            return;
                        }
                    }
                    break;

                default:
                    if (tagIndex != HtmlTagIndex.P)
                    {
                        if (tagIndex != HtmlTagIndex.Span)
                        {
                            return;
                        }
                        HtmlToken.AttributeEnumerator enumerator4 = token.Attributes.GetEnumerator();
                        while (enumerator4.MoveNext())
                        {
                            HtmlAttribute current4 = enumerator4.Current;
                            if (current4.NameIndex == HtmlNameIndex.Style)
                            {
                                scratch.Reset();
                                scratch.AppendHtmlAttributeValue(current4, 4096);
                                RecognizeInterestingFontNameInInlineStyle recognizeInterestingFontNameInInlineStyle = default(RecognizeInterestingFontNameInInlineStyle);
                                int num2 = 0;
                                while (num2 < scratch.Length && !recognizeInterestingFontNameInInlineStyle.IsFinished)
                                {
                                    recognizeInterestingFontNameInInlineStyle.AddCharacter(scratch.Buffer[num2]);
                                    num2++;
                                }
                                textMapping = recognizeInterestingFontNameInInlineStyle.TextMapping;
                                return;
                            }
                        }
                    }
                    else if (token.Attributes.Find(HtmlNameIndex.Class))
                    {
                        HtmlAttribute current5 = token.Attributes.Current;
                        if (current5.Value.CaseInsensitiveCompareEqual("msonormal"))
                        {
                            wideGap = false;
                            nextParagraphCloseWideGap = false;
                            return;
                        }
                    }
                    break;
                }
            }
        }