asp.net safe intercepts html or ubb strings of specified length

  • 2020-05-07 19:26:37
  • OfStack

When intercepting a string, we need to record whether every tag is closed or not. If there is a tag that is not closed when intercepting a string, we need to close the tag or delete the tag that is not closed. Not consider some does not require a closing tag, start and end tags html always come in pairs, we can traverse the input string, and at the beginning of the label onto the stack, meet with the end tag popup one element from the stack, so traverse to the specified length, left the stack of label need to completion, or delete tags.

The following is the code implementation, if you have a better method please give out:

static char END_SLASH = '/'; 

/// <summary> 
///  Safe truncation string  
/// </summary> 
/// <param name="input"> The input string </param> 
/// <param name="length"> Truncation length </param> 
/// <param name="trimHalfTag">true : truncate the label in half; false : complete half of the label </param> 
/// <param name="tagStartChar"> Tag start character </param> 
/// <param name="tagEndChar"> Tag closing character </param> 
/// <param name="mustCloseTags"> Array of labels that need to be closed </param> 
/// <returns>length Length string </returns> 
public static string SafeTrim(string input, int length, bool trimHalfTag, char tagStartChar, char tagEndChar, string[] mustCloseTags) 
{ 
if (length <= 0) throw new ArgumentException("length  It has to be positive "); 
if (mustCloseTags == null) throw new ArgumentNullException("mustCloseTags"); 

int inputLen = input.Length; 
if (string.IsNullOrEmpty(input) || inputLen <= length) return input; 

string result = string.Empty; 

// The declaration stack is used to hold labels  
Stack<string> tags = new Stack<string>(); 

for (int i = 0; i < length; i++) 
{ 
char c = input[i]; 

if (c == tagStartChar) 
{ 
string tag = string.Empty; 
int tagIndex = i + 1; 
bool isTagEnd = false; 
bool isTagNameEnd = false; 
result += c; 
bool hasMarkTagInStack = false; 
while (tagIndex < inputLen) 
{ 
char tagC = input[tagIndex]; 
result += tagC; 
tagIndex++; 
if (tag == string.Empty && tagC == END_SLASH) 
{ 
isTagEnd = true; 
continue; 
} 
if (!isTagNameEnd) 
{ 
if (char.IsLetter(tagC) || char.IsNumber(tagC)) 
{ 
tag += tagC; 
} 
else 
{ 
isTagNameEnd = true; 
} 
} 

if (!string.IsNullOrEmpty(tag)) 
{ 
if (isTagNameEnd && !hasMarkTagInStack) 
{ 
if (isTagEnd) 
{ 
tags.Pop(); 
} 
else 
{ 
tags.Push(tag); 
} 
hasMarkTagInStack = true; 
} 
} 

if (isTagNameEnd) 
{ 
if (tagC == tagEndChar) 
{ 
i = tagIndex - 1; 
break; 
} 
} 

} 
} 
else 
{ 
result += c; 
} 
} 

while (tags.Count > 0) 
{ 
string tag = tags.Pop(); 

bool isMustCloseTag = false; 
foreach (string mustCloseTag in mustCloseTags) 
{ 
if (string.Compare(mustCloseTag, tag, true) == 0) 
{ 
isMustCloseTag = true; 
break; 
} 
} 
if (isMustCloseTag) 
{ 
if (trimHalfTag) 
{ 
int lastTagIndex = result.LastIndexOf(tagStartChar.ToString() + tag, StringComparison.CurrentCultureIgnoreCase); 

result = result.Substring(0, lastTagIndex); 
} 
else 
{ 
result += (tagStartChar.ToString() + END_SLASH + tag + tagEndChar); 
} 
} 
} 

return result; 
}

Please keep the link to   jade technology blog

Related articles: