コード例 #1
0
ファイル: Parser.php プロジェクト: raz0rsdge/horde
 /**
  * Constructor.
  *
  * @param string $css                       CSS data.
  * @param Sabberworm\CSS\Settings $charset  Parser settings.
  */
 public function __construct($css, Sabberworm\CSS\Settings $settings = null)
 {
     if (is_null($settings)) {
         $settings = Sabberworm\CSS\Settings::create();
         $settings->withMultibyteSupport(false);
     }
     $this->parser = new Sabberworm\CSS\Parser($css, $settings);
     $this->doc = $this->parser->parse();
 }
コード例 #2
0
 public static function processCSSContent($sContent, $oFile)
 {
     $oCache = new Cache('preview_css' . $oFile->getInternalPath(), DIRNAME_TEMPLATES);
     header("Content-Type: text/css;charset=" . Settings::getSetting('encoding', 'browser', 'utf-8'));
     if ($oCache->entryExists() && !$oCache->isOutdated($oFile->getFullPath())) {
         $oCache->sendCacheControlHeaders();
         $oCache->passContents();
         exit;
     }
     $oParser = new Sabberworm\CSS\Parser($sContent, Sabberworm\CSS\Settings::create()->withDefaultCharset(Settings::getSetting('encoding', 'browser', 'utf-8')));
     $oCssContents = $oParser->parse();
     //Make all rules important
     // foreach($oCssContents->getAllRuleSets() as $oCssRuleSet) {
     //	foreach($oCssRuleSet->getRules() as $oRule) {
     //		$oRule->setIsImportant(true);
     //	}
     // }
     //Multiply all rules and prepend specific strings
     $aPrependages = array('#rapila_admin_menu', '.filled-container.editing', '.ui-dialog', '.cke_dialog_contents', '#widget-notifications', '.cke_reset', 'body > .cke_reset_all', '.tag_panel');
     foreach ($oCssContents->getAllDeclarationBlocks() as $oBlock) {
         $aNewSelector = array();
         foreach ($oBlock->getSelectors() as $iKey => $oSelector) {
             $sSelector = $oSelector->getSelector();
             if (StringUtil::startsWith($sSelector, "body ") || StringUtil::startsWith($sSelector, "html ")) {
                 $aNewSelector[] = $sSelector;
             } else {
                 foreach ($aPrependages as $sPrependage) {
                     if (StringUtil::startsWith($sSelector, "{$sPrependage} ") || StringUtil::startsWith($sSelector, "{$sPrependage}.") || $sSelector === $sPrependage) {
                         $aNewSelector[] = $sSelector;
                     } else {
                         $aNewSelector[] = "{$sPrependage} {$sSelector}";
                     }
                 }
             }
         }
         $oBlock->setSelector($aNewSelector);
     }
     //Absolutize all URLs
     foreach ($oCssContents->getAllValues() as $oValue) {
         if ($oValue instanceof Sabberworm\CSS\Value\URL) {
             $sURL = $oValue->getURL()->getString();
             if (!StringUtil::startsWith($sURL, '/') && !preg_match('/^\\w+:/', $sURL)) {
                 $sURL = $oFile->getFrontendDirectoryPath() . DIRECTORY_SEPARATOR . $sURL;
             }
             $oValue->setURL(new Sabberworm\CSS\Value\CSSString($sURL));
         }
     }
     $sContents = $oCssContents->render(Sabberworm\CSS\OutputFormat::createCompact());
     $oCache->setContents($sContents);
     $oCache->sendCacheControlHeaders();
     print $sContents;
 }
