示例#1
0
        /// <summary>
        /// Returns the content of specified tag.
        /// </summary>
        /// <param name="pageHtml">html code</param>
        /// <param name="justTagName">The tag name like TABLE</param>
        public static string GetTagContent(ref string pageHtml, string justTagName)
        {
            string startTag = '<' + justTagName;
            string endTag = justTagName + '>';
            int    start, end;

            start = StringCompare.IndexOfIgnoreCase(ref pageHtml, startTag);
            if (start == -1)
            {
                return("");
            }
            start = StringCompare.IndexOfMatchCase(ref pageHtml, '>', start);
            if (start == -1)
            {
                return("");
            }
            start++;

            end = StringCompare.IndexOfIgnoreCase(ref pageHtml, endTag, start);
            if (end == -1)
            {
                return("");
            }
            end = StringCompare.LastIndexOfMatchCase(ref pageHtml, '<', end);
            if (end == -1 || start > end)
            {
                return("");
            }

            return(pageHtml.Substring(start, end - start).Trim());
        }
示例#2
0
        public static TextRange FindCSSClassStyleUrlValuePosition(ref string pageHtml, int startindex)
        {
            int          valueStart, valueEnd;
            const string strCSSUrlValue = "url(";

            TextRange result;            // = new TextRange(-1, -1);

            result.End   = -1;
            result.Start = -1;


            //==============================
            if (startindex >= pageHtml.Length)
            {
                return(result);
            }


            // Find first position
            valueStart = StringCompare.IndexOfIgnoreCase(ref pageHtml, strCSSUrlValue, startindex);
            if (valueStart == -1)
            {
                return(result);
            }

            valueStart += strCSSUrlValue.Length;
            valueEnd    = StringCompare.IndexOfMatchCase(ref pageHtml, ")", valueStart);

            if (valueEnd == -1)
            {
                return(result);
            }

            if (valueEnd > StringCompare.IndexOfMatchCase(ref pageHtml, ";", valueStart))
            {
                return(result);
            }

            result.Start = valueStart;
            result.End   = valueEnd;
            return(result);
        }
示例#3
0
        /// <summary>
        /// Removes specified tag
        /// </summary>
        /// <param name="pageHtml"></param>
        /// <param name="tagName"></param>
        public static void RemoveTagOnly(ref string pageHtml, string tagName)
        {
            string tagEnd      = "</" + tagName + ">";
            string tagStart    = "<" + tagName;
            string tagStartEnd = ">";

            int tagStartPos    = 0;
            int tagStartEndPos = 0;
            int tagEndPos      = 0;

            do
            {
                tagStartPos = StringCompare.IndexOfIgnoreCase(ref pageHtml, tagStart, tagStartPos);
                if (tagStartPos == -1)
                {
                    return;
                }
                tagStartEndPos = StringCompare.IndexOfMatchCase(ref pageHtml, tagStartEnd, tagStartPos);

                if (tagStartEndPos == -1)
                {
                    return;
                }
                tagStartEndPos += tagStartEnd.Length;

                // removes tag start
                pageHtml = pageHtml.Remove(tagStartPos, tagStartEndPos - tagStartPos);


                // find tag end pos
                tagEndPos = StringCompare.IndexOfMatchCase(ref pageHtml, tagEnd, tagStartPos);

                if (tagEndPos == -1)
                {
                    return;
                }

                pageHtml = pageHtml.Remove(tagEndPos, tagEnd.Length);
            } while (tagEndPos != -1);
        }
示例#4
0
        /// <summary>
        /// Locates @import rule value
        /// </summary>
        /// <param name="cssCodes"></param>
        /// <param name="startindex"></param>
        /// <param name="onlyWithoutUrlOption">Specifies that operator should not catch rule with URL attribute.</param>
        /// <param name="captureExactInUrl">Specifies that operator should strip the URL attribute if there is any</param>
        /// <returns></returns>
        public static TextRange FindImportRuleUrlPosition(ref string cssCodes, int startindex, bool onlyWithoutUrlOption, bool captureExactlyInUrl)
        {
            int          valueStart, valueEnd;
            TextRange    result           = new TextRange(-1, -1);
            const string strCSSImportRule = "@import";
            const string strCSSUrlValue   = "url(";

            //==============================
            if (startindex >= cssCodes.Length)
            {
                return(result);
            }


            // Find first position
            valueStart = StringCompare.IndexOfIgnoreCase(ref cssCodes, strCSSImportRule, startindex);
            if (valueStart == -1)
            {
                return(result);
            }

            valueStart += strCSSImportRule.Length;

            //
            if (cssCodes.Substring(valueStart, 1).Trim().Length > 0)
            {
                return(result);
            }
            else
            {
                valueStart++;
            }

            valueEnd = StringCompare.IndexOfMatchCase(ref cssCodes, ";", valueStart);

            if (valueEnd == -1)
            {
                return(result);
            }
            else
            {
                int urlPos = StringCompare.IndexOfIgnoreCase(ref cssCodes, strCSSUrlValue, valueStart);
                if (urlPos != -1 && urlPos < valueEnd)
                {
                    if (onlyWithoutUrlOption)
                    {
                        result.Start = valueEnd;
                        result.End   = -1;
                        return(result);
                    }

                    if (captureExactlyInUrl)
                    {
                        valueStart = urlPos + strCSSUrlValue.Length;

                        urlPos = StringCompare.IndexOfMatchCase(ref cssCodes, ")", valueStart);
                        if (urlPos != -1 && urlPos < valueEnd)
                        {
                            valueEnd = urlPos;
                        }
                    }
                }
            }

            result.Start = valueStart;
            result.End   = valueEnd;
            return(result);
        }
