<commit_before><?php

namespace Tracer\Formatter;

use Tracer\Step;

class SingleHtmlFile implements FormatterInterface
{
    protected $traceId = 0;

    protected $title = "Execution Trace";

    protected $openItem = false;

    protected $funcnames = array();

    public function __construct($config = array())
    {
        if (isset($config['title'])) {
            $this->title = $config['title'];
        }
    }

    public function startOutput()
    {
        $title = htmlspecialchars($this->title);

        $html = <<<EOHTML
<!DOCTYPE html>
<html>
<head>
    <title>$title</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            // Fix trace window dimentions
            $(window).resize(function() {
                setViewportSize();
            });
            setViewportSize();

            // Replace function name references
            $("#trace label.fn").each(function(i) {
                var name = funcnametbl[parseInt($(this).text(), 10)];
                if (name) { $(this).text(name); }
            });

            // Attach collapse / expand function
            $("#trace li:has(ul) > div label").click(function() {
                var parentItem = $(this).closest("li");
                $(parentItem).children("ul").first().toggle();
                $(parentItem).toggleClass("folded");
                if ($(parentItem).hasClass("folded")) {
                    var hiddenLines = $("li", parentItem).length;
                    $(" > div", parentItem).append('<span class="folded-info">[' + hiddenLines + ' lines hidden]</span>');
                } else {
                    $('span.folded-info', parentItem).remove();
                }
            });

            $("#trace li > div div.step-tooltip").prev("label").tooltip({
                delay: 100,
                showURL: false,
                fade: 50,
                bodyHandler: function() {
                    return $(this).next("div.step-tooltip").html();
                }
            });
        });

        function setViewportSize() {
            $("#trace").height($(document).height() - 114);
            $("#trace").width($(window).width() - 24);
        }

        function toParent(a) {
            var parentId = $(a).closest("ul").closest("li").attr('id');
            if (parentId) {
                window.location = '#' + parentId;
                $('#' + parentId + ' > div label').effect("highlight", {}, 3000);
            }
        }

        function filterOut(label) {
            var matches = $("#trace label:contains(" + label + ")").closest("li");
            matches.hide();
            alert("Filtered out " + matches.length + " lines");
        }

        {$this->getExternalJavaScript()}
    </script>
    <style type="text/css">
        body { font-family: Tahoma, sans-serif; font-size: 9pt; padding: 0; margin: 0 }
        h2 { margin: 0; }
        #trace { padding: 1em; position: fixed; left: 0; width: 100%; top: 114px; overflow: auto; }
        #trace ul { margin: 0; padding: 0; list-style-type: none; }
        #trace li ul { padding-left: 1em; }
        #trace ul li.folded { background-color: #e0e0e0; color: #a0a0a0; }
        #trace .include > div { background-color: #9edede; }
        #trace .functioncall > div { background-color: #ffffff; }
        #trace .header > div { background-color: #bcbcff; }
        #trace .throws > div { background-color: #ffbcbc !important; }
        #trace .write > div,
        #trace .exit > div,
        #trace .sendheaders > div { background-color: #fcfc20; }
        #trace li.request > div > span { font-size: 150%; font-weight: bold; }
        #trace label { font-family: monospace; font-weight: bold }
        #trace li div .line-controls { display: none; margin: 0 1em; }
        #trace li > div:hover .line-controls { display: inline-block; }
        #trace li .trace-id { display: inline-block; float: right; }
        #trace li > div:hover { background-color: #f0f000; }
        #heading { padding: 1em; border-bottom: 1px solid #a0a0a0; position: fixed; top: 0px; left: 0px; width: 100%; box-shadow: 5px 5px 10px #a0a0a0; height: 90px; background-color: #ffffff; z-index: 10 }
        .step-tooltip { display: none; }
        #tooltip { background: #333; color:#fff; padding: 5px; border-radius: 3px; box-shadow: 3px 3px 5px #aaa; position: absolute; z-index: 3000; opacity: 0.85; }
    </style>
</head>
<body>
    <div id="heading">
        <h1>$title</h1>
        <div id="controls">
            <div class="filter-control">
                <label>Filter out calls to: </label><input id="filter-out-input" type="text" />
                <button onclick="filterOut($('#filter-out-input')[0].value);">filter out</button>
            </div>
        </div>
    </div>

    <div id="trace">
        <ul>
