#============================================================================== # ■ htmlcheck.rb ver 0.90 #------------------------------------------------------------------------------ #  開き忘れたり閉じ忘れたりしたタグを無効にします。 # 簡易版なので、要素の破壊は行えません。 #============================================================================== #============================================================================== # ■ CGIsystem #============================================================================== module CGIsystem #-------------------------------------------------------------------------- # ● 使用可能タグ指定 #-------------------------------------------------------------------------- def self.can_use_tag(tag = "") # ここで使えるタグの指定をします。初期状態ではほぼフルに使えます。 base = ["p", "strong", "em", "del", "tt", "span", "div", "pre", "font", "u", "sup", "sub", "h1", "h2", "h3", "h4", "h5", "h6", "ins", "q", "blockquote", "a", "ul", "ol", "dl", "table"] case tag.downcase when "table" return (base + ["th", "tr"]) when "tr" return (base + ["td"]) when "ul", "ol" return (base + ["li"]) when "dl" return (base + ["dt", "dd"]) else return base end end end #============================================================================== # ■ String #============================================================================== class String #-------------------------------------------------------------------------- # ● 欠損タグの破壊 #-------------------------------------------------------------------------- def htmlcheck(tag) @text = self @text.gsub!(/(\r\n|\r|\n)/){"
"} @text.gsub!(/\\/){"\\\\"} if tag @stack = [] @tag = [] @save = [] @tagset = "" @text.gsub!(/([^<]\/)\//im){""} @text.gsub!(/<(br([ \n]+?clear=\"(left|right|all)\")?)>/im){"\000#{$1}\001"} @text.gsub!(/<(nobr)>/im){"\000#{$1}\001"} @text.gsub!(/<(\?php[ \n].+[ \n]\?)>/im){"\000#{$1}\001"} @text.gsub!(/<(img(([ \n][^<>]+?=\"[^<>]+\")+)?)>/im){"\000#{$1}\001"} while @text =~ /.*?<([^\/<>]+?(([ \n].[^<>]+?=\".[^<>]+\")+)? *?|\/[^\/<>]+?>)/im self.stackset end for s in @save @text.sub!(/#{s}\000(([^\/<>]+?)(([ \n][^<>]+?=\"[^<>]+\")+)? *?)\001/im){"#{s}<#{$1}>"} end end @text.gsub!(/&/){"&"} @text.gsub!(/( | ) /i){"#{$1} "} @text.gsub!(//){">"} @text.gsub!(/(\003)/){">"} @text.gsub!(/(\000)/){"<"} @text.gsub!(/(\001)/){">"} return @text end #-------------------------------------------------------------------------- # ● 欠損タグの破壊(ループ部分) #-------------------------------------------------------------------------- def stackset if @text =~ /(<([^\/<>]+)(([ \n][^<>]+?=\"[^<>]+\")+)?.*?>|<\/([^<>]+?)>)/im if $5 != nil if @stack.last == $5.downcase @text.sub!(/(#{@save.last})\000(#{@tag.last})\001(.*?)<\/(#{$5})>/im){"#{$1}\000#{$2}\001#{$3}\000/#{$4}\001"} @tagset = "#{$1}\000#{$2}\001#{$3}\000/#{$4}\001" @save.pop @stack.pop @tag.pop elsif @text =~ /(#{@save.last})\000(#{@tag.last})\001<\/(#{@stack.last})>/im @text.sub!(/(#{$1})\000(#{$2})\001<\/(#{$3})>/im){"#{$1}\002#{$2}\003\002\/#{$3}\003"} else @text.sub!(/(.*?)<\/([^<>]+?)>/im){"#{$1}\002/#{$2}\003"} end else if CGIsystem.can_use_tag("#{@stack.last}").include?($2.downcase) @text.sub!(/(.*?)<(([^\/<>]+?)(([ \n][^<>]+?=\"[^<>]+\")+)?)>/im){"#{$1}\000#{$2}\001"} @save.push($1) @stack.push($3.downcase) @tag.push($2) else @text.sub!(/(.*?)<(([^\/<>]+?)(([ \n][^<>]+?=\"[^<>]+\")+)?)>/im){"#{$1}\002#{$2}\003"} end end else @text.sub!(/(.*?)<((.+?)(([ \n].+?=\".+\")+)?)>/im){"#{$1}\002#{$2}\003"} end end end