示例#5
0
        /// <summary>
        /// Replaces head http refresh codes
        /// </summary>
        public static void ReplaceHttpRefresh(ref string htmlCodes,
                                              string pageUrlNoQuery,
                                              string newPageFormat,
                                              string pagePath,
                                              string rootUrl,
                                              bool encodeUrl)
        {
            const string tag           = "<meta";
            const string attr1         = "http-equiv";
            const string attr1Value    = "refresh";
            const string attr2         = "content";
            const string contentUrlTag = "url=";

            TextRange attr1Result = new TextRange(-1, -1);
            TextRange attr2Result = new TextRange(-1, -1);
            int       cursorPos = 0;
            string    tmp, actionSrc = "";

            do
            {
                // Find position of Meta tag and HTTP-EQUIV attribute
                attr1Result = HtmlParser.GetTagAttributeValuePos(ref htmlCodes, tag, attr1, cursorPos);
                if (attr1Result.Start > -1 && attr1Result.End > -1)
                {
                    string tmpRelType = htmlCodes.Substring(attr1Result.Start, attr1Result.End - attr1Result.Start);
                    if (tmpRelType.Trim().ToLower() != attr1Value.Trim().ToLower())
                    {
                        if (attr1Result.Start != -1)
                        {
                            cursorPos = attr1Result.Start;
                        }
                        continue;
                    }
                }
                else
                {
                    break;
                }

                // Find position of CONTENT attribute
                attr2Result = HtmlParser.GetTagAttributeValuePos(ref htmlCodes, tag, attr2, cursorPos);

                if (attr2Result.Start == -1)
                {
                    break;
                }

                if (attr2Result.Start > -1 && attr2Result.End > -1)
                {
                    cursorPos = attr2Result.Start;

                    // Get CONTENT attribute
                    actionSrc = htmlCodes.Substring(attr2Result.Start, attr2Result.End - attr2Result.Start);

                    // Some time CONTENT contains url address and refresh time, take them apart
                    string[] contentParts = actionSrc.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    // If the CONTENT does not contain any url address, no changes needed!
                    if (contentParts.Length < 2)
                    {
                        cursorPos = attr2Result.Start;
                        // next one!!
                        continue;
                    }

                    // remove Url= from CONTENT
                    actionSrc = contentParts[1].Trim();
                    if (actionSrc.ToLower().StartsWith(contentUrlTag))
                    {
                        actionSrc = actionSrc.Substring(contentUrlTag.Length, actionSrc.Length - contentUrlTag.Length);
                    }

                    // Convert virtual url to absolute
                    actionSrc = UrlProvider.JoinUrl(actionSrc, pageUrlNoQuery, pagePath, rootUrl);

                    // Delete invalid character such as tab and line feed ======
                    actionSrc = UrlProvider.IgnoreInvalidUrlCharctersInHtml(actionSrc);

                    // If there is a bookmark
                    if (actionSrc.IndexOf('#') != -1)
                    {
                        actionSrc = UrlBuilder.RemoveUrlBookmark(actionSrc, out tmp);
                    }

                    // Get clear url
                    actionSrc = HttpUtility.HtmlDecode(actionSrc);

                    // Encode url to make it unknown
                    if (encodeUrl)
                    {
                        actionSrc = UrlProvider.EncodeUrl(actionSrc);
                    }
                    else
                    {
                        // just url safe
                        actionSrc = UrlProvider.EscapeUrlQuery(actionSrc);
                    }

                    // Add it to our url
                    actionSrc = string.Format(newPageFormat, actionSrc);

                    // Make CONTENT attribute state
                    actionSrc = contentParts[0] + ';' + contentUrlTag + actionSrc;

                    // Make it safe
                    actionSrc = HttpUtility.HtmlEncode(actionSrc);

                    // Replace it with old url
                    htmlCodes = htmlCodes.Remove(attr2Result.Start, attr2Result.End - attr2Result.Start);
                    htmlCodes = htmlCodes.Insert(attr2Result.Start, actionSrc);
                }
                else
                {
                    if (attr2Result.Start != -1)
                    {
                        cursorPos = attr2Result.Start;
                    }
                    cursorPos = StringCompare.IndexOfMatchCase(ref htmlCodes, ">", cursorPos);
                }
            }while (attr2Result.Start != -1);
        }
