/** * * 压缩开始标签 */ public function compressStartTag($token) { if ($this->isXML) { return $token['value']; } $tag = $token['tag']; $lowerTag = $token['lowerTag']; $attrs = $token['attrs']; $resultAttrs = array(); foreach ($attrs as $item) { if ($item[1] === '=') { $valueDetail = Fl_Html_Static::getUnquoteText($item[2]); if ($this->options['removeAttrDefaultValue'] && Fl_Html_Static::isTagAttrDefaultValue($item[0], $valueDetail['text'], $lowerTag)) { continue; } if ($this->options['attrOnlyName'] && Fl_Html_Static::isTagOnlyNameAttr($item[0])) { $item = array($item[0]); } else { if ($this->options['removeAttrQuote'] && Fl_Html_Static::isAttrValueNoQuote($valueDetail['text'], $this)) { $item[2] = $valueDetail['text']; } } } $resultAttrs[] = $item; } $return = Fl_Html_Static::LEFT; if ($this->options['tagToLower']) { $return .= $lowerTag; } else { $return .= $tag; } $blankChar = ' '; $return .= $blankChar; foreach ($resultAttrs as $item) { $itemText = join('', $item); $lastChar = substr($return, strlen($return) - 1); if ($lastChar !== '"' && $lastChar !== "'" && $lastChar !== $blankChar) { if ($item[1] !== '=' && $this->isTpl($item[0]) && !$this->checkTplHasOutput($item[0])) { //do nothing } else { $return .= $blankChar; } } $return .= $itemText; } if ($this->options['endSingleTag'] && Fl_Html_Static::isSingleTag($lowerTag)) { $lastChar = substr($return, strlen($return) - 1); if ($lastChar !== '"' && $lastChar !== "'" && $lastChar !== $blankChar) { $return .= $blankChar; } $return .= '/'; } $return = rtrim($return); $return .= Fl_Html_Static::RIGHT; return $return; }
/** * * 压缩开始标签 */ public function compressStartTag($token) { if ($this->isXML || !$this->options['compress_tag']) { return $token['value']; } $tag = $token['tag']; $lowerTag = $token['lowerTag']; if ($lowerTag === 'meta' && $this->options['simple_charset']) { $result = $this->compressCharset($token); if ($result) { return $result; } } $attrs = $token['attrs']; $resultAttrs = array(); foreach ($attrs as $item) { if ($item[1] === '=') { $valueDetail = Fl_Html_Static::getUnquoteText($item[2]); $nameLower = strtolower($item[0]); if ($this->options['remove_optional_attrs'] && Fl_Html_Static::isTagAttrDefaultValue($item[0], $valueDetail['text'], $lowerTag)) { continue; } if ($this->options['remove_attrs_optional_value'] && Fl_Html_Static::isTagOnlyNameAttr($item[0])) { $item = array($item[0]); } else { if ($this->options['remove_attrs_quote'] && Fl_Html_Static::isAttrValueNoQuote($valueDetail['text'], $this)) { if ($nameLower != 'style') { $item[2] = $valueDetail['text']; $valueDetail['quote'] = ''; } } } //$nameLower = strtolower ( $item [0] ); //remove html xmlns attr if ($lowerTag === 'html' && $nameLower === 'xmlns' && $this->options['remove_html_xmlns']) { continue; } if ($this->options['remove_http_protocal'] || $this->options['remove_https_protocal']) { if ($nameLower === 'href' || $nameLower === "src") { $valueDetail = Fl_Html_Static::getUnquoteText($item[2]); $value = $valueDetail['text']; if ($this->options['remove_http_protocal'] && strpos($value, "http://") === 0) { $value = substr($value, 5); $item[2] = $valueDetail['quote'] . $value . $valueDetail['quote']; } elseif ($this->options['remove_https_protocal'] && strpos($value, "https://") === 0) { $value = substr($value, 6); $item[2] = $valueDetail['quote'] . $value . $valueDetail['quote']; } } } //$valueDetail = Fl_Html_Static::getUnquoteText ( $item [2] ); //remove ext blank in class value if ($nameLower === 'class' && !$this->containTpl($item[2])) { $value = trim($valueDetail['text']); $value = preg_split(FL_SPACE_PATTERN, $value); $item[2] = $valueDetail['quote'] . join(FL_SPACE, $value) . $valueDetail['quote']; } else { if ($this->options['compress_style_value'] && $nameLower === 'style' && !$this->containTpl($valueDetail["text"])) { //如果压缩失败,则直接使用源代码 try { if ($this->cssCompressMethod) { $value = call_user_func($this->cssCompressMethod, "*{" . $valueDetail["text"] . "}", $this); } else { $value = $this->getInstance("Fl_Css_Compress", "*{" . $valueDetail["text"] . "}")->run(); } $item[2] = $valueDetail['quote'] . substr($value, 2, strlen($value) - 3) . $valueDetail['quote']; } catch (Fl_Exception $e) { //这里不抛出异常 } } else { if (strpos($nameLower, "on") === 0) { //remove last ; in onxxx attr $value = trim(trim($valueDetail['text']), ';'); $item[2] = $valueDetail['quote'] . $value . $valueDetail['quote']; } } } } $resultAttrs[] = $item; } $return = Fl_Html_Static::LEFT; if ($this->options['tag_to_lower']) { $return .= $lowerTag; } else { $return .= $tag; } $blankChar = FL_SPACE; $return .= $blankChar; foreach ($resultAttrs as $item) { $itemText = join('', $item); $lastChar = substr($return, strlen($return) - 1); if ($lastChar !== '"' && $lastChar !== "'" && $lastChar !== $blankChar) { if ($item[1] !== '=' && $this->isTpl($item[0]) && !$this->checkTplHasOutput($item[0])) { //do nothing } else { $return .= $blankChar; } } else { $last2Char = substr($return, strlen($return) - 2); if ($last2Char === '\\"' || $last2Char === "\\'") { $return .= $blankChar; } } $return .= $itemText; } $return = rtrim($return) . Fl_Html_Static::RIGHT; return $return; }