From 61a0a331fc783898aa2dab90627b8437042efe47 Mon Sep 17 00:00:00 2001 From: Geoff McLane Date: Mon, 4 Apr 2016 18:02:26 +0200 Subject: [PATCH] Issue #390 - fix indent with --hide-endtags yes. The problem was, with --hide-endtags yes, a conditional pprint buffer flush had nothing to flush, thus the indent was not adjusted. To track down this bug added a lot of MSVC Debug code, but is only existing if some additional items defined, so has no effect on the release code. This, what feels like a good fix, was first reported about 12 years ago by @OlafvdSpek in SF Bugs 563. Hopefully finally closed. --- src/pprint.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 3 deletions(-) diff --git a/src/pprint.c b/src/pprint.c index 206f234..a4f2aef 100644 --- a/src/pprint.c +++ b/src/pprint.c @@ -17,6 +17,18 @@ #include "tmbstr.h" #include "utf8.h" +/* *** FOR DEBUG ONLY *** */ +#if !defined(NDEBUG) && defined(_MSC_VER) +/* #define DEBUG_PPRINT */ +/* #define DEBUG_INDENT */ +#ifdef DEBUG_PPRINT +extern void dbg_show_node( TidyDocImpl* doc, Node *node, int caller, int indent ); +#endif +#ifdef DEBUG_INDENT +#include "sprtf.h" +#endif +#endif + /* Block-level and unknown elements are printed on new lines and their contents indented 2 spaces @@ -640,7 +652,12 @@ static Bool CheckWrapIndent( TidyDocImpl* doc, uint indent ) { WrapLine( doc ); if ( pprint->indent[ 0 ].spaces < 0 ) + { +#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_INDENT) + SPRTF("%s Indent from %d to %d\n", __FUNCTION__, pprint->indent[ 0 ].spaces, indent ); +#endif pprint->indent[ 0 ].spaces = indent; + } return yes; } return no; @@ -705,7 +722,14 @@ void TY_(PFlushLine)( TidyDocImpl* doc, uint indent ) TY_(WriteChar)( '\n', doc->docOut ); pprint->line++; - pprint->indent[ 0 ].spaces = indent; + + if (pprint->indent[ 0 ].spaces != (int)indent ) + { +#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_INDENT) + SPRTF("%s Indent from %d to %d\n", __FUNCTION__, pprint->indent[ 0 ].spaces, indent ); +#endif + pprint->indent[ 0 ].spaces = indent; + } } static void PCondFlushLine( TidyDocImpl* doc, uint indent ) @@ -718,6 +742,14 @@ static void PCondFlushLine( TidyDocImpl* doc, uint indent ) TY_(WriteChar)( '\n', doc->docOut ); pprint->line++; + } + + /* Issue #390 - Whether chars to flush or not, set new indent */ + if ( pprint->indent[ 0 ].spaces != (int)indent ) + { +#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_INDENT) + SPRTF("%s Indent from %d to %d\n", __FUNCTION__, pprint->indent[ 0 ].spaces, indent ); +#endif pprint->indent[ 0 ].spaces = indent; } } @@ -741,7 +773,13 @@ void TY_(PFlushLineSmart)( TidyDocImpl* doc, uint indent ) pprint->line++; } - pprint->indent[ 0 ].spaces = indent; + if ( pprint->indent[ 0 ].spaces != (int)indent ) + { +#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_INDENT) + SPRTF("%s Indent from %d to %d\n", __FUNCTION__, pprint->indent[ 0 ].spaces, indent ); +#endif + pprint->indent[ 0 ].spaces = indent; + } } static void PCondFlushLineSmart( TidyDocImpl* doc, uint indent ) @@ -757,8 +795,20 @@ static void PCondFlushLineSmart( TidyDocImpl* doc, uint indent ) TY_(WriteChar)( '\n', doc->docOut ); pprint->line++; } + } - pprint->indent[ 0 ].spaces = indent; + /*\ + * Issue #390 - Must still deal with fixing indent! + * If TidyHideEndTags or TidyOmitOptionalTags, then + * in certain circumstance no PrintEndTag will be done, + * so linelen will be 0... + \*/ + if (pprint->indent[ 0 ].spaces != (int)indent) + { +#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_INDENT) + SPRTF("%s Indent from %d to %d\n", __FUNCTION__, pprint->indent[ 0 ].spaces, indent ); +#endif + pprint->indent[ 0 ].spaces = indent; } } @@ -2026,6 +2076,9 @@ void PPrintScriptStyle( TidyDocImpl* doc, uint mode, uint indent, Node *node ) if ( node->content && pprint->indent[ 0 ].spaces != (int)indent ) { +#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_INDENT) + SPRTF("%s Indent from %d to %d\n", __FUNCTION__, pprint->indent[ 0 ].spaces, indent ); +#endif pprint->indent[ 0 ].spaces = indent; } PPrintEndTag( doc, mode, indent, node ); @@ -2131,6 +2184,10 @@ void TY_(PPrintTree)( TidyDocImpl* doc, uint mode, uint indent, Node *node ) doc->progressCallback( tidyImplToDoc(doc), node->line, node->column, doc->pprint.line + 1 ); } +#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_PPRINT) + dbg_show_node( doc, node, 4, GetSpaces( &doc->pprint ) ); +#endif + if (node->type == TextNode) { PPrintText( doc, mode, indent, node ); @@ -2379,6 +2436,18 @@ void TY_(PPrintTree)( TidyDocImpl* doc, uint mode, uint indent, Node *node ) TY_(PFlushLineSmart)( doc, indent ); PPrintEndTag( doc, mode, indent, node ); } + else if (hideend) + { + /* Issue #390 - must still deal with adjusting indent */ + TidyPrintImpl* pprint = &doc->pprint; + if (pprint->indent[ 0 ].spaces != (int)indent) + { +#if !defined(NDEBUG) && defined(_MSC_VER) && defined(DEBUG_INDENT) + SPRTF("%s Indent from %d to %d\n", __FUNCTION__, pprint->indent[ 0 ].spaces, indent ); +#endif + pprint->indent[ 0 ].spaces = indent; + } + } } if (!indcont && !hideend && !nodeIsHTML(node) && !classic)