示例#6
0
        public static void ReplaceFormsSources(ref string htmlCodes,
                                               string pageUrlNoQuery,
                                               string newPageFormat,
                                               string pagePath,
                                               string siteRootUrl,
                                               bool encodeUrl,
                                               bool changeMethod,
                                               string extraAttributeFormat)
        {
            TextRange methodResult;            // = new TextRange();
            TextRange actionResult;            // = new TextRange();
            int       cursorPos = 0;
            string    newAttribute = "";
            string    formMethod = "";
            string    tmp, actionSrc = "";
            string    orgValue = "";

            bool addNewAttribute = false;
            bool hasNewAttribute = false;

            if (!string.IsNullOrEmpty(extraAttributeFormat))
            {
                addNewAttribute = true;
                hasNewAttribute = true;
            }


            do
            {
                addNewAttribute = hasNewAttribute;

                if (changeMethod)
                {
                    methodResult = HtmlParser.GetTagAttributeValuePos(ref htmlCodes, "<form", "method", cursorPos);
                    if (methodResult.Start > -1 && methodResult.End > -1)
                    {
                        // get the method
                        formMethod = htmlCodes.Substring(methodResult.Start, 2);

                        // validate the method
                        formMethod = WebMethods.DetectMethod(formMethod, WebMethods.DefaultMethods.GET);

                        htmlCodes = htmlCodes.Remove(methodResult.Start, methodResult.End - methodResult.Start);
                        htmlCodes = htmlCodes.Insert(methodResult.Start, "POST");
                    }
                    else
                    {
                        int formPos = StringCompare.IndexOfIgnoreCase(ref htmlCodes, "<form", cursorPos);
                        int tagEnd;
                        if (formPos != -1)
                        {
                            tagEnd = StringCompare.IndexOfMatchCase(ref htmlCodes, '>', formPos);
                            if (tagEnd != -1)
                            {
                                htmlCodes = htmlCodes.Insert(tagEnd, " method=POST ");
                            }
                        }

                        formMethod = WebMethods.GET;
                    }
                }

                actionResult = HtmlParser.GetTagAttributeValuePos(ref htmlCodes, "<form", "action", cursorPos);

                if (actionResult.Start == -1)
                {
                    break;
                }

                if (actionResult.Start > -1 && actionResult.End > -1)
                {
                    cursorPos = actionResult.Start;


                    //====== Correct value position according to quotes existence=======
                    // actionResult = ASProxyFunctions.CorrectValueIfQuoteExists(ref pageHtml, actionResult);

                    // Get the value
                    actionSrc = htmlCodes.Substring(actionResult.Start, actionResult.End - actionResult.Start);

                    // BUG fixed in v5 beta 2
                    // now supports forms with javascript
                    if (UrlProvider.IsClientSitdeUrl(actionSrc) == false)
                    {
                        //====== Convert virtual url to absolute ======
                        actionSrc = UrlProvider.JoinUrl(actionSrc, pageUrlNoQuery, pagePath, siteRootUrl);

                        //====== Delete invalid character such as tab and line feed ======
                        actionSrc = UrlProvider.IgnoreInvalidUrlCharctersInHtml(actionSrc);

                        orgValue = actionSrc;

                        //===== If another site url, has bookmark=====
                        if (actionSrc.IndexOf('#') != -1)
                        {
                            actionSrc = UrlBuilder.RemoveUrlBookmark(actionSrc, out tmp);
                        }

                        //=====Get desired address=======
                        actionSrc = HttpUtility.HtmlDecode(actionSrc);

                        //====== Encode url to make unknown it ======
                        if (encodeUrl)
                        {
                            actionSrc = UrlProvider.EncodeUrl(actionSrc);
                        }
                        else
                        {
                            // just url safe
                            actionSrc = UrlProvider.EscapeUrlQuery(actionSrc);
                        }

                        //====== Add it to our url ======
                        actionSrc = string.Format(newPageFormat, actionSrc);

                        if (changeMethod)
                        {
                            //actionSrc = UrlBuilder.AddUrlQuery(actionSrc, Consts.qIsPostForm, ((int)method).ToString());
                            actionSrc = UrlBuilder.AddUrlQueryToEnd(actionSrc, Consts.Query.WebMethod, formMethod);
                        }


                        // Make it html safe
                        actionSrc = HttpUtility.HtmlEncode(actionSrc);

                        //====== Replace it with old url ======
                        htmlCodes = htmlCodes.Remove(actionResult.Start, actionResult.End - actionResult.Start);
                        htmlCodes = htmlCodes.Insert(actionResult.Start, actionSrc);
                    }
                    else
                    {
                        // this is client side url
                        addNewAttribute = false;
                    }


                    if (addNewAttribute)
                    {
                        // Apply orginal value and encoded value to format
                        newAttribute = string.Format(extraAttributeFormat, orgValue, actionSrc, "POST");

                        // Locate end of tag
                        cursorPos = StringCompare.IndexOfMatchCase(ref htmlCodes, '>', actionResult.Start);
                        if (htmlCodes[cursorPos - 1] == '/')
                        {
                            cursorPos--;
                        }

                        // Insert to it
                        htmlCodes = htmlCodes.Insert(cursorPos, newAttribute);
                    }
                }
                else
                {
                    if (actionResult.Start != -1)
                    {
                        cursorPos = actionResult.Start;
                    }
                    cursorPos = StringCompare.IndexOfMatchCase(ref htmlCodes, ">", cursorPos);
                }
            }while (actionResult.Start != -1);
        }