コード例 #3
0
 public function adminGetContainers()
 {
     $oTemplate = $this->oPage->getTemplate();
     foreach ($oTemplate->identifiersMatching('container', Template::$ANY_VALUE) as $oIdentifier) {
         $oInheritedFrom = null;
         $sContainerName = $oIdentifier->getValue();
         if (BooleanParser::booleanForString($oIdentifier->getParameter('inherit'))) {
             $oInheritedFrom = $this->oPage;
             $iInheritedObjectCount = 0;
             while ($iInheritedObjectCount === 0 && ($oInheritedFrom = $oInheritedFrom->getParent()) !== null) {
                 $iInheritedObjectCount = $oInheritedFrom->countObjectsForContainer($sContainerName);
             }
         }
         $sInheritedFrom = $oInheritedFrom ? $oInheritedFrom->getName() : '';
         $aTagParams = array('class' => 'template-container template-container-' . $sContainerName, 'data-container-name' => $sContainerName, 'data-container-string' => TranslationPeer::getString('container_name.' . $sContainerName, null, $sContainerName), 'data-inherited-from' => $sInheritedFrom);
         $oContainerTag = TagWriter::quickTag('ol', $aTagParams);
         $mInnerTemplate = new Template(TemplateIdentifier::constructIdentifier('content'), null, true);
         //Replace container info
         //…name
         $mInnerTemplate->replaceIdentifierMultiple('content', TagWriter::quickTag('div', array('class' => 'template-container-description'), TranslationPeer::getString('wns.page.template_container', null, null, array('container' => TranslationPeer::getString('template_container.' . $sContainerName, null, $sContainerName)), true)));
         //…additional info
         $mInnerTemplate->replaceIdentifierMultiple('content', TagWriter::quickTag('div', array('class' => 'template-container-info')));
         //…tag
         $mInnerTemplate->replaceIdentifierMultiple('content', $oContainerTag);
         //Replace actual container
         $oTemplate->replaceIdentifier($oIdentifier, $mInnerTemplate);
     }
     $bUseParsedCss = Settings::getSetting('admin', 'use_parsed_css_in_config', true);
     $oStyle = null;
     if ($bUseParsedCss) {
         $sTemplateName = $this->oPage->getTemplateNameUsed() . Template::$SUFFIX;
         $sCacheKey = 'parsed-css-' . $sTemplateName;
         $oCssCache = new Cache($sCacheKey, DIRNAME_PRELOAD);
         $sCssContents = "";
         if (!$oCssCache->entryExists() || $oCssCache->isOutdated(ResourceFinder::create(array(DIRNAME_TEMPLATES, $sTemplateName)))) {
             $oIncluder = new ResourceIncluder();
             foreach ($oTemplate->identifiersMatching('addResourceInclude', Template::$ANY_VALUE) as $oIdentifier) {
                 $oIncluder->addResourceFromTemplateIdentifier($oIdentifier);
             }
             foreach ($oIncluder->getAllIncludedResources() as $sIdentifier => $aResource) {
                 if ($aResource['resource_type'] === ResourceIncluder::RESOURCE_TYPE_CSS && !isset($aResource['ie_condition']) && !isset($aResource['frontend_specific'])) {
                     if (isset($aResource['media'])) {
                         $sCssContents .= "@media {$aResource['media']} {";
                     }
                     if (isset($aResource['file_resource'])) {
                         $sCssContents .= file_get_contents($aResource['file_resource']->getFullPath());
                     } else {
                         // Absolute link, requires fopen wrappers
                         $sCssContents .= file_get_contents($aResource['location']);
                     }
                     if (isset($aResource['media'])) {
                         $sCssContents .= "}";
                     }
                 }
             }
             $oParser = new Sabberworm\CSS\Parser($sCssContents, Sabberworm\CSS\Settings::create()->withDefaultCharset(Settings::getSetting("encoding", "browser", "utf-8")));
             $oCss = $oParser->parse();
             $this->cleanupCSS($oCss);
             $sCssContents = Template::htmlEncode($oCss->render(Sabberworm\CSS\OutputFormat::createCompact()));
             $oCssCache->setContents($sCssContents);
         } else {
             $sCssContents = $oCssCache->getContentsAsString();
         }
         $oStyle = new HtmlTag('style');
         $oStyle->addParameters(array('scoped' => 'scoped'));
         $oStyle->appendChild($sCssContents);
     }
     $sTemplate = $oTemplate->render();
     $sTemplate = substr($sTemplate, strpos($sTemplate, '<body') + 5);
     $sTemplate = substr($sTemplate, strpos($sTemplate, '>') + 1);
     $sTemplate = substr($sTemplate, 0, strpos($sTemplate, '</body'));
     $oParser = new TagParser("<body>{$sTemplate}</body>");
     $oTag = $oParser->getTag();
     $this->cleanupContainerStructure($oTag);
     if ($bUseParsedCss) {
         $oTag->appendChild($oStyle);
     }
     $sResult = $oTag->__toString();
     $sResult = substr($sResult, strpos($sResult, '<body>') + 6);
     $sResult = substr($sResult, 0, strrpos($sResult, '</body>'));
     return array('html' => $sResult, 'css_parsed' => $bUseParsedCss);
 }