180 foreach($this->content->getBlocks() as $item)
182 $grouped[$item[
'place']][] = $item[
'value'];
185 foreach($grouped as $place => $values)
187 $blocks[$place] =
"\n" . implode(
"\n", $values) .
"\n";
194 foreach($this->content->getStyles() as $item)
196 $styles .=
"\n" . $item[
'value'];
199 if($styles && preg_match_all(
"#<style[\\s\\S]*?>([\\s\\S]*?)</style>#i", $styles, $matchesStyles))
202 $matchesStylesCount = count($matchesStyles);
203 for($i = 0; $i < $matchesStylesCount; $i++)
205 $styles .=
"\n" . $matchesStyles[1][$i];
210 if(!$styles && count($blocks) === 0)
232 $placeList = $this->document->querySelectorAll(
'[' . static::BLOCK_PLACE_ATTR .
']');
233 if(empty($placeList))
239 $firstPlaceCode =
null;
240 $bodyPlaceCode =
null;
241 $placeListByCode = array();
242 foreach($placeList as $place)
245 if (!$place || !$place->getAttributeNode(static::BLOCK_PLACE_ATTR))
258 $placeCode = $place->getAttribute(static::BLOCK_PLACE_ATTR);
259 $placeListByCode[$placeCode] = $place;
262 $firstPlaceCode = $placeCode;
265 if(!$bodyPlaceCode && $placeCode == static::BLOCK_PLACE_ATTR_DEF_VALUE)
267 $bodyPlaceCode = $placeCode;
272 $blocksByExistType = array();
273 foreach($blocks as $placeCode => $blockHtml)
276 if(!array_key_exists($placeCode, $placeListByCode))
280 $placeCode = $bodyPlaceCode;
282 elseif($firstPlaceCode)
284 $placeCode = $firstPlaceCode;
292 $blocksByExistType[$placeCode][] = $blockHtml;
296 foreach($blocksByExistType as $placeCode => $blockHtmlList)
298 if(!array_key_exists($placeCode, $placeListByCode))
303 $place = $placeListByCode[$placeCode];
304 $place->setInnerHTML(implode(
"\n", $blockHtmlList));
310 $headDomElement = $this->document->getHead();
316 $styleNode = end($headDomElement->querySelectorAll(
'style[' . self::STYLIST_TAG_ATTR .
']'));
319 $styleNode = $this->document->createElement(
'style');
320 $styleNode->setAttribute(
'type',
'text/css');
321 $styleNode->setAttribute(self::STYLIST_TAG_ATTR,
'item');
322 $headDomElement->appendChild($styleNode);
323 $styleNode->appendChild($this->document->createTextNode($styleString));
327 $styleList1 = CssParser::parseCss($styleNode->getTextContent());
328 $styleList2 = CssParser::parseCss($styleString);
329 $styleList = array_merge($styleList1, $styleList2);
331 $styleListByKey = array();
332 foreach($styleList as $styleItem)
334 if(!is_array($styleListByKey[$styleItem[
'SELECTOR']]))
336 $styleListByKey[$styleItem[
'SELECTOR']] = array();
339 $styleListByKey[$styleItem[
'SELECTOR']] = array_merge(
340 $styleListByKey[$styleItem[
'SELECTOR']],
346 foreach($styleListByKey as $selector => $declarationList)
348 $stylesString .= $selector .
'{' . CssParser::getDeclarationString($declarationList) .
"}\n";
353 $styleNode->setInnerHTML(
'');
354 $styleNode->appendChild($this->document->createTextNode($stylesString));