From ed3cbe7975eeb9d142c4f649334038b6389abc0e Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Thu, 23 Jul 2015 12:40:32 +0100 Subject: [PATCH] Fixes #129 - lexing superfluous end tags. Prevents a superfluous end tag of a self-closing HTML tag from closing its parent element prematurely, for example: ```html ``` (note is self closing) being turned into: ```html ``` --- lib/oga/xml/lexer.rb | 5 ++++- spec/oga/html/lexer/closing_mismatch_spec.rb | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/oga/xml/lexer.rb b/lib/oga/xml/lexer.rb index 001a1f5..8ffc830 100644 --- a/lib/oga/xml/lexer.rb +++ b/lib/oga/xml/lexer.rb @@ -512,8 +512,11 @@ module Oga end end - add_token(:T_ELEM_END) + # Prevents a superfluous end tag of a self-closing HTML tag from + # closing its parent element prematurely + return if html? && name && name != current_element + add_token(:T_ELEM_END) @elements.pop end diff --git a/spec/oga/html/lexer/closing_mismatch_spec.rb b/spec/oga/html/lexer/closing_mismatch_spec.rb index f177745..5cabc4e 100644 --- a/spec/oga/html/lexer/closing_mismatch_spec.rb +++ b/spec/oga/html/lexer/closing_mismatch_spec.rb @@ -9,5 +9,16 @@ describe Oga::XML::Lexer do [:T_ELEM_END, nil, 1] ] end + + it 'lexes a element' do + lex_html('').should == [ + [:T_ELEM_NAME, 'object', 1], + [:T_ELEM_NAME, 'param', 1], + [:T_ELEM_END, nil, 1], + [:T_ELEM_NAME, 'param', 1], + [:T_ELEM_END, nil, 1], + [:T_ELEM_END, nil, 1] + ] + end end end