示例#7
0
        private static void ReplaceTagSrcAttribute(ref string htmlCodes,
                                                   string pageUrlNoQuery,
                                                   string tagName,
                                                   string attributeName,
                                                   string pagePath,
                                                   string newPageFormat,
                                                   string siteRootUrl,
                                                   bool encodeUrl,
                                                   string extraAttributeFormat,
                                                   bool canEncloseWithTags)
        {
            int       index = 0;      //====== In first run, index must be Zero ======
            TextRange position;
            string    oldValue, newValue;
            string    orgValue        = "";
            string    bookmarkPart    = "";
            string    newAttribute    = "";
            bool      addNewAttribute = false;
            bool      hasNewAttribute = false;

            if (!string.IsNullOrEmpty(extraAttributeFormat))
            {
                addNewAttribute = true;
                hasNewAttribute = true;
            }

            do
            {
                addNewAttribute = hasNewAttribute;

                position = HtmlParser.GetTagAttributeValuePos(ref htmlCodes, tagName, attributeName, index);

                if (position.Start == -1)
                {
                    break;
                }

                // If requested, test statement that shouldn't enclose with specified tags
                bool continueLoop = true;

                // this causes heavy pressure
                //if (canEncloseWithTags && position.Start != -1 && position.End != -1)
                //    continueLoop = !HtmlTags.IsEnclosedBy(ref htmlCodes, position.Start, "<script", "</script>");

                if (continueLoop &&
                    position.Start != -1 &&
                    position.End != -1 &&
                    position.End > position.Start)
                {
                    //======OK. go to end of tag=============
                    index = StringCompare.IndexOfMatchCase(ref htmlCodes, '>', position.Start);

                    // Replace new address

                    //====== Correct value position according to quotes existence=======
                    //position = ASProxyFunctions.CorrectValueIfQuoteExists(ref pageHtml, position);

                    //====== Get the attribute value ======
                    oldValue = htmlCodes.Substring(position.Start, position.End - position.Start);

                    oldValue = HtmlTags.RemoveEscapeQuotesFromTagAttributeValue(oldValue);

                    oldValue = HtmlTags.RemoveQuotesFromTagAttributeValue(oldValue);

                    //===== If link is a bookmark don't change it=====
                    if (oldValue.StartsWith("#"))
                    {
                        continue;
                    }

                    if (UrlProvider.IsClientSitdeUrl(oldValue) == false)
                    {
                        //====== Convert virtual url to absolute ======
                        oldValue = UrlProvider.JoinUrl(oldValue, pageUrlNoQuery, pagePath, siteRootUrl);

                        //====== Delete invalid character such as tab and line feed ======
                        oldValue = UrlProvider.IgnoreInvalidUrlCharctersInHtml(oldValue);

                        // Save orginal value
                        orgValue = oldValue;

                        //===== If another site url, has bookmark
                        if (oldValue.IndexOf('#') != -1)
                        {
                            oldValue = UrlBuilder.RemoveUrlBookmark(oldValue, out bookmarkPart);
                        }


                        //====== Get desigred url addrress
                        oldValue = HttpUtility.HtmlDecode(oldValue);

                        //====== Encode url to make it unknown ======
                        if (encodeUrl)
                        {
                            oldValue = UrlProvider.EncodeUrl(oldValue);
                        }
                        else
                        {
                            // just url safe
                            oldValue = UrlProvider.EscapeUrlQuery(oldValue);
                        }

                        //====== Add it to our url ======
                        newValue = string.Format(newPageFormat, oldValue);

                        //===== Add bookmark to last url =====
                        if (bookmarkPart.Length > 0)
                        {
                            newValue    += bookmarkPart;
                            bookmarkPart = "";
                        }
                    }
                    else
                    {
                        newValue        = oldValue;
                        addNewAttribute = false;
                    }

                    //====== Make it safe
                    newValue = HttpUtility.HtmlEncode(newValue);

                    //====== Replace it with old url
                    htmlCodes = htmlCodes.Remove(position.Start, position.End - position.Start);
                    htmlCodes = htmlCodes.Insert(position.Start, newValue);


                    if (addNewAttribute)
                    {
                        // Apply original value and encoded value to format
                        // BUG: Problem with format string that contain (') or (") characters
                        // Bug Fixed since version 4.7
                        newAttribute = string.Format(extraAttributeFormat, orgValue, newValue);

                        // Locate end of tag
                        index = StringCompare.IndexOfMatchCase(ref htmlCodes, '>', position.Start);
                        if (htmlCodes[index - 1] == '/')
                        {
                            index--;
                        }

                        // Insert to tag
                        htmlCodes = htmlCodes.Insert(index, newAttribute);
                    }

                    //===============End of Replace new address =========
                }
                else
                {
                    if (position.Start != -1)
                    {
                        index = position.Start;
                    }
                    index = StringCompare.IndexOfMatchCase(ref htmlCodes, '>', index);
                }
            }while ((index != -1));
        }
