Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/Indenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class Indenter {
'indentation_character' => ' '
),
$inline_elements = array('b', 'big', 'i', 'small', 'tt', 'abbr', 'acronym', 'cite', 'code', 'dfn', 'em', 'kbd', 'strong', 'samp', 'var', 'a', 'bdo', 'br', 'img', 'span', 'sub', 'sup'),
$temporary_replacements_script = array(),
$ignore_elements = array('script','pre','textarea'),

$temporary_replacements_ignore = array(),
$temporary_replacements_inline = array();

const ELEMENT_TYPE_BLOCK = 0;
Expand Down Expand Up @@ -60,11 +62,13 @@ public function setElementType ($element_name, $type) {
public function indent ($input) {
$this->log = array();

// Dindent does not indent <script> body. Instead, it temporary removes it from the code, indents the input, and restores the script body.
if (preg_match_all('/<script\b[^>]*>([\s\S]*?)<\/script>/mi', $input, $matches)) {
$this->temporary_replacements_script = $matches[0];
foreach ($matches[0] as $i => $match) {
$input = str_replace($match, '<script>' . ($i + 1) . '</script>', $input);
// Dindent does not indent. Instead, it temporary removes it from the code, indents the input, and restores the script body.
foreach ($this->ignore_elements as $key) {
if (preg_match_all('/<'.$key.'\b[^>]*>([\s\S]*?)<\/'.$key.'>/mi', $input, $matches)) {
$this->temporary_replacements_ignore[$key] = $matches[0];
foreach ($matches[0] as $i => $match) {
$input = str_replace($match, '<'.$key.'>' . ($i + 1) . '</'.$key.'>', $input);
}
}
}

Expand Down Expand Up @@ -159,8 +163,12 @@ public function indent ($input) {

$output = preg_replace('/(<(\w+)[^>]*>)\s*(<\/\2>)/', '\\1\\3', $output);

foreach ($this->temporary_replacements_script as $i => $original) {
$output = str_replace('<script>' . ($i + 1) . '</script>', $original, $output);
foreach ($this->ignore_elements as $key) {
if(isset($this->temporary_replacements_ignore[$key])){
foreach ($this->temporary_replacements_ignore[$key] as $i => $original) {
$output = str_replace('<'.$key.'>' . ($i + 1) . '</'.$key.'>', $original, $output);
}
}
}

foreach ($this->temporary_replacements_inline as $i => $original) {
Expand Down