EOHTML;

        $this->openItem = false;
        return $html;
    }

    public function increaseDepth()
    {
        if ($this->openItem) {
            $this->openItem = false;
            return "<ul>\n";
        } else {
            return "<li><ul>\n";
        }
    }

    public function write(Step $line)
    {
        if ($this->openItem) {
            $output = "</li>";
        } else {
            $output = "";
        }

        switch($line->type) {
            case Step::FILEINCLUDE:
                $class = "include";
                $html = "include <label>" . htmlspecialchars($line->data['file']) . "</label>";
                break;

            case Step::FUNCTIONCALL:
                $class = "functioncall";
                $html = $this->getFunctioncallHtml($line, $class);
                break;

            case Step::HEADER:
                $class = "header";
                $html = "header: <label>" . htmlspecialchars($line->data['header']) . '</label>';
                if (isset($line->data['replace'])) {
                    $html .= " (replace)";
                }
                break;

            case Step::REQUEST:
                $class = 'request';
                $html = "<span>Request for {$line->data['originalurl']} from {$line->data['remoteip']}</span>";
                break;

            case Step::WRITE:
                $class = "write";
                $html = "<strong>--- write {$line->data['size']} bytes to output ---</strong>";
                break;

            case Step::SENDHEADERS:
                $class = 'sendheaders';
                $html = "<strong>--- SEND HEADERS ---</strong>";
                break;

            case Step::SCRIPTEXIT:
                $class = 'exit';
                $html = "<strong>--- END SCRIPT ---</strong>";
                break;
        }

        $this->traceId++;

        if (isset($html)) {
            $output .= "<li class=\"$class\" id=\"traceline-{$this->traceId}\"><div>$html\n";
            if ($this->traceId > 1) {
                $output .= '<span class="line-controls"><a href="#" onclick="toParent(this); return false;">to parent</a></span>';
            }
            $output .= '<span class="trace-id">' . $this->traceId . '</span>';
            $output .= '</div>';

        } else {
            $output = '<li>&nbsp;';
            fprintf(STDERR, "WARNING: Unhandled type by renderer: $line->type\n");
        }

        $this->openItem = true;
        return $output;
    }

    public function decreaseDepth()
    {
        if ($this->openItem) {
            $this->openItem = false;
            return "</li></ul>\n";
        } else {
            return "</ul>";
        }
    }

    public function endOutput()
    {
        if ($this->openItem) {
            $this->openItem = false;
            $output = "\n</li>";
        } else {
            $output = "\n";
        }
        $output .= "</ul></div>";

        // write function name table
        $output .= '<script type="text/javascript">var funcnametbl = ' .
            json_encode(array_keys($this->funcnames)) . ';</script>';

        $output .= "</body></html>";

        return $output;
    }

    protected function getFuncNameRef($fname)
    {
        if (! isset($this->funcnames[$fname])) {
            $this->funcnames[$fname] = count($this->funcnames);
        }

        return $this->funcnames[$fname];
    }

    protected function getFunctioncallHtml(Step $step, &$class)
    {
        if (! $class) {
            $class = "functioncall";
        }

        $html = '<label class="fn">';

        if (isset($step->data['classname'])) {
            $class .= " method";
            if (! isset($step->data['this'])) $class .= " static";
            $funcname = $step->data['classname'] . (isset($step->data['this']) ? '->' : '::') . $step->data['funcname'];
        } else {
            $funcname = $step->data['funcname'];
        }

        $html .= $this->getFuncNameRef($funcname) . '</label>';

        // Create tooltip
        $html .= '<div class="step-tooltip">';

        if (isset($step->data['this'])) {
            $html .= '<div>Instance: ' . htmlspecialchars($step->data['this']) . '</div>';
        }

        if (isset($step->data['arguments'])) {
            $html .= '<div>Arguments: ' . htmlspecialchars($step->data['arguments']) . '</div>';
        }

        $html .= '<div>';
        if (isset($step->data['retvalue'])) {
            $html .= 'Returned: ' . htmlspecialchars($step->data['retvalue']);
        } elseif (isset($step->data['throws'])) {
            $class .= " throws";
            $html .= 'Exception Thrown: ' . htmlspecialchars($step->data['throws']);
        } elseif ($step->data['funcname'] == '__construct') {
            $class .= " constructor";
        } else {
            $html .= 'Returned: null';
        }

        $html .= '</div></div>';

        return $html;
    }

    /**
     * This function returns some 3rd party JavaScript code to be included in
     * the output HTML
     *
     * It uses Open-source JavaScript code with accordance to relevant licenses.
     *
     * Code is base64-encoded to ensure safety
     *
     * @return string
     */

    protected function getExternalJavascript()
    {
        return base64_decode(<<<'ENDOFJS'
<KEY>
                );
    }
}<commit_msg>placing hidden line cound in right position<commit_after><?php

namespace Tracer\Formatter;

use Tracer\Step;

class SingleHtmlFile implements FormatterInterface
{
    protected $traceId = 0;

    protected $title = "Execution Trace";

