Class: HTML5::Filters::WhitespaceFilter

Inherits:
Base
  • Object
show all
Defined in:
lib/html5/filters/whitespace.rb

Constant Summary collapse

SPACE_PRESERVE_ELEMENTS =
%w[pre textarea] + RCDATA_ELEMENTS
SPACES =
/[#{SPACE_CHARACTERS.join('')}]+/m

Instance Method Summary collapse

Instance Method Details

#eachObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/html5/filters/whitespace.rb', line 11

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
      token[:data] = " " if preserve == 0 && token[:data]

    when :Characters
      token[:data] = token[:data].sub(SPACES,' ') if preserve == 0
    end

    yield token
  end
end