Support for Enumerator inputs in the XML lexer.

This fixes #48.
This commit is contained in:
Yorick Peterse committed 2014-09-28 22:27:30 +02:00
1 parent 27ee23eb31
commit 0299ff1ea4
3 files changed
+45 -22

No files matched your search

+11 -18
View File
@@ -66,7 +66,7 @@ module Oga
@line = 1
@elements = []
@data.rewind if io_input?
@data.rewind if @data.respond_to?(:rewind)
reset_native
end
@@ -78,25 +78,18 @@ module Oga
# @yieldparam [String]
#
def read_data
# We can't check for #each_line since String also defines that. Using
# String#each_line has no benefit over just lexing the String in one
# go.
if io_input?
@data.each_line do |line|
yield line
end
else
if @data.is_a?(String)
yield @data
end
end
##
# Returns `true` if the input is an IO like object, false otherwise.
#
# @return [TrueClass|FalseClass]
#
def io_input?
return @data.is_a?(IO) || @data.is_a?(StringIO)
# IO, StringIO, etc
# THINK: read(N) would be nice, but currently this screws up the C code
elsif @data.respond_to?(:each_line)
@data.each_line { |line| yield line }
# Enumerator, Array, etc
elsif @data.respond_to?(:each)
@data.each { |chunk| yield chunk }
end
end
##