示例#8
0
        public static void ReplaceTwoAttributeTagsValue(ref string htmlCodes,
                                                        string newValueFormat,
                                                        bool encodeUrl,
                                                        string tagName,
                                                        string attr1,
                                                        string attr1Value,
                                                        string attr2)
        {
            TextRange attr1Result = new TextRange(-1, -1);
            TextRange attr2Result = new TextRange(-1, -1);
            int       cursorPos   = 0;
            string    actionSrc   = "";

            do
            {
                attr1Result = HtmlParser.GetTagAttributeValuePos(ref htmlCodes, tagName, attr1, cursorPos);
                if (attr1Result.Start > -1 && attr1Result.End > -1)
                {
                    string tmpRelType = htmlCodes.Substring(attr1Result.Start, attr1Result.End - attr1Result.Start);
                    if (tmpRelType.Trim().ToLower() != attr1Value.Trim().ToLower())
                    {
                        if (attr1Result.Start != -1)
                        {
                            cursorPos         = attr1Result.Start;
                            attr2Result.Start = attr1Result.Start;
                        }
                        continue;
                    }
                }
                else
                {
                    break;
                }

                attr2Result = HtmlParser.GetTagAttributeValuePos(ref htmlCodes, tagName, attr2, cursorPos);

                if (attr2Result.Start == -1)
                {
                    break;
                }

                if (attr2Result.Start > -1 && attr2Result.End > -1)
                {
                    cursorPos = attr2Result.Start;

                    //====== Correct value position according to quotes existence=======
                    attr2Result = HtmlTags.CorrectValueIfQuoteExists(ref htmlCodes, attr2Result);

                    // Get the value
                    actionSrc = htmlCodes.Substring(attr2Result.Start, attr2Result.End - attr2Result.Start);

                    // Get clear url
                    actionSrc = HttpUtility.HtmlDecode(actionSrc);

                    //====== Encode url to make it unknown ======
                    if (encodeUrl)
                    {
                        actionSrc = UrlProvider.EncodeUrl(actionSrc);
                    }
                    else
                    {
                        // just url safe
                        actionSrc = UrlProvider.EscapeUrlQuery(actionSrc);
                    }

                    //====== Add it to our url ======
                    actionSrc = string.Format(newValueFormat, actionSrc);

                    // Make it safe
                    actionSrc = HttpUtility.HtmlEncode(actionSrc);

                    //====== Replace it with old url ======
                    htmlCodes = htmlCodes.Remove(attr2Result.Start, attr2Result.End - attr2Result.Start);
                    htmlCodes = htmlCodes.Insert(attr2Result.Start, actionSrc);
                }
                else
                {
                    if (attr2Result.Start != -1)
                    {
                        cursorPos = attr2Result.Start;
                    }
                    cursorPos = StringCompare.IndexOfMatchCase(ref htmlCodes, ">", cursorPos);
                }
            }while (attr2Result.Start != -1);
        }
