escapeHtml = $escapeHtml; $this->escapeAttrs = $escapeAttrs; } protected function escape(NodeAbstract $node) { $enabled = $node->getEscaping()->isEnabled(); if ($enabled !== null) { return; } if ($node instanceof Text) { // interpolated strings that are not in attribute name/value // are plain HTML and should not be escaped if ($this->inInterpolatedString && !$this->inAttr) { $node->getEscaping()->setEnabled(false); return; } } // everything we don't explicitly not escape is escaped if ($this->inAttr) { $this->setEscape($node, $this->escapeAttrs); } else { $this->setEscape($node, $this->escapeHtml); } } protected function setEscape(NodeAbstract $node, $mode) { switch ($mode) { case self::ESCAPE_FALSE: $node->getEscaping()->setEnabled(false); break; case self::ESCAPE_ONCE: $node->getEscaping()->setEnabled(true)->setOnce(true); break; case self::ESCAPE_TRUE: $node->getEscaping()->setEnabled(true); break; } } public function enterTagAttribute(TagAttribute $node) { ++$this->inAttr; } public function leaveTagAttribute(TagAttribute $node) { --$this->inAttr; } public function enterInterpolatedString(InterpolatedString $node) { ++$this->inInterpolatedString; } public function leaveInterpolatedString(InterpolatedString $node) { --$this->inInterpolatedString; } public function enterText(Text $node) { $this->escape($node); } public function enterInsert(Insert $node) { $this->escape($node); } }