From ed9d8c05a2637b8fa83674da14582997ebf990e0 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Wed, 12 Mar 2014 22:20:12 +0100 Subject: [PATCH] Added support for parsing comments. --- lib/oga/parser.y | 12 ++++++++++++ spec/oga/parser/comments_spec.rb | 13 +++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 spec/oga/parser/comments_spec.rb diff --git a/lib/oga/parser.y b/lib/oga/parser.y index e67623d..e0ccd0e 100644 --- a/lib/oga/parser.y +++ b/lib/oga/parser.y @@ -4,6 +4,7 @@ token T_NEWLINE T_SPACE token T_STRING T_TEXT token T_DOCTYPE_START T_DOCTYPE_END T_DOCTYPE_TYPE token T_CDATA_START T_CDATA_END +token T_COMMENT_START T_COMMENT_END options no_result_var @@ -21,6 +22,7 @@ rule expression : doctype | cdata + | comment ; # Doctypes @@ -58,6 +60,16 @@ rule | T_CDATA_START T_TEXT T_CDATA_END { s(:cdata, val[1]) } ; + # Comments + + comment + # + : T_COMMENT_START T_COMMENT_END { s(:comment) } + + # + | T_COMMENT_START T_TEXT T_COMMENT_END { s(:comment, val[1]) } + ; + whitespaces : whitespaces whitespace | whitespace diff --git a/spec/oga/parser/comments_spec.rb b/spec/oga/parser/comments_spec.rb new file mode 100644 index 0000000..034da7f --- /dev/null +++ b/spec/oga/parser/comments_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' + +describe Oga::Parser do + context 'comments' do + example 'parse an empty comment' do + parse_html('').should == s(:document, s(:comment)) + end + + example 'parse a comment' do + parse_html('').should == s(:document, s(:comment, 'foo')) + end + end +end