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.
This commit is contained in:
Geoff McLane 2016-04-04 18:02:26 +02:00
parent 7598fdfff2
commit 61a0a331fc

View file

@ -17,6 +17,18 @@
#include "tmbstr.h" #include "tmbstr.h"
#include "utf8.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 Block-level and unknown elements are printed on
new lines and their contents indented 2 spaces new lines and their contents indented 2 spaces
@ -640,7 +652,12 @@ static Bool CheckWrapIndent( TidyDocImpl* doc, uint indent )
{ {
WrapLine( doc ); WrapLine( doc );
if ( pprint->indent[ 0 ].spaces < 0 ) 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; pprint->indent[ 0 ].spaces = indent;
}
return yes; return yes;
} }
return no; return no;
@ -705,8 +722,15 @@ void TY_(PFlushLine)( TidyDocImpl* doc, uint indent )
TY_(WriteChar)( '\n', doc->docOut ); TY_(WriteChar)( '\n', doc->docOut );
pprint->line++; pprint->line++;
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; pprint->indent[ 0 ].spaces = indent;
} }
}
static void PCondFlushLine( TidyDocImpl* doc, uint indent ) static void PCondFlushLine( TidyDocImpl* doc, uint indent )
{ {
@ -718,6 +742,14 @@ static void PCondFlushLine( TidyDocImpl* doc, uint indent )
TY_(WriteChar)( '\n', doc->docOut ); TY_(WriteChar)( '\n', doc->docOut );
pprint->line++; 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; pprint->indent[ 0 ].spaces = indent;
} }
} }
@ -741,8 +773,14 @@ void TY_(PFlushLineSmart)( TidyDocImpl* doc, uint indent )
pprint->line++; pprint->line++;
} }
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; pprint->indent[ 0 ].spaces = indent;
} }
}
static void PCondFlushLineSmart( TidyDocImpl* doc, uint indent ) static void PCondFlushLineSmart( TidyDocImpl* doc, uint indent )
{ {
@ -757,7 +795,19 @@ static void PCondFlushLineSmart( TidyDocImpl* doc, uint indent )
TY_(WriteChar)( '\n', doc->docOut ); TY_(WriteChar)( '\n', doc->docOut );
pprint->line++; pprint->line++;
} }
}
/*\
* 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; 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 ( 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; pprint->indent[ 0 ].spaces = indent;
} }
PPrintEndTag( doc, mode, indent, node ); 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 ); 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) if (node->type == TextNode)
{ {
PPrintText( doc, mode, indent, node ); PPrintText( doc, mode, indent, node );
@ -2379,6 +2436,18 @@ void TY_(PPrintTree)( TidyDocImpl* doc, uint mode, uint indent, Node *node )
TY_(PFlushLineSmart)( doc, indent ); TY_(PFlushLineSmart)( doc, indent );
PPrintEndTag( doc, mode, indent, node ); 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) if (!indcont && !hideend && !nodeIsHTML(node) && !classic)