Fixed advancing column numbers.

In a bunch of cases the column number would not be increased correctly.
This commit is contained in:
Yorick Peterse 2014-03-07 23:54:56 +01:00
parent 1c9a6c8b76
commit cd53d5e426
3 changed files with 37 additions and 11 deletions

View File

@ -73,7 +73,7 @@ module Oga
def add_token(type, value) def add_token(type, value)
token = [type, value, @line, @column] token = [type, value, @line, @column]
advance_column(value.length) advance_column(value.length) if value
@tokens << token @tokens << token
end end
@ -125,12 +125,10 @@ module Oga
} }
action string_dquote { action string_dquote {
advance_column
fcall string_dquote; fcall string_dquote;
} }
action string_squote { action string_squote {
advance_column
fcall string_squote; fcall string_squote;
} }
@ -138,6 +136,7 @@ module Oga
^dquote => buffer_string; ^dquote => buffer_string;
dquote => { dquote => {
emit_string_buffer emit_string_buffer
advance_column
fret; fret;
}; };
*|; *|;
@ -146,6 +145,7 @@ module Oga
^squote => buffer_string; ^squote => buffer_string;
squote => { squote => {
emit_string_buffer emit_string_buffer
advance_column
fret; fret;
}; };
*|; *|;
@ -236,7 +236,7 @@ module Oga
# First emit the token, then advance the column. This way the column # First emit the token, then advance the column. This way the column
# number points to the < and not the "p" in <p>. # number points to the < and not the "p" in <p>.
action open_element { action open_element {
t(:T_ELEM_OPEN, p) t(:T_ELEM_OPEN, @ts + 1)
advance_column advance_column
@ -268,10 +268,11 @@ module Oga
element_name element_name
%{ %{
t(:T_ATTR, @ts, p) t(:T_ATTR, @ts, p)
advance_column
} }
'=' (dquote @string_dquote | squote @string_squote); '=' (dquote @string_dquote | squote @string_squote);
# Non self-closing tags. # Non self-closing elements.
'</' element_name { '</' element_name {
emit_text_buffer emit_text_buffer
t(:T_ELEM_CLOSE, p) t(:T_ELEM_CLOSE, p)
@ -282,6 +283,13 @@ module Oga
advance_column(2) advance_column(2)
fret; fret;
}; };
# self-closing / void elements.
'/>' => {
advance_column
add_token(:T_ELEM_CLOSE, nil)
fret;
};
*|; *|;
main := |* main := |*

View File

@ -13,8 +13,8 @@ describe Oga::Lexer do
lex('<!DOCTYPE HTML PUBLIC "foobar" "baz">').should == [ lex('<!DOCTYPE HTML PUBLIC "foobar" "baz">').should == [
[:T_DOCTYPE_START, '<!DOCTYPE HTML', 1, 1], [:T_DOCTYPE_START, '<!DOCTYPE HTML', 1, 1],
[:T_DOCTYPE_TYPE, 'PUBLIC', 1, 16], [:T_DOCTYPE_TYPE, 'PUBLIC', 1, 16],
[:T_STRING, 'foobar', 1, 24], [:T_STRING, 'foobar', 1, 23],
[:T_STRING, 'baz', 1, 33], [:T_STRING, 'baz', 1, 32],
[:T_DOCTYPE_END, '>', 1, 37] [:T_DOCTYPE_END, '>', 1, 37]
] ]
end end
@ -23,8 +23,8 @@ describe Oga::Lexer do
lex("<!DOCTYPE HTML PUBLIC 'foobar' 'baz'>").should == [ lex("<!DOCTYPE HTML PUBLIC 'foobar' 'baz'>").should == [
[:T_DOCTYPE_START, '<!DOCTYPE HTML', 1, 1], [:T_DOCTYPE_START, '<!DOCTYPE HTML', 1, 1],
[:T_DOCTYPE_TYPE, 'PUBLIC', 1, 16], [:T_DOCTYPE_TYPE, 'PUBLIC', 1, 16],
[:T_STRING, 'foobar', 1, 24], [:T_STRING, 'foobar', 1, 23],
[:T_STRING, 'baz', 1, 33], [:T_STRING, 'baz', 1, 32],
[:T_DOCTYPE_END, '>', 1, 37] [:T_DOCTYPE_END, '>', 1, 37]
] ]
end end

View File

@ -28,8 +28,8 @@ describe Oga::Lexer do
[:T_ELEM_OPEN, 'p', 1, 1], [:T_ELEM_OPEN, 'p', 1, 1],
[:T_ATTR, 'class', 1, 4], [:T_ATTR, 'class', 1, 4],
[:T_STRING, 'foo', 1, 10], [:T_STRING, 'foo', 1, 10],
[:T_TEXT, 'Hello', 1, 15], [:T_TEXT, 'Hello', 1, 16],
[:T_ELEM_CLOSE, 'p', 1, 20] [:T_ELEM_CLOSE, 'p', 1, 21]
] ]
end end
end end
@ -56,4 +56,22 @@ describe Oga::Lexer do
] ]
end end
end end
context 'void elements' do
example 'lex a void element' do
lex('<br />').should == [
[:T_ELEM_OPEN, 'br', 1, 1],
[:T_ELEM_CLOSE, nil, 1, 6]
]
end
example 'lex a void element with an attribute' do
lex('<br class="foo" />').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 end