From 9b8e9f49c66b0ac0bf8092ce02819bb680dea141 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 3 Sep 2014 23:10:50 +0200 Subject: [PATCH] Support for lexing empty attribute values. This ensures that Oga can lex the following properly: Previously Ragel would stop upon finding the empty string. This was caused due to the string rules being declared as following: string_dquote = (dquote ^dquote+ dquote); string_squote = (squote ^squote+ squote); These rules only match strings _with_ content, not without. Since Ragel stops consuming input the moment it finds unhandled data this resulted in incorrect tokens being emitted. --- ext/ragel/base_lexer.rl | 4 ++-- spec/oga/xml/lexer/elements_spec.rb | 10 ++++++++++ spec/oga/xml/parser/elements_spec.rb | 28 ++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/ext/ragel/base_lexer.rl b/ext/ragel/base_lexer.rl index 21f75ad..4b7a4b1 100644 --- a/ext/ragel/base_lexer.rl +++ b/ext/ragel/base_lexer.rl @@ -116,8 +116,8 @@ dquote = '"'; squote = "'"; - string_dquote = (dquote ^dquote+ dquote); - string_squote = (squote ^squote+ squote); + string_dquote = (dquote ^dquote* dquote); + string_squote = (squote ^squote* squote); string = string_dquote | string_squote; diff --git a/spec/oga/xml/lexer/elements_spec.rb b/spec/oga/xml/lexer/elements_spec.rb index 97054ab..3455e2b 100644 --- a/spec/oga/xml/lexer/elements_spec.rb +++ b/spec/oga/xml/lexer/elements_spec.rb @@ -53,6 +53,16 @@ describe Oga::XML::Lexer do ] end + example 'lex an element with an attribute with an empty value' do + lex('

').should == [ + [:T_ELEM_START, nil, 1], + [:T_ELEM_NAME, 'p', 1], + [:T_ATTR, 'foo', 1], + [:T_STRING, '', 1], + [:T_ELEM_END, nil, 1] + ] + end + example 'lex a paragraph element with attributes' do lex('

Hello

').should == [ [:T_ELEM_START, nil, 1], diff --git a/spec/oga/xml/parser/elements_spec.rb b/spec/oga/xml/parser/elements_spec.rb index 4488594..afe317f 100644 --- a/spec/oga/xml/parser/elements_spec.rb +++ b/spec/oga/xml/parser/elements_spec.rb @@ -55,6 +55,34 @@ describe Oga::XML::Parser do end end + context 'elements with attributes without values' do + before :all do + @element = parse('').children[0] + end + + example 'return an Element instance' do + @element.is_a?(Oga::XML::Element).should == true + end + + example 'set the bar attribute' do + @element.attribute('bar').value.should be_nil + end + end + + context 'elements with attributes with empty values' do + before :all do + @element = parse('').children[0] + end + + example 'return an Element instance' do + @element.is_a?(Oga::XML::Element).should == true + end + + example 'set the bar attribute' do + @element.attribute('bar').value.should == '' + end + end + context 'elements with namespaced attributes' do before :all do @element = parse('').children[0]