Lex/parser XML declaration attributes.

This commit is contained in:
Yorick Peterse 2014-04-02 22:01:17 +02:00
parent fa2e71c790
commit d6c0a1f3f3
4 changed files with 14 additions and 6 deletions

View File

@ -229,6 +229,7 @@ module Oga
newline = '\n' | '\r\n';
whitespace = [ \t];
identifier = [a-zA-Z0-9\-_]+;
# Strings
#
@ -403,6 +404,12 @@ module Oga
fret;
};
# Attributes and their values (e.g. version="1.0").
identifier => { emit(:T_ATTR) };
dquote => start_string_dquote;
squote => start_string_squote;
any;
*|;
@ -450,7 +457,7 @@ module Oga
newline => { advance_line };
# Attribute names.
element_name => { emit(:T_ATTR) };
identifier => { emit(:T_ATTR) };
# Attribute values.
dquote => start_string_dquote;

View File

@ -123,8 +123,8 @@ rule
# XML declarations
xmldecl
: T_XML_DECL_START T_XML_DECL_END { s(:xml_decl) }
| T_XML_DECL_START text T_XML_DECL_END { s(:xml_decl, val[1]) }
: T_XML_DECL_START T_XML_DECL_END { s(:xml_decl) }
| T_XML_DECL_START attributes T_XML_DECL_END { s(:xml_decl, val[1]) }
# Plain text

View File

@ -16,7 +16,8 @@ describe Oga::XML::Lexer do
example 'lex a tag with text inside it' do
lex('<?xml version="1.0" ?>').should == [
[:T_XML_DECL_START, nil, 1],
[:T_TEXT, ' version="1.0" ', 1],
[:T_ATTR, 'version', 1],
[:T_STRING, '1.0', 1],
[:T_XML_DECL_END, nil, 1]
]
end

View File

@ -3,9 +3,9 @@ require 'spec_helper'
describe Oga::XML::Parser do
context 'XML declaration tags' do
example 'lex an XML declaration tag' do
parse('<?xml hello ?>').should == s(
parse('<?xml version="1.0" ?>').should == s(
:document,
s(:xml_decl, s(:text, ' hello '))
s(:xml_decl, s(:attributes, s(:attribute, 'version', '1.0')))
)
end
end