From e2a1f0ec42a3c4b00ac34c52214fe288263fb5e3 Mon Sep 17 00:00:00 2001 From: Moksh Upadhyay Date: Thu, 3 Dec 2026 11:28:51 +0530 Subject: [PATCH] Indent a processing instruction in the Pretty formatter Comments and CDATA sections are indented to the nesting level, but a processing instruction inside an element was written at column 0. --- lib/rexml/formatters/pretty.rb | 5 +++++ test/formatter/test_pretty.rb | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/rexml/formatters/pretty.rb b/lib/rexml/formatters/pretty.rb index ff0e1022..b35260c9 100644 --- a/lib/rexml/formatters/pretty.rb +++ b/lib/rexml/formatters/pretty.rb @@ -103,6 +103,11 @@ def write_cdata( node, output) super end + def write_instruction( node, output ) + output << ' ' * @level + super + end + def write_document( node, output ) # Ok, this is a bit odd. All XML documents have an XML declaration, # but it may not write itself if the user didn't specifically add it, diff --git a/test/formatter/test_pretty.rb b/test/formatter/test_pretty.rb index a3e97d54..a89981fe 100644 --- a/test/formatter/test_pretty.rb +++ b/test/formatter/test_pretty.rb @@ -7,6 +7,14 @@ def format(node, indentation=2) output end + class InstructionTest < self + def test_instruction_is_indented_like_comment + document = REXML::Document.new("") + assert_equal("\n \n \n \n", + format(document)) + end + end + class TextTest < self def test_source_document_is_not_modified document = REXML::Document.new("hello world")