示例#9
0
        public static void ReplaceTwoAttributeTagsValue(ref string htmlCodes,
                                                        string pageUrlNoQuery,
                                                        string newPageFormat,
                                                        string pagePath,
                                                        string siteRootUrl,
                                                        bool encodeUrl,
                                                        string tagName,
                                                        string attr1,
                                                        string attr1Value,
                                                        string attr2)
        {
            TextRange attr1Result = new TextRange(-1, -1);
            TextRange attr2Result = new TextRange(-1, -1);
            int       cursorPos = 0;
            string    tmp, actionSrc = "";

            do
            {
                attr1Result = HtmlParser.GetTagAttributeValuePos(ref htmlCodes, tagName, attr1, cursorPos);
                if (attr1Result.Start > -1 && attr1Result.End > -1)
                {
                    string tmpRelType = htmlCodes.Substring(attr1Result.Start, attr1Result.End - attr1Result.Start);
                    if (tmpRelType.Trim().ToLower() != attr1Value.Trim().ToLower())
                    {
                        if (attr1Result.Start != -1)
                        {
                            cursorPos = attr1Result.Start;
                        }
                        continue;
                    }
                }
                else
                {
                    break;
                }

                attr2Result = HtmlParser.GetTagAttributeValuePos(ref htmlCodes, tagName, attr2, cursorPos);

                if (attr2Result.Start == -1)
                {
                    break;
                }

                if (attr2Result.Start > -1 && attr2Result.End > -1)
                {
                    cursorPos = attr2Result.Start;

                    //====== Correct value position according to quotes existence=======
                    attr2Result = HtmlTags.CorrectValueIfQuoteExists(ref htmlCodes, attr2Result);

                    // Get the value
                    actionSrc = htmlCodes.Substring(attr2Result.Start, attr2Result.End - attr2Result.Start);


                    //====== Convert virtual url to absolute ======
                    actionSrc = UrlProvider.JoinUrl(actionSrc, pageUrlNoQuery, pagePath, siteRootUrl);

                    //====== Delete invalid character such as tab and line feed ======
                    actionSrc = UrlProvider.IgnoreInvalidUrlCharctersInHtml(actionSrc);

                    //===== If another site url, has bookmark=====
                    if (actionSrc.IndexOf('#') != -1)
                    {
                        actionSrc = UrlBuilder.RemoveUrlBookmark(actionSrc, out tmp);
                    }

                    // Get clear url
                    actionSrc = HttpUtility.HtmlDecode(actionSrc);

                    //====== Encode url to make it unknown ======
                    if (encodeUrl)
                    {
                        actionSrc = UrlProvider.EncodeUrl(actionSrc);
                    }
                    else
                    {
                        // just url safe
                        actionSrc = UrlProvider.EscapeUrlQuery(actionSrc);
                    }

                    //====== Add it to our url ======
                    actionSrc = string.Format(newPageFormat, actionSrc);

                    // Make it safe
                    actionSrc = HttpUtility.HtmlEncode(actionSrc);

                    //====== Replace it with old url ======
                    htmlCodes = htmlCodes.Remove(attr2Result.Start, attr2Result.End - attr2Result.Start);
                    htmlCodes = htmlCodes.Insert(attr2Result.Start, actionSrc);
                }
                else
                {
                    if (attr2Result.Start != -1)
                    {
                        cursorPos = attr2Result.Start;
                    }
                    cursorPos = StringCompare.IndexOfMatchCase(ref htmlCodes, ">", cursorPos);
                }
            }while (attr2Result.Start != -1);
        }
示例#10
0
        /// <summary>
        /// Locate and remove a tag from html codes
        /// </summary>
        /// <param name="pageHtml">Html codes</param>
        /// <param name="tagName">Name of tags</param>
        /// <param name="removeTag">Remove the tag also</param>
        public static void RemoveTagContent(ref string pageHtml, string tagName, bool removeTag)
        {
            string tagEnd      = "</" + tagName + ">";
            string tagStart    = "<" + tagName;
            string tagStartEnd = ">";

            int tagStartPos    = 0;
            int tagStartEndPos = 0;
            int tagEndPos      = 0;

            if (removeTag)
            {
                do
                {
                    tagStartPos = StringCompare.IndexOfIgnoreCase(ref pageHtml, tagStart, tagStartPos);
                    if (tagStartPos == -1)
                    {
                        return;
                    }
                    tagStartEndPos = StringCompare.IndexOfMatchCase(ref pageHtml, tagStartEnd, tagStartPos);

                    if (tagStartEndPos == -1)
                    {
                        return;
                    }
                    tagStartEndPos += tagStartEnd.Length;

                    tagEndPos = StringCompare.IndexOfMatchCase(ref pageHtml, tagEnd, tagStartEndPos);

                    if (tagEndPos == -1)
                    {
                        return;
                    }
                    tagEndPos += tagEnd.Length;

                    pageHtml = pageHtml.Remove(tagStartPos, tagEndPos - tagStartPos);
                } while (tagEndPos != -1);
            }
            else
            {
                do
                {
                    tagStartPos = StringCompare.IndexOfIgnoreCase(ref pageHtml, tagStart, tagStartPos);
                    if (tagStartPos == -1)
                    {
                        return;
                    }
                    tagStartEndPos = StringCompare.IndexOfMatchCase(ref pageHtml, tagStartEnd, tagStartPos);

                    if (tagStartEndPos == -1)
                    {
                        return;
                    }
                    tagStartEndPos += tagStartEnd.Length;

                    tagEndPos = StringCompare.IndexOfMatchCase(ref pageHtml, tagEnd, tagStartEndPos);

                    if (tagEndPos == -1)
                    {
                        return;
                    }

                    pageHtml = pageHtml.Remove(tagStartEndPos, tagEndPos - tagStartEndPos);
                } while (tagEndPos != -1);
            }
        }
