Today's the day

向软件大牛炫耀我会焊单片机,向硬件大牛炫耀我会写 Rails,向软硬件大牛炫耀我生物,向软硬件生物大牛炫耀我会折腾期货 -_-bbb

Rails 2.1 Release
用 File.expand_path 过滤文件名

自写 html 闭合的一个 Bug

galeki posted @ 2008年6月04日 21:22 in RoR , 4225 阅读

前阵子拿自己写的那个 html 闭合函数,洋洋得意的替换掉了 RubyfulSoup 那个大库,结果却没有发现隐藏在其中的一个 Bug。

当初虽然找了不少 html 样本进行测试,最后却忘记测试 self-close 标签存在的情况,多亏了 獨立的圓 帮我找出了这个问题

Bug 就是如果 html 中有 <br/> <img ... /> 这样的标签,闭合函数就会错误的再追加 </br> </img> 这样的标签在简介末尾,要不是导致 獨立的圓 的 Blog 格式混乱,不知道什么时候才能发现这个问题。

知道了问题所在,修正就很简单了,下面就是改善过的版本,不知道还有没有没有考虑到的地方,高亮部分是改动的部分:

  1. def close_html(html)
  2.     stream = html.scan( /<\s*[\/]*[^>^\/]+>/ ).map { |x| x[ /<\s*([^>^\s]* )[^>]*>/, 1]}
  3.     stack = []
  4.     stream.each do |x|
  5.         if x =~ /^\//
  6.             stack.pop if x[1..-1] == stack.last
  7.         else
  8.             stack.push x
  9.         end
  10.     end
  11.     stack.reverse.each {|x| html << "</#{x}>\n"}
  12.     html
  13. end
  • 无匹配
  • 无匹配
Avatar_small
獨立的圓 说:
2008年6月04日 22:05

找bug并除之,是一种乐趣

Avatar_small
galeki 说:
2008年6月04日 22:22

深刻的体会到了~

Anonymous 说:
2023年3月17日 18:55

On that website page, you'll see your description, why not read through this. Orlando Wildlife Removal

Anonymous 说:
2023年3月21日 01:10

I exploit solely premium quality products -- you will observe these individuals on: Tampa Exterminators

anonymous 说:
2023年4月28日 16:58

It is fine, nonetheless evaluate the information and facts around this correct. Free Liker

anonymous 说:
2023年4月28日 17:03

I recommend only good and reliable information, so see it:  Insta Followers Pro

anonymous 说:
2023年5月11日 20:31

I exploit solely premium quality products -- you will observe these individuals on: all inclusive tulum resorts for families

anonymous 说:
2023年5月11日 20:37

You should mainly superior together with well-performing material, which means that see it: all inclusive aruba resorts for families

anonymous 说:
2023年5月11日 20:42

I read this article. I think You put a lot of effort to create this article. I appreciate your work. all inclusive bahamas resorts for families

anonymous 说:
2023年5月19日 23:09

I simply want to tell you that I am new to weblog and definitely liked this blog site. Very likely I’m going to bookmark your blog . You absolutely have wonderful stories. Cheers for sharing with us your blog. Aria Prospect wedding pictures

anonymous 说:
2023年6月04日 15:30

Hmm… I interpret blogs on a analogous issue, however i never visited your blog. I added it to populars also i’ll be your faithful primer. Grand Dunman

anonymous 说:
2023年6月14日 14:09

You should mainly superior together with well-performing material, which means that see it: microdermabrasion

anonymous 说:
2023年6月14日 14:11

It is very good, but look at the information at this address.  The Continuum


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter