renderer = $renderer; $this->random = $random; $this->processors = $processors; } /** * Renders HTML tag while possibly modifying or using it's attributes and content for security reasons. * * @param string $tagName Like "script" or "style" * @param string[] $attributes Attributes map, values must not be escaped. * @param string|null $content Tag's content. * @param bool $textContent Whether to treat the tag's content as text or HTML. * @return string */ public function renderTag( string $tagName, array $attributes, ?string $content = null, bool $textContent = true ): string { $tag = new TagData($tagName, $attributes, $content, $textContent); foreach ($this->processors as $processor) { $tag = $processor->processTag($tag); } return $this->renderer->renderTag($tag); } /** * Render event listener as an HTML attribute while possibly modifying or using it's content for security reasons. * * @param string $eventName Full attribute name like "onclick". * @param string $javascript * @return string */ public function renderEventListener(string $eventName, string $javascript): string { $event = new EventHandlerData($eventName, $javascript); foreach ($this->processors as $processor) { $event = $processor->processEventHandler($event); } return $this->renderer->renderEventHandler($event); } /** * Render event listener script as a separate tag instead of an attribute. * * @param string $eventName Full event name like "onclick". * @param string $attributeJavascript JS that would've gone to an HTML attribute. * @param string $elementSelector CSS selector for the element we handle the event for. * @return string Result script tag. */ public function renderEventListenerAsTag( string $eventName, string $attributeJavascript, string $elementSelector ): string { if (!$eventName || !$attributeJavascript || !$elementSelector || mb_strpos($eventName, 'on') !== 0) { throw new \InvalidArgumentException('Invalid JS event handler data provided'); } $random = $this->random->getRandomString(10); $listenerFunction = 'eventListener' . $random; $elementName = 'listenedElement' . $random; $script = <<