/** * * beautify * @param array $ast */ public function beautifyAst($ast, $parentType = '') { $result = ''; $first = true; foreach ($ast as $item) { if (!$first) { $result = rtrim($result, FL_NEWLINE) . FL_NEWLINE; } $hasComment = false; if (count($item['value']['commentBefore'])) { $hasComment = true; } $comment = $this->beautifyComment($item['value']); if ($comment['remove']) { $result = rtrim($result, FL_NEWLINE); } $result .= $comment['value']; // if ($item['type'] === FL_TOKEN_HTML_TAG_END) { continue; } if ($first) { $first = false; } $indent = $newline = false; if ($item['type'] === FL_TOKEN_HTML_TAG) { $count = count($item['children']); if ($count > 1) { $indent = true; $newline = true; } elseif ($count === 1) { $c = $item['children'][$count - 1]; if ($c['type'] !== FL_TOKEN_HTML_TEXT) { $indent = true; $newline = true; } } } else { if (count($ast) > 1) { $newline = true; } if ($item['type'] === FL_TOKEN_HTML_DOCTYPE || $item['type'] === FL_TOKEN_HTML_SINGLE_TAG) { $newline = true; } if ($item['type'] === FL_TOKEN_HTML_STYLE_TAG || $item['type'] === FL_TOKEN_HTML_SCRIPT_TAG) { $newline = true; $indent = true; } } if ($item['type'] !== FL_TOKEN_HTML_TEXT) { $result .= $this->getIndentString(); } else { if (count($ast) > 1) { $result .= $this->getIndentString(); } } if ($parentType === FL_TOKEN_HTML_STYLE_TAG) { Fl::loadClass("Fl_Css_Static"); $value = Fl_Css_Static::getStyleDetail($item['value']['value']); if ($value['prefix']) { $result .= $value['prefix'] . FL_NEWLINE; } $result .= $this->beautify_special($value['value'], 'css'); if ($value['suffix']) { $result .= FL_NEWLINE . $value['suffix'] . FL_NEWLINE; } } elseif ($parentType === FL_TOKEN_HTML_SCRIPT_TAG) { $result .= $this->beautify_special($item['value']['value'], 'js'); } else { $result .= $item['value']['value']; } if ($newline) { $result .= FL_NEWLINE; } if ($indent) { $this->indent++; } if (count($item['children'])) { $type = $item['type']; if ($type === FL_TOKEN_HTML_SCRIPT_TAG) { $tagInfo = Fl_Html_Static::getScriptTagInfo($item['value']['value'], $this); //虽然是script标签,但不一定是Js if (!$tagInfo['script'] || $tagInfo['external']) { $type = ''; } } $children = $this->beautifyAst($item['children'], $type); if ($tagInfo['external'] && !strlen(trim($children))) { $result = rtrim($result, FL_NEWLINE); $newline = false; $indent = 2; } else { $result .= $children; } } $types = array(FL_TOKEN_HTML_TAG => 1, FL_TOKEN_HTML_SCRIPT_TAG => 1, FL_TOKEN_HTML_PRE_TAG => 1, FL_TOKEN_HTML_STYLE_TAG => 1, FL_TOKEN_HTML_TEXTAREA_TAG => 1); $this->preToken = $item['value']; if (isset($types[$item['type']])) { if ($newline) { $result = rtrim($result, FL_NEWLINE); $result .= FL_NEWLINE; } if ($indent) { $this->indent--; if ($indent === true) { $result .= $this->getIndentString(); } } if (!empty($item['end'])) { $this->preToken = $item['end']; $comment = $this->beautifyComment($item['end']); $result .= rtrim($comment['value'], FL_NEWLINE); } $result .= '</' . $item['tag'] . '>'; } } return $result; }
/** * * compress style * @param array $token */ public function compressStyle($token) { if (!$this->options['compress_tag']) { return $token['value']; } $info = Fl_Html_Static::splitSpecialValue($token['value'], 'style', $this); $content = trim($info['content']); if ($this->options['remove_empty_style'] && !$content) { return ''; } if ($this->options['compress_inline_css'] && $content) { Fl::loadClass("Fl_Css_Static"); $value = Fl_Css_Static::getStyleDetail($content); $containTpl = $this->containTpl($value['value']); //自定义内联CSS压缩方法 if (!$containTpl && $this->cssCompressMethod) { $content = call_user_func($this->cssCompressMethod, $value['value'], $this); } else { $content = $this->getInstance("Fl_Css_Compress", $value['value'])->run(); } } if ($this->options['remove_optional_attrs']) { $tagInfo = $this->getInstance("Fl_Html_TagToken", $info['tag_start'])->run(); $tagInfo['lowerTag'] = strtolower($tagInfo['tag']); $info['tag_start'] = $this->compressStartTag($tagInfo); } if ($this->options['merge_adjacent_css']) { $endStyle = '</style>'; $outputLen = strlen($this->output); $last = substr($this->output, $outputLen - 8); if (strtolower($last) === $endStyle) { $this->output = substr($this->output, 0, $outputLen - 8); return $content . $info['tag_end']; } } return $info['tag_start'] . $content . $info['tag_end']; }