diff --git a/lib/oga/lexer.rl b/lib/oga/lexer.rl index 899776e..b761170 100644 --- a/lib/oga/lexer.rl +++ b/lib/oga/lexer.rl @@ -73,7 +73,7 @@ module Oga def add_token(type, value) token = [type, value, @line, @column] - advance_column(value.length) + advance_column(value.length) if value @tokens << token end @@ -125,12 +125,10 @@ module Oga } action string_dquote { - advance_column fcall string_dquote; } action string_squote { - advance_column fcall string_squote; } @@ -138,6 +136,7 @@ module Oga ^dquote => buffer_string; dquote => { emit_string_buffer + advance_column fret; }; *|; @@ -146,6 +145,7 @@ module Oga ^squote => buffer_string; squote => { emit_string_buffer + advance_column fret; }; *|; @@ -236,7 +236,7 @@ module Oga # First emit the token, then advance the column. This way the column # number points to the < and not the "p" in

. action open_element { - t(:T_ELEM_OPEN, p) + t(:T_ELEM_OPEN, @ts + 1) advance_column @@ -268,10 +268,11 @@ module Oga element_name %{ t(:T_ATTR, @ts, p) + advance_column } '=' (dquote @string_dquote | squote @string_squote); - # Non self-closing tags. + # Non self-closing elements. '' => { + advance_column + add_token(:T_ELEM_CLOSE, nil) + fret; + }; *|; main := |* diff --git a/spec/oga/lexer/doctype_spec.rb b/spec/oga/lexer/doctype_spec.rb index 67ea0bb..dad3ea6 100644 --- a/spec/oga/lexer/doctype_spec.rb +++ b/spec/oga/lexer/doctype_spec.rb @@ -13,8 +13,8 @@ describe Oga::Lexer do lex('').should == [ [:T_DOCTYPE_START, '', 1, 37] ] end @@ -23,8 +23,8 @@ describe Oga::Lexer do lex("").should == [ [:T_DOCTYPE_START, '', 1, 37] ] end diff --git a/spec/oga/lexer/elements_spec.rb b/spec/oga/lexer/elements_spec.rb index 5908a25..77e6b2e 100644 --- a/spec/oga/lexer/elements_spec.rb +++ b/spec/oga/lexer/elements_spec.rb @@ -28,8 +28,8 @@ describe Oga::Lexer do [:T_ELEM_OPEN, 'p', 1, 1], [:T_ATTR, 'class', 1, 4], [:T_STRING, 'foo', 1, 10], - [:T_TEXT, 'Hello', 1, 15], - [:T_ELEM_CLOSE, 'p', 1, 20] + [:T_TEXT, 'Hello', 1, 16], + [:T_ELEM_CLOSE, 'p', 1, 21] ] end end @@ -56,4 +56,22 @@ describe Oga::Lexer do ] end end + + context 'void elements' do + example 'lex a void element' do + lex('
').should == [ + [:T_ELEM_OPEN, 'br', 1, 1], + [:T_ELEM_CLOSE, nil, 1, 6] + ] + end + + example 'lex a void element with an attribute' do + lex('
').should == [ + [:T_ELEM_OPEN, 'br', 1, 1], + [:T_ATTR, 'class', 1, 5], + [:T_STRING, 'foo', 1, 11], + [:T_ELEM_CLOSE, nil, 1, 18] + ] + end + end end