/** * Compiles AngularJS ng-value attributes by evaluating expression inside it * and setting value attribute. * * @param \DOMElement $domElement DOM element to compile. * @param Scope $scope Scope object containing data for expression. * @return \DOMElement Compiled DOM element. */ public function compile(\DOMElement $domElement, Scope $scope) { $expressionString = $domElement->getAttribute('ng-value'); $expression = new Expression($this->phCompile); $expressionValue = $expression->compile($expressionString, $scope); $domElement->setAttribute('value', $expressionValue); return $domElement; }
/** * Renders AngularJS ng-show and ng-hide attributes by evaluating expression * inside them and setting "ng-hide" class if needed. * * @param \DOMElement $domElement DOM element to render. * @param Scope $scope Scope object containing data for expression. * @return \DOMElement Compiled DOM element. */ public function compile(\DOMElement $domElement, Scope $scope) { $expressionString = $domElement->getAttribute('ng-show'); /** * Get attribute expression's value. */ $expression = new Expression($this->phCompile); $expressionValue = (bool) $expression->compile($expressionString, $scope); /** * Set appropriate class to DOM element if needed. */ if ($expressionValue === false) { Utils::addClass($domElement, 'ng-hide'); } return $domElement; }
/** * Compiles AngularJS ng-src attributes by evaluating expression inside it * and setting src attribute. * * @param \DOMElement $domElement DOM element to compile. * @param Scope $scope Scope object containing data for expression. * @return \DOMElement Compiled DOM element. */ public function compile(\DOMElement $domElement, Scope $scope) { $attrValue = $domElement->getAttribute('ng-src'); $foundExpressions = array(); $expression = new Expression($this->phCompile); /** * Find all {{}} expressions. */ preg_match_all('/{{([^}]+)}}/', $attrValue, $foundExpressions); foreach ($foundExpressions[1] as $foundExpression) { /** * Render and cover with span for easy client-site reverting. */ $renderedExpression = $expression->compile($foundExpression, $scope); /** * Replace {{}} expression with rendered value. */ $attrValue = str_replace('{{' . $foundExpression . '}}', $renderedExpression, $attrValue); } $domElement->setAttribute('src', $attrValue); return $domElement; }
/** * Finds and compiles expressions in template's HTML. */ protected function compileExpressions() { $foundExpressions = array(); $renderAttribute = $this->phCompile->getConfig('compile.attr'); $expression = new Expression($this->phCompile); /** * Find all {{}} expressions. */ preg_match_all('/{{([^}]+)}}/', $this->html, $foundExpressions); foreach ($foundExpressions[1] as $foundExpression) { /** * Render and cover with span for easy client-site reverting. */ $renderedExpression = $expression->compile($foundExpression, $this->scope); $renderedExpression = '<span ' . $renderAttribute . '="' . $foundExpression . '">' . $renderedExpression . '</span>'; /** * Replace {{}} expression with rendered value. */ $this->html = str_replace('{{' . $foundExpression . '}}', $renderedExpression, $this->html); } }
/** * Compiles ng-class attribute as expression evaluating to object with values * to evaluate and keys as class names. * * @param string $classAttr Ng-class attribute value. * @param Scope $scope Scope object with data for current expression. * @return string Compiled class string. */ protected function compileObject($classAttr, $scope) { $expression = new Expression($this->phCompile); $classArray = explode(',', trim($classAttr, ' {}')); array_walk($classArray, function (&$singleExpression) { $singleExpression = explode(':', $singleExpression); }); $classString = ''; foreach ($classArray as $singleClassArray) { if ($expression->compile(trim($singleClassArray[1]), $scope) == true) { $classString .= ' ' . trim($singleClassArray[0]); } } return trim($classString); }
/** * @expectedException PhCompile\Template\Expression\InvalidExpressionException */ public function testForbidFunction() { $this->scope->setData(array()); $expression = new Expression($this->phCompile); $expression->compile('system()', $this->scope); }