From d6c0a1f3f3a49a2b116861721d041e831c608271 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 2 Apr 2014 22:01:17 +0200 Subject: [PATCH] Lex/parser XML declaration attributes. --- lib/oga/xml/lexer.rl | 9 ++++++++- lib/oga/xml/parser.y | 4 ++-- spec/oga/xml/lexer/xml_declaration_spec.rb | 3 ++- spec/oga/xml/parser/xml_declaration_spec.rb | 4 ++-- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/oga/xml/lexer.rl b/lib/oga/xml/lexer.rl index d7bc011..765e235 100644 --- a/lib/oga/xml/lexer.rl +++ b/lib/oga/xml/lexer.rl @@ -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; diff --git a/lib/oga/xml/parser.y b/lib/oga/xml/parser.y index ac90cc8..9a1aec8 100644 --- a/lib/oga/xml/parser.y +++ b/lib/oga/xml/parser.y @@ -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 diff --git a/spec/oga/xml/lexer/xml_declaration_spec.rb b/spec/oga/xml/lexer/xml_declaration_spec.rb index eb3de1f..35a6a86 100644 --- a/spec/oga/xml/lexer/xml_declaration_spec.rb +++ b/spec/oga/xml/lexer/xml_declaration_spec.rb @@ -16,7 +16,8 @@ describe Oga::XML::Lexer do example 'lex a tag with text inside it' do lex('').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 diff --git a/spec/oga/xml/parser/xml_declaration_spec.rb b/spec/oga/xml/parser/xml_declaration_spec.rb index 3096a5d..ba6a17b 100644 --- a/spec/oga/xml/parser/xml_declaration_spec.rb +++ b/spec/oga/xml/parser/xml_declaration_spec.rb @@ -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('').should == s( + parse('').should == s( :document, - s(:xml_decl, s(:text, ' hello ')) + s(:xml_decl, s(:attributes, s(:attribute, 'version', '1.0'))) ) end end