Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug that SAX2 parser doesn't expand the predefined entities for "characters" #168

Merged
merged 2 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions lib/rexml/parsers/sax2parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,25 +157,8 @@ def parse
end
end
when :text
#normalized = @parser.normalize( event[1] )
#handle( :characters, normalized )
copy = event[1].clone

esub = proc { |match|
if @entities.has_key?($1)
@entities[$1].gsub(Text::REFERENCE, &esub)
else
match
end
}

copy.gsub!( Text::REFERENCE, &esub )
copy.gsub!( Text::NUMERICENTITY ) {|m|
m=$1
m = "0#{m}" if m[0] == ?x
[Integer(m)].pack('U*')
}
handle( :characters, copy )
unnormalized = @parser.unnormalize( event[1], @entities )
handle( :characters, unnormalized )
when :entitydecl
handle_entitydecl( event )
when :processing_instruction, :comment, :attlistdecl,
Expand Down
4 changes: 2 additions & 2 deletions lib/rexml/parsers/streamparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def parse
@listener.tag_end( event[1] )
@tag_stack.pop
when :text
normalized = @parser.unnormalize( event[1] )
@listener.text( normalized )
unnormalized = @parser.unnormalize( event[1] )
@listener.text( unnormalized )
when :processing_instruction
@listener.instruction( *event[1,2] )
when :start_doctype
Expand Down
16 changes: 16 additions & 0 deletions test/test_pullparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ def test_character_references
assert_equal("B", events['b'])
end

def test_text_entity_references
source = '<root><a>&lt;P&gt; &lt;I&gt; &lt;B&gt; Text &lt;/B&gt; &lt;/I&gt;</a></root>'
parser = REXML::Parsers::PullParser.new( source )

events = []
while parser.has_next?
event = parser.pull
case event.event_type
when :text
events << event[1]
end
end

assert_equal(["<P> <I> <B> Text </B> </I>"], events)
end

def test_text_content_with_line_breaks
source = "<root><a>A</a><b>B\n</b><c>C\r\n</c></root>"
parser = REXML::Parsers::PullParser.new( source )
Expand Down
11 changes: 11 additions & 0 deletions test/test_sax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ def test_entity_replacement
assert_equal '--1234--', results[1]
end

def test_characters_predefined_entities
source = '<root><a>&lt;P&gt; &lt;I&gt; &lt;B&gt; Text &lt;/B&gt; &lt;/I&gt;</a></root>'

sax = Parsers::SAX2Parser.new( source )
results = []
sax.listen(:characters) {|x| results << x }
sax.parse

assert_equal(["<P> <I> <B> Text </B> </I>"], results)
end

def test_sax2
File.open(fixture_path("documentation.xml")) do |f|
parser = Parsers::SAX2Parser.new( f )
Expand Down