示例#11
0
        /// <summary>
        /// Locate value of attribute position for specified html tag
        /// </summary>
        public static TextRange GetTagAttributeValuePos(ref string pageHtml, string tagName, string attributeName, int start)
        {
            int tagStart, tagEnd;
            int attrStart;
            int valueStart;

            // Allocate result with default values
            TextRange result = new TextRange(-1, -1);

            // Incorrect input
            if (start >= pageHtml.Length)
            {
                return(result);
            }


            // Correct tag name
            if (tagName[0] != '<')
            {
                tagName = '<' + tagName;
            }

            //=============================================================//

            // Find tag first position
            tagStart = StringCompare.IndexOfIgnoreCase(ref pageHtml, tagName, start);
            if (tagStart == -1)
            {
                return(result);
            }

            // Locate end of tag
            tagEnd = StringCompare.IndexOfMatchCase(ref pageHtml, ">", tagStart);

            // Wrong html code
            if (tagEnd == -1)
            {
                return(result);
            }

            // Find the Attribute position
            attrStart = StringCompare.IndexOfIgnoreCase(ref pageHtml, attributeName, tagStart);


            // There is no attribute, so go away!
            if (attrStart == -1 || attrStart >= tagEnd)
            {
                // Set found start position to result
                result.Start = tagStart;
                result.End   = -1;
                return(result);
            }

            // Find value start position
            valueStart = StringCompare.IndexOfMatchCase(ref pageHtml, '=', attrStart);

            // There is no value, so go away!
            if (valueStart == -1 || valueStart >= tagEnd)
            {
                // Set found start position to result
                result.Start = attrStart;
                result.End   = -1;
                return(result);
            }

            // Locate value of attribute position
            result = FindAttributeValuePosition(ref pageHtml, tagEnd, valueStart);

            return(result);
        }
示例#12
0
        /// <summary>
        /// Finds and override url addresses within "style" tag.
        /// </summary>
        public static void ReplaceStyleTagStyleUrl(
            ref string htmlCode,
            string currentUrlWithoutParameters,
            string importRuleUrl,
            string othersNewUrl,
            string replacmentBasePath,
            string siteAddress,
            bool encodeUrl)
        {
            const string styleStart = "<style";
            const string styleEnd   = "</style>";
            const string htmlTagEnd = ">";
            TextRange    position;
            int          index = 0;

            //string bookmarkPart = "";

            do
            {
                string styleContent = "";

                // Find style tag position start
                position.Start = StringCompare.IndexOfIgnoreCase(ref htmlCode, styleStart, index);
                if (position.Start == -1)
                {
                    break;
                }

                // locate tag end
                position.Start = StringCompare.IndexOfMatchCase(ref htmlCode, htmlTagEnd, position.Start);
                if (position.Start == -1)
                {
                    break;
                }
                position.Start++;

                // Locate end tag
                position.End = StringCompare.IndexOfIgnoreCase(ref htmlCode, styleEnd, position.Start);
                if (position.End == -1)
                {
                    break;
                }

                // Set next searching start position
                index = position.End;

                // Get the content
                styleContent = htmlCode.Substring(position.Start, position.End - position.Start);

                // If there is nothing, go to next tag
                if (styleContent.Trim().Length == 0)
                {
                    continue;
                }

                // Process the style content for "Import Rule"
                // We don't need "Import Rule" process with "url" property any more. It will done by bext command. Since v4.0
                ReplaceCSSClassStyleUrl(ref styleContent, currentUrlWithoutParameters, importRuleUrl, replacmentBasePath, siteAddress, encodeUrl, true);

                // Process the style content for backgrounds.
                // So, this breach the backgound option role. Since v4.0
                ReplaceCSSClassStyleUrl(ref styleContent, currentUrlWithoutParameters, othersNewUrl, replacmentBasePath, siteAddress, encodeUrl, false);


                // Replace the new style
                htmlCode = htmlCode.Remove(position.Start, position.End - position.Start);
                htmlCode = htmlCode.Insert(position.Start, styleContent);
            } while (index != -1);
        }
