2007-07-05 00:36:59 +02:00
|
|
|
require 'html5/constants'
|
|
|
|
require 'html5/filters/base'
|
2007-06-14 06:05:15 +02:00
|
|
|
|
2007-07-05 00:36:59 +02:00
|
|
|
module HTML5
|
2007-06-14 06:05:15 +02:00
|
|
|
module Filters
|
|
|
|
class WhitespaceFilter < Base
|
|
|
|
|
|
|
|
SPACE_PRESERVE_ELEMENTS = %w[pre textarea] + RCDATA_ELEMENTS
|
|
|
|
SPACES = /[#{SPACE_CHARACTERS.join('')}]+/m
|
|
|
|
|
|
|
|
def each
|
|
|
|
preserve = 0
|
|
|
|
__getobj__.each do |token|
|
|
|
|
case token[:type]
|
|
|
|
when :StartTag
|
|
|
|
if preserve > 0 or SPACE_PRESERVE_ELEMENTS.include?(token[:name])
|
|
|
|
preserve += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
when :EndTag
|
|
|
|
preserve -= 1 if preserve > 0
|
|
|
|
|
|
|
|
when :SpaceCharacters
|
2007-08-30 19:19:10 +02:00
|
|
|
token[:data] = " " if preserve == 0 && token[:data]
|
2007-06-14 06:05:15 +02:00
|
|
|
|
|
|
|
when :Characters
|
|
|
|
token[:data] = token[:data].sub(SPACES,' ') if preserve == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
yield token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|