/**
  * @param HtmlHeaders $htmlHeaders
  */
 private function organizeJS($htmlHeaders)
 {
     $htmlContent = $this->DOMHtml->getContent();
     $regex = '/<script((?:.)*?)>(.*?)<\\/script>/smix';
     $config = $this->config;
     $self = $this;
     $content = preg_replace_callback($regex, function ($script) use($htmlHeaders, $config, $self) {
         $regex_tribb = '/(\\S+)=["\']((?:.(?!["\']\\s+(?:\\S+)=|[>"\']))+.)["\']/';
         preg_match_all($regex_tribb, $script[1], $matches);
         if (isset($matches[1]) && isset($matches[2])) {
             foreach ($matches[1] as $k => $key) {
                 if (trim($key) == 'src') {
                     $v = File::url_decode(trim($matches[2][$k]));
                 } else {
                     $v = trim($matches[2][$k]);
                 }
                 $attributes[trim($key)] = $v;
             }
         }
         if ($attributes['type'] == 'text/javascript' || !$attributes['type']) {
             if ($attributes['src']) {
                 $htmlHeaders->addJs($attributes);
                 return;
             } elseif ($config['JavascriptIntegrateInline'] && $config['JavascriptIntegrate']) {
                 $attributes['value'] = isset($script[2]) ? $script[2] : '';
                 if (!$self->jsAwaysInline($attributes['value'])) {
                     $self->addJsInline($htmlHeaders, $attributes);
                     return;
                 } else {
                     return $script[0];
                     /**
                      * @todo Adjust to work fine with document.write
                      */
                     /*
                      $id = md5($script[0]);
                      $attributes['value'] = str_replace('document.write(', 'replace_text("' . $id . '",', $attributes['value']);
                      $self->addJsInline($htmlHeaders, $attributes);
                      $replace = '<script type="text/javascript" id="' . $id . '">';
                      $replace .= 'var elem = document.getElementById("' . $id . '");';
                      $replace .= 'elem.addEventListener("' . $id . '", function (event) {';
                      $replace .= 'document.write(event.detail);';
                      $replace .= '});';
                      $replace .= '</script>';
                      return $replace;
                     */
                 }
             } else {
                 return $script[0];
             }
         } else {
             return $script[0];
         }
     }, $htmlContent);
     $this->DOMHtml->setContent($content ?: $htmlContent);
 }