Skip to content

Commit ef3157e

Browse files
committed
Fix issues with user-specified settings changing
User-specified settings were being fiddled with by tidy internally. User settings set by the user should always be able to be read back by the user, but `AdjustConfig()` would change them. This change contributes toward fixing the situation by using `AdjustConfig()` at the only point that it's needed, as well by NOT automatically reverting to the snapshot after outputting a buffer (which should be stateless, but caused PHP to break because it tried to save buffer more than once, so subsequent calls would use non-fiddle settings.).
1 parent 25fbb0f commit ef3157e

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

include/tidy.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -548,10 +548,10 @@ TIDY_EXPORT int TIDY_CALL tidySetOutCharEncoding(TidyDoc tdoc, /**< The
548548
** @note In general, you should expect that options you set should stay set.
549549
** This isn't always the case, though, because Tidy will adjust options
550550
** for internal use during the lexing, parsing, cleaning, and printing
551-
** phases, but will restore them after the printing process. If you
552-
** require access to user configuration values at any time between the
553-
** tidyParseXXX() process and the tidySaveXXX() process, make sure to
554-
** keep your own copy.
551+
** phases. If you require access to user configuration values at any
552+
** time after the tidyParseXXX() process, make sure to keep your own
553+
** copy, or use tidyOptResetToSnapshot() when you no longer need to
554+
** use any other tidy functions.
555555
** @{
556556
******************************************************************************/
557557

src/config.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@ static const struct {
304304

305305

306306
/* forward declarations */
307-
static void AdjustConfig( TidyDocImpl* doc );
308307
static Bool GetPickListValue( ctmbstr value, PickListItems* pickList, uint *result );
309308

310309

@@ -713,7 +712,11 @@ void TY_(TakeConfigSnapshot)( TidyDocImpl* doc )
713712
const TidyOptionValue* value = &doc->config.value[ 0 ];
714713
TidyOptionValue* snap = &doc->config.snapshot[ 0 ];
715714

716-
AdjustConfig( doc ); /* Make sure it's consistent */
715+
/* @jsd: do NOT mess with user-specified settings until
716+
* absolutely necessary, and ensure that we can
717+
* can restore them immediately after the need.
718+
*/
719+
// TY_(AdjustConfig)( doc ); /* Make sure it's consistent */
717720
for ( ixVal=0; ixVal < N_TIDY_OPTIONS; ++option, ++ixVal )
718721
{
719722
assert( ixVal == (uint) option->id );
@@ -762,7 +765,12 @@ void TY_(CopyConfig)( TidyDocImpl* docTo, TidyDocImpl* docFrom )
762765
}
763766
if ( needReparseTagsDecls )
764767
ReparseTagDecls( docTo, changedUserTags );
765-
AdjustConfig( docTo ); /* Make sure it's consistent */
768+
769+
/* @jsd: do NOT mess with user-specified settings until
770+
* absolutely necessary, and ensure that we can
771+
* can restore them immediately after the need.
772+
*/
773+
// TY_(AdjustConfig)( docTo ); /* Make sure it's consistent */
766774
}
767775
}
768776

@@ -1074,7 +1082,11 @@ int TY_(ParseConfigFileEnc)( TidyDocImpl* doc, ctmbstr file, ctmbstr charenc )
10741082
if ( fname != (tmbstr) file )
10751083
TidyDocFree( doc, fname );
10761084

1077-
AdjustConfig( doc );
1085+
/* @jsd: do NOT mess with user-specified settings until
1086+
* absolutely necessary, and ensure that we can
1087+
* can restore them immediately after the need.
1088+
*/
1089+
// TY_(AdjustConfig)( docTo ); /* Make sure it's consistent */
10781090

10791091
/* any new config errors? If so, return warning status. */
10801092
return (doc->optionErrors > opterrs ? 1 : 0);
@@ -1214,7 +1226,7 @@ Bool TY_(AdjustCharEncoding)( TidyDocImpl* doc, int encoding )
12141226

12151227

12161228
/* ensure that config is self consistent */
1217-
static void AdjustConfig( TidyDocImpl* doc )
1229+
void TY_(AdjustConfig)( TidyDocImpl* doc )
12181230
{
12191231
if ( cfgBool(doc, TidyEncloseBlockText) )
12201232
TY_(SetOptionBool)( doc, TidyEncloseBodyText, yes );

src/config.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,20 @@ Bool TY_(ParseConfigValue)( TidyDocImpl* doc, TidyOptionId optId, ctmbstr optVa
335335
Bool TY_(AdjustCharEncoding)( TidyDocImpl* doc, int encoding );
336336

337337

338+
/** Ensure that the configuration options are self consistent.
339+
** THIS PROCESS IS DESTRUCTIVE TO THE USER STATE. It examines
340+
** certain user-specified options and changes other options
341+
** as a result. This means that documented API functions such
342+
** as tidyOptGetValue() won't return the user-set values after
343+
** this is used. As a result, *don't* just use this function
344+
** at every opportunity, but only where needed, which is ONLY
345+
** prior to parsing a stream, and again prior to saving a
346+
** stream (because we reset after parsing.)
347+
** @param doc The Tidy document to adjust.
348+
*/
349+
void TY_(AdjustConfig)( TidyDocImpl* doc );
350+
351+
338352
/** Indicates whether or not the current configuration is completely default.
339353
** @param doc The Tidy document.
340354
** @returns The result.

src/tidylib.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,8 +1462,9 @@ int TY_(DocParseStream)( TidyDocImpl* doc, StreamIn* in )
14621462
assert( doc->docIn == NULL );
14631463
doc->docIn = in;
14641464

1465-
TY_(ResetTags)(doc); /* reset table to html5 mode */
1466-
TY_(TakeConfigSnapshot)( doc ); /* Save config state */
1465+
TY_(ResetTags)(doc); /* Reset table to html5 mode */
1466+
TY_(TakeConfigSnapshot)( doc ); /* Save config state */
1467+
TY_(AdjustConfig)( doc ); /* Ensure config internal consistency */
14671468
TY_(FreeAnchors)( doc );
14681469

14691470
TY_(FreeNode)(doc, &doc->root);
@@ -2283,7 +2284,8 @@ int tidyDocSaveStream( TidyDocImpl* doc, StreamOut* out )
22832284
doc->docOut = NULL;
22842285
}
22852286

2286-
TY_(ResetConfigToSnapshot)( doc );
2287+
/* @jsd: removing this should solve #673, and allow saving of the buffer multiple times. */
2288+
// TY_(ResetConfigToSnapshot)( doc );
22872289
doc->pConfigChangeCallback = callback;
22882290

22892291
return tidyDocStatus( doc );

0 commit comments

Comments
 (0)