Skip to content

Instantly share code, notes, and snippets.

@LeonardoCA
Created September 30, 2012 21:31
Show Gist options
  • Save LeonardoCA/3808502 to your computer and use it in GitHub Desktop.
Save LeonardoCA/3808502 to your computer and use it in GitHub Desktop.
dumpHtml - Nette dump combined with Tidy html formating and prettyPrint to make debuging Html code easier
<?php
use Nette\Diagnostics\Debugger;
/**
* Dump rendered Html element or code and inteded output generated by tidy and optional also Html element structure
* @param Html|string Html element or string
* @param bool Return output in string and don't send it to output?
* @param int MaxDepth for Debugger::Dump output - to see how is the Html element built
* @param bool Send along javascript code for prettyPrint activation?
* @return string Html output
*/
function dumpHtml($el, $onlyReturn = false, $maxDepth = 0, $includePrettyPrintActivation = false)
{
$maxDepthBackup = Debugger::$maxDepth;
$maxLenBackup = Debugger::$maxLen;
$showLocationBackup = Debugger::$showLocation;
Debugger::$maxDepth = $maxDepth;
Debugger::$maxLen = 200000;
Debugger::$showLocation = false;
$output = '<style>
li.L0, li.L1, li.L2, li.L3, li.L5, li.L6, li.L7, li.L8 {list-style-type: inherit;}
pre.prettyprint{
border: 1px solid #E1E1E8;
}
.prettyprint.linenums {
box-shadow: 45px 0 0 #FBFBFC inset, 46px 0 0 #ECECF0 inset;
}
pre{
background-color: #F7F7F9;
border: 1px solid #E1E1E8;
padding: 5px;
font-family: Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,serif;
max-height: 700px;
overflow-y: auto;
padding: 5px;
width: auto;
/*white-space: pre;*/}
ol.linenums {
margin: 0 0 0 43px;
width: auto;
}
ol.linenums li {
color: #BEBEC5;
line-height: 20px;
padding-left: 6px;
text-shadow: 0 1px 0 #FFFFFF;
width: auto;
}</style>';
$output .= '<div class="alert alert-info">' . (string) $el . '</div>';
$output .= '<pre class="prettyprint linenums">' . htmlspecialchars(tidyFormatString((string) $el)) . '</pre>';
if ($includePrettyPrintActivation) {
$output .= '<script type="text/javascript">/* <![CDATA[ */prettyPrint();/* ]]> */</script>';
}
if ($maxDepth > 0) {
Nette\Diagnostics\Debugger::dump((string)$el);
Nette\Diagnostics\Debugger::dump($el);
}
Debugger::$maxDepth = $maxDepthBackup;
Debugger::$maxLen = $maxLenBackup;
Debugger::$showLocation = $showLocationBackup;
if (!$onlyReturn) {
echo $output;
}
return $output;
}
/**
* Formats Html string to readable output
* options are set in a way so tidy should not fix anything - because this function is intented to be used for debugging
* @param string String which needs formating
* @param array $tidy_config Optional configuration parameters
* @return string Tidy formated string
*/
function tidyFormatString($string, $tidy_config = '')
{
$config = array(
'show-body-only' => true,
'clean' => false,
'char-encoding' => 'utf8',
'add-xml-decl' => true,
'add-xml-space' => true,
'output-html' => false,
'output-xml' => false,
'output-xhtml' => true,
'numeric-entities' => false,
'ascii-chars' => false,
'doctype' => 'strict',
'bare' => true,
'fix-uri' => false,
'indent' => true,
'indent-spaces' => 4,
'tab-size' => 4,
'wrap-attributes' => false,
'wrap' => 0,
'indent-attributes' => false,
'join-classes' => false,
'join-styles' => false,
'enclose-block-text' => false,
'fix-bad-comments' => false,
'fix-backslash' => false,
'replace-color' => false,
'wrap-asp' => false,
'wrap-jste' => false,
'wrap-php' => false,
'write-back' => false,
'drop-proprietary-attributes' => false,
'hide-comments' => false,
'hide-endtags' => false,
'literal-attributes' => false,
'drop-empty-paras' => false,
'enclose-text' => false,
'enclose-block-text' => false,
'quote-ampersand' => false,
'quote-marks' => false,
'quote-nbsp' => false,
'vertical-space' => false,
'wrap-script-literals' => false,
'tidy-mark' => false,
'merge-divs' => false,
'repeated-attributes' => 'keep-last',
'break-before-br' => false,
'preserve-entities' => true,
'force-output' => true,
'quiet' => true,
'output-bom' => false,
'new-empty-tags' => 'li'
);
if( $tidy_config == '' ) {
$tidy_config = &$config;
}
$tidy = new tidy();
$tidy->parseString($string, $tidy_config);
//dump($tidy->getConfig());
$out = (string )$tidy;
unset($tidy);
unset($tidy_config);
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment