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); }
/// <summary> /// Locate value of attribute position using specified start index /// </summary> internal static TextRange FindAttributeValuePosition(ref string htmlCode, int tagEnd, int valueStart) { // Allocate result with default values TextRange result; // = new TextRange(-1, -1); // Increase start position valueStart++; int valueStartPos = -1, valueEndPos = -1; int index = valueStart; char current; bool valueStartFound = false; bool continueDo = true; ValueStartType startType = ValueStartType.None; // Set default start position valueStartPos = valueStart; do { if (index >= tagEnd) { valueEndPos = index; break; } current = htmlCode[index]; if (valueStartFound == false && current != ' ' && current != '\r' && current != '\t' && current != '\n') { valueStartPos = index; valueStartFound = true; if (current == '\'') { startType = ValueStartType.Quote; } else if (current == '\"') { startType = ValueStartType.DblQuote; } else { startType = ValueStartType.None; } index++; continue; } if (valueStartFound && (startType == ValueStartType.None) && (current == '\r' || current == '\n' || current == ' ' || current == '\t')) { valueEndPos = index; break; } if (valueStartFound && (startType != ValueStartType.None)) { if (startType == ValueStartType.Quote && current == '\'') { valueEndPos = index; break; } else if ((startType == ValueStartType.DblQuote) && current == '\"') { valueEndPos = index; break; } } index++; }while (continueDo); result.End = valueEndPos; result.Start = valueStartPos; // Remove needless characters result = HtmlTags.CorrectValueIfQuoteExists(ref htmlCode, result); return(result); }
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); }
/// <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)); }