    protected $openItem = false;

    protected $funcnames = array();

    public function __construct($config = array())
    {
        if (isset($config['title'])) {
            $this->title = $config['title'];
        }
    }

    public function startOutput()
    {
        $title = htmlspecialchars($this->title);

        $html = <<<EOHTML
<!DOCTYPE html>
<html>
<head>
    <title>$title</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            // Fix trace window dimentions
            $(window).resize(function() {
                setViewportSize();
            });
            setViewportSize();

            // Replace function name references
            $("#trace label.fn").each(function(i) {
                var name = funcnametbl[parseInt($(this).text(), 10)];
                if (name) { $(this).text(name); }
            });

            // Attach collapse / expand function
            $("#trace li:has(ul) > div label").click(function() {
                var parentItem = $(this).closest("li");
                $(parentItem).children("ul").first().toggle();
                $(parentItem).toggleClass("folded");
                if ($(parentItem).hasClass("folded")) {
                    var hiddenLines = $("li", parentItem).length;
                    $(this).append('<span class="folded-info">[' + hiddenLines + ' lines hidden]</span>');
                } else {
                    $('span.folded-info', this).remove();
                }
            });

            $("#trace li > div div.step-tooltip").prev("label").tooltip({
                delay: 100,
                showURL: false,
                fade: 50,
                bodyHandler: function() {
                    return $(this).next("div.step-tooltip").html();
                }
            });
        });

        function setViewportSize() {
            $("#trace").height($(document).height() - 114);
            $("#trace").width($(window).width() - 24);
        }

        function toParent(a) {
            var parentId = $(a).closest("ul").closest("li").attr('id');
            if (parentId) {
                window.location = '#' + parentId;
                $('#' + parentId + ' > div label').effect("highlight", {}, 3000);
            }
        }

        function filterOut(label) {
            var matches = $("#trace label:contains(" + label + ")").closest("li");
            matches.hide();
            alert("Filtered out " + matches.length + " lines");
        }

        {$this->getExternalJavaScript()}
    </script>
    <style type="text/css">
        body { font-family: Tahoma, sans-serif; font-size: 9pt; padding: 0; margin: 0 }
        h2 { margin: 0; }
        #trace { padding: 1em; position: fixed; left: 0; width: 100%; top: 114px; overflow: auto; }
        #trace ul { margin: 0; padding: 0; list-style-type: none; }
        #trace li ul { padding-left: 1em; }
        #trace ul li.folded { background-color: #e0e0e0; color: #a0a0a0; }
        #trace .include > div { background-color: #9edede; }
        #trace .functioncall > div { background-color: #ffffff; }
        #trace .header > div { background-color: #bcbcff; }
        #trace .throws > div { background-color: #ffbcbc !important; }
        #trace .write > div,
        #trace .exit > div,
        #trace .sendheaders > div { background-color: #fcfc20; }
        #trace li.request > div > span { font-size: 150%; font-weight: bold; }
        #trace label { font-family: monospace; font-weight: bold }
        #trace li div .line-controls { display: none; margin: 0 1em; }
        #trace li > div:hover .line-controls { display: inline-block; }
        #trace li .trace-id { display: inline-block; float: right; }
        #trace li .folded-info { display: inline-block; margin: 0 1em; }
        #trace li > div:hover { background-color: #f0f000; }
        #heading { padding: 1em; border-bottom: 1px solid #a0a0a0; position: fixed; top: 0px; left: 0px; width: 100%; box-shadow: 5px 5px 10px #a0a0a0; height: 90px; background-color: #ffffff; z-index: 10 }
        .step-tooltip { display: none; }
        #tooltip { background: #333; color:#fff; padding: 5px; border-radius: 3px; box-shadow: 3px 3px 5px #aaa; position: absolute; z-index: 3000; opacity: 0.85; }
    </style>
</head>
<body>
    <div id="heading">
        <h1>$title</h1>
        <div id="controls">
            <div class="filter-control">
                <label>Filter out calls to: </label><input id="filter-out-input" type="text" />
                <button onclick="filterOut($('#filter-out-input')[0].value);">filter out</button>
            </div>
        </div>
    </div>

    <div id="trace">
        <ul>