示例#13
0
        /// <summary>
        /// Process the styles and replace them
        /// </summary>
        public static void ReplaceCSSClassStyleUrl(ref string htmlCode, string currentUrlWithoutParameters, string newUrl, string replacmentBasePath, string siteAddress, bool encodeUrl, bool forImportRule)
        {
            int       index = 0;
            TextRange position;
            string    oldValue, newValue;
            string    bookmarkPart = "";

            do
            {
                if (forImportRule)
                {
                    // do not find "Import Rule"s with url option, it will done by other codes. Since v4.0
                    position = CSSParser.FindImportRuleUrlPosition(ref htmlCode, index, false, false);
                }
                else
                {
                    position = CSSParser.FindCSSClassStyleUrlValuePosition(ref htmlCode, index);
                }

                if (position.Start == -1)
                {
                    break;
                }

                if (position.Start != -1 && position.End != -1)
                {
                    bool shouldAddQuote = false;
                    //======OK. go to end of tag=============
                    index = position.End;

                    //========================================================//
                    //====================Replace new address=================//
                    //========================================================//

                    //====== Correct value position according to quotes existence=======
                    position = HtmlTags.CorrectValueIfQuoteExists(ref htmlCode, position);


                    //====== Get the attribute value ======
                    oldValue = htmlCode.Substring(position.Start, position.End - position.Start);

                    // Trim!
                    oldValue = oldValue.Trim();

                    // Removes URL attribute if there is any
                    if (HtmlTags.RemoveUrlAttribCssLocation(oldValue, out oldValue))
                    {
                        shouldAddQuote = true;
                    }

                    oldValue = HtmlTags.RemoveQuotesFromTagAttributeValue(oldValue);
                    //===== If link is a bookmark don't change it=====
                    if (oldValue.StartsWith("#"))
                    {
                        continue;
                    }

                    //====== Convert virtual url to absolute ======
                    oldValue = UrlProvider.JoinUrl(oldValue, currentUrlWithoutParameters, replacmentBasePath, siteAddress);

                    //====== Delete invalid character such as tab and line feed ======
                    oldValue = UrlProvider.IgnoreInvalidUrlCharctersInHtml(oldValue);

                    //===== If another site url, has bookmark=====
                    if (StringCompare.IndexOfMatchCase(ref oldValue, '#') != -1)
                    {
                        oldValue = UrlBuilder.RemoveUrlBookmark(oldValue, out bookmarkPart);
                    }

                    //==== Make it clear=========
                    oldValue = HttpUtility.HtmlDecode(oldValue);

                    //====== Encode url to make it unknown ======
                    if (encodeUrl)
                    {
                        oldValue = UrlProvider.EncodeUrl(oldValue);
                    }
                    else
                    {
                        // just url safe
                        oldValue = UrlProvider.EscapeUrlQuery(oldValue);
                    }


                    //====== Add it to our url ======
                    newValue = string.Format(newUrl, oldValue);

                    //===== Add bookmark to last url =====
                    if (bookmarkPart.Length > 0)
                    {
                        newValue    += bookmarkPart;
                        bookmarkPart = "";
                    }

                    // Make it safe
                    //newValue = HttpUtility.HtmlEncode(newValue);

                    if (shouldAddQuote)
                    {
                        newValue = "\"" + newValue + "\"";
                    }

                    //====== Replace it with old url ======
                    htmlCode = htmlCode.Remove(position.Start, position.End - position.Start);
                    htmlCode = htmlCode.Insert(position.Start, newValue);

                    //========================================================//
                    //==============End of Replace new address================//
                    //========================================================//
                }
                else
                {
                    if (position.Start != -1)
                    {
                        index = position.Start;
                    }
                    index = StringCompare.IndexOfMatchCase(ref htmlCode, ' ', index);
                }
            }while ((index != -1));
        }
示例#14
0
        /// <summary>
        /// Finds and encode codes within "script" tag.
        /// </summary>
        public static void ReplaceScriptTagCodes(ref string htmlCode)
        {
            const string scriptStart = "<script";
            const string scriptEnd   = "</script>";
            const string htmlTagEnd  = ">";
            TextRange    position;
            int          index = 0;

            do
            {
                string scriptContent = "";

                // Find script tag position start
                position.Start = StringCompare.IndexOfIgnoreCase(ref htmlCode, scriptStart, index);
                if (position.Start == -1)
                {
                    break;
                }

                // locate tag end
                position.Start = StringCompare.IndexOfMatchCase(ref htmlCode, htmlTagEnd, position.Start);
                if (position.Start == -1)
                {
                    break;
                }
                position.Start++;

                // Locate end tag
                position.End = StringCompare.IndexOfIgnoreCase(ref htmlCode, scriptEnd, position.Start);
                if (position.End == -1)
                {
                    break;
                }

                // Set next searching start position
                index = position.End;

                // Get the content
                scriptContent = htmlCode.Substring(position.Start, position.End - position.Start);

                // If there is nothing, go to next tag
                if (scriptContent.Trim().Length == 0)
                {
                    continue;
                }


                // Very bad use of processor, But there is no other way to do!
                // Create a new instance of processor
                JSProcessor processor = new JSProcessor();

                // Process the script content
                processor.Execute(ref scriptContent,
                                  null, null, null, null);


                // Replace the new style
                htmlCode = htmlCode.Remove(position.Start, position.End - position.Start);
                htmlCode = htmlCode.Insert(position.Start, scriptContent);
            } while (index != -1);
        }