Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
More Jobs
Recent
Closure scoping issue
Parse HTTP Accept headers
Int32 Quick Parser
Find the file with the highest mtime in a directory
While loop to get unique value
ruby metaprogramming
includes_all?
Expanding input list using jQuery
Streamline attaching onPaste behavior to fields
Which is more readable? Math.* or if (...)
Popular
Int32 Quick Parser
ruby metaprogramming
includes_all?
Find the file with the highest mtime in a directory
Parse HTTP Accept headers
Closure scoping issue
While loop to get unique value
Command Pattern from Head First Design Patterns book.
Which is more readable? Math.* or if (...)
Streamline attaching onPaste behavior to fields
Pastable version of
Balance HTML Tags
<div style="overflow:auto;border:solid 1px #ccc;background:#000;color:#F8F8F8"> <div class="section"> <pre style="float:left;margin:0 10px;border-right:0;color:#666;">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71</pre> <pre class="sunburst"><span style="color:#E28964;">private</span> <span style="color:#E28964;">static</span> Regex _namedtags = <span style="color:#E28964;">new</span> Regex (@<span style="color:#65B042;"><span style="color:#65B042;">"</span></?(?<tagname><span style="color:#DDF2A4;">\w</span>+)[^>]*(<span style="color:#DDF2A4;">\s</span>|$|>)<span style="color:#65B042;">"</span></span>, RegexOptions.Singleline | RegexOptions.ExplicitCapture); <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">//</span>/ <summary></span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">//</span>/ attempt to balance HTML tags in the html string</span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">//</span>/ by removing any unmatched opening or closing tags</span> <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">//</span>/ </summary></span> <span style="color:#E28964;">public</span> <span style="color:#E28964;">static</span> <span style="color:#99CF50;">string</span> BalanceTags(<span style="color:#99CF50;">string</span> html) { var tagname = <span style="color:#65B042;"><span style="color:#65B042;">"</span><span style="color:#65B042;">"</span></span>; var tag = <span style="color:#65B042;"><span style="color:#65B042;">"</span><span style="color:#65B042;">"</span></span>; var ignoredtags = <span style="color:#65B042;"><span style="color:#65B042;">"</span><p<img<br<li<span style="color:#65B042;">"</span></span>; var match = <span style="color:#3387CC;">0</span>; MatchCollection tags = _namedtags.Matches(html); <span style="color:#99CF50;">bool</span>[] tagpaired = <span style="color:#E28964;">new</span> <span style="color:#99CF50;">bool</span>[tags.Count]; <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">//</span> loop through matched tags in reverse order</span> <span style="color:#E28964;">for</span> (<span style="color:#99CF50;">int</span> i = tags.Count - <span style="color:#3387CC;">1</span>; i > -<span style="color:#3387CC;">1</span>; i--) { tagname = tags[i].Groups[<span style="color:#65B042;"><span style="color:#65B042;">"</span>tagname<span style="color:#65B042;">"</span></span>].Value.ToLower(); tag = tags[i].Value; match = -<span style="color:#3387CC;">1</span>; <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">//</span> skip any tags in our ignore list; assume they're self-closed</span> <span style="color:#E28964;">if</span> (!tagpaired[i] && !ignoredtags.Contains(<span style="color:#65B042;"><span style="color:#65B042;">"</span><<span style="color:#65B042;">"</span></span> + tagname)) { <span style="color:#E28964;">if</span> (tag.StartsWith(<span style="color:#65B042;"><span style="color:#65B042;">"</span></<span style="color:#65B042;">"</span></span>)) { <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">//</span> if searching backwards, look for opening tags</span> <span style="color:#E28964;">for</span> (<span style="color:#99CF50;">int</span> j = i - <span style="color:#3387CC;">1</span>; j > -<span style="color:#3387CC;">1</span>; j--) { <span style="color:#E28964;">if</span> (!tagpaired[j] && tags[j].Value.ToLower().StartsWith(<span style="color:#65B042;"><span style="color:#65B042;">"</span><<span style="color:#65B042;">"</span></span> + tagname)) { match = j; <span style="color:#E28964;">break</span>; } } } <span style="color:#E28964;">else</span> { <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">//</span> if searching forwards, look for closing tags</span> <span style="color:#E28964;">for</span> (<span style="color:#99CF50;">int</span> j = i + <span style="color:#3387CC;">1</span>; j < tags.Count; j++) { <span style="color:#E28964;">if</span> (!tagpaired[j] && tags[j].Value.ToLower().StartsWith(<span style="color:#65B042;"><span style="color:#65B042;">"</span></<span style="color:#65B042;">"</span></span> + tagname)) { match = j; <span style="color:#E28964;">break</span>; } } } <span style="color:#E28964;">if</span> (match > -<span style="color:#3387CC;">1</span>) { <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">//</span> found matching opening/closing tag</span> tagpaired[match] = <span style="color:#E28964;">true</span>; tagpaired[i] = <span style="color:#E28964;">true</span>; } <span style="color:#E28964;">else</span> { <span style="color:#AEAEAE;font-style:italic;"><span style="color:#AEAEAE;font-style:italic;">//</span> no matching opening/closing tag found -- remove this tag!</span> html = html.Remove(tags[i].Index, tags[i].Length); tagpaired[i] = <span style="color:#E28964;">true</span>; System.Diagnostics.Debug.WriteLine(<span style="color:#65B042;"><span style="color:#65B042;">"</span>unbalanced tag removed: <span style="color:#65B042;">"</span></span> + tags[i]); } } } <span style="color:#E28964;">return</span> html; } </pre> </div> </div> <a href="http://refactormycode.com/codes/360-balance-html-tags" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://refactormycode.com/images/small_logo.gif" style="border:0" /></a>