EOHTML;

        $this->openItem = false;
        return $html;
    }

    public function increaseDepth()
    {
        if ($this->openItem) {
            $this->openItem = false;
            return "<ul>\n";
        } else {
            return "<li><ul>\n";
        }
    }

    public function write(Step $line)
    {
        if ($this->openItem) {
            $output = "</li>";
        } else {
            $output = "";
        }

        switch($line->type) {
            case Step::FILEINCLUDE:
                $class = "include";
                $html = "include <label>" . htmlspecialchars($line->data['file']) . "</label>";
                break;

            case Step::FUNCTIONCALL:
                $class = "functioncall";
                $html = $this->getFunctioncallHtml($line, $class);
                break;

            case Step::HEADER:
                $class = "header";
                $html = "header: <label>" . htmlspecialchars($line->data['header']) . '</label>';
                if (isset($line->data['replace'])) {
                    $html .= " (replace)";
                }
                break;

            case Step::REQUEST:
                $class = 'request';
                $html = "<span>Request for {$line->data['originalurl']} from {$line->data['remoteip']}</span>";
                break;

            case Step::WRITE:
                $class = "write";
                $html = "<strong>--- write {$line->data['size']} bytes to output ---</strong>";
                break;

            case Step::SENDHEADERS:
                $class = 'sendheaders';
                $html = "<strong>--- SEND HEADERS ---</strong>";
                break;

            case Step::SCRIPTEXIT:
                $class = 'exit';
                $html = "<strong>--- END SCRIPT ---</strong>";
                break;
        }

        $this->traceId++;

        if (isset($html)) {
            $output .= "<li class=\"$class\" id=\"traceline-{$this->traceId}\"><div>$html\n";
            if ($this->traceId > 1) {
                $output .= '<span class="line-controls"><a href="#" onclick="toParent(this); return false;">to parent</a></span>';
            }
            $output .= '<span class="trace-id">' . $this->traceId . '</span>';
            $output .= '</div>';

        } else {
            $output = '<li>&nbsp;';
            fprintf(STDERR, "WARNING: Unhandled type by renderer: $line->type\n");
        }

        $this->openItem = true;
        return $output;
    }

    public function decreaseDepth()
    {
        if ($this->openItem) {
            $this->openItem = false;
            return "</li></ul>\n";
        } else {
            return "</ul>";
        }
    }

    public function endOutput()
    {
        if ($this->openItem) {
            $this->openItem = false;
            $output = "\n</li>";
        } else {
            $output = "\n";
        }
        $output .= "</ul></div>";

        // write function name table
        $output .= '<script type="text/javascript">var funcnametbl = ' .
            json_encode(array_keys($this->funcnames)) . ';</script>';

        $output .= "</body></html>";

        return $output;
    }

    protected function getFuncNameRef($fname)
    {
        if (! isset($this->funcnames[$fname])) {
            $this->funcnames[$fname] = count($this->funcnames);
        }

        return $this->funcnames[$fname];
    }

    protected function getFunctioncallHtml(Step $step, &$class)
    {
        if (! $class) {
            $class = "functioncall";
        }

        $html = '<label class="fn">';

        if (isset($step->data['classname'])) {
            $class .= " method";
            if (! isset($step->data['this'])) $class .= " static";
            $funcname = $step->data['classname'] . (isset($step->data['this']) ? '->' : '::') . $step->data['funcname'];
        } else {
            $funcname = $step->data['funcname'];
        }

        $html .= $this->getFuncNameRef($funcname) . '</label>';

        // Create tooltip
        $html .= '<div class="step-tooltip">';

        if (isset($step->data['this'])) {
            $html .= '<div>Instance: ' . htmlspecialchars($step->data['this']) . '</div>';
        }

        if (isset($step->data['arguments'])) {
            $html .= '<div>Arguments: ' . htmlspecialchars($step->data['arguments']) . '</div>';
        }

        $html .= '<div>';
        if (isset($step->data['retvalue'])) {
            $html .= 'Returned: ' . htmlspecialchars($step->data['retvalue']);
        } elseif (isset($step->data['throws'])) {
            $class .= " throws";
            $html .= 'Exception Thrown: ' . htmlspecialchars($step->data['throws']);
        } elseif ($step->data['funcname'] == '__construct') {
            $class .= " constructor";
        } else {
            $html .= 'Returned: null';
        }

        $html .= '</div></div>';

        return $html;
    }

    /**
     * This function returns some 3rd party JavaScript code to be included in
     * the output HTML
     *
     * It uses Open-source JavaScript code with accordance to relevant licenses.
     *
     * Code is base64-encoded to ensure safety
     *
     * @return string
     */

    protected function getExternalJavascript()
    {
        return base64_decode(<<<'ENDOFJS'
<KEY>
ENDOFJS
                );
    }
}
{'fraction_non_alphanumeric': 0.17460241654329242, 'fraction_numerical': 0.009933616696117293, 'mean_word_length': 1.7786624203821657, 'pattern_counts': {'":': 0, '<': 175, '<?xml version=': 0, '>': 286, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '236680', 'n_tokens_mistral': 6265, 'n_tokens_neox': 5900, 'n_words': 1310}