Discovers and API endpoint from an HTML document

@param string $html HTML document
@return Provider A Provider instance or NULL
427826
public function discoverFromHtml($html)
    {
        $links = $this->findLinks($html);

        if ( ! $link = reset($links)) {
            return;
        }

        $type = null;

        switch($link['type']) {
        case 'application/json+oembed':
            $type = Provider::TYPE_JSON;
            break;
        case 'text/xml+oembed':
            $type = Provider::TYPE_XML;
            break;
        }

        $url = $link['href'];
        $endpoint = $this->getEndpointUrl($url);

        return new Provider($endpoint, $type);
    }
533288
public function wash($html)
    {
        // Charset seems to be ignored (probably if defined in the HTML document)
        $node = new DOMDocument('1.0', $this->config['charset']);
        $this->extlinks = false;

        $html = $this->cleanup($html);

        // Find base URL for images
        if (preg_match('/<base\s+href=[\'"]*([^\'"]+)/is', $html, $matches)) {
            $this->config['base_url'] = $matches[1];
        }
        else {
            $this->config['base_url'] = '';
        }

        // Detect max nesting level (for dumpHTML) (#1489110)
        $this->max_nesting_level = (int) @ini_get('xdebug.max_nesting_level');

        // SVG need to be parsed as XML
        $this->is_xml = stripos($html, '<html') === false && stripos($html, '<svg') !== false;
        $method       = $this->is_xml ? 'loadXML' : 'loadHTML';
        $options      = 0;

        // Use optimizations if supported
        if (PHP_VERSION_ID >= 50400) {
            $options = LIBXML_PARSEHUGE | LIBXML_COMPACT | LIBXML_NONET;
            @$node->{$method}($html, $options);
        }
        else {
            @$node->{$method}($html);
        }

        return $this->dumpHtml($node);
    }
695362
public function HTMLToPDF($html, GrabzItPDFOptions $options = null)
	{
		if ($options == null)
		{
			$options = new GrabzItPDFOptions();			
		}

		$this->request = new GrabzItRequest($this->getRootUrl(true) . GrabzItClient::TakePDF, true, $options, $html);
	}
1049997
public function setHtml($html)
    {
        if (!\is_string($html)) {
            throw new \InvalidArgumentException('The provided HTML must be a string.', 1540403913);
        }
        if ($html === '') {
            throw new \InvalidArgumentException('The provided HTML must not be empty.', 1540403910);
        }

        $this->createUnifiedDomDocument($html);
    }
290348
private function extractAdSection(string $html): string
    {
        $html = preg_replace('/<!--\s*google_ad_section_start\(weight=ignore\)\s*-->.*?<!--\s*google_ad_section_end.*?-->/su', '', $html);
        if (preg_match('/<!--\s*google_ad_section_start[^>]*-->/u', $html)) {
            preg_match_all('/<!--\s*google_ad_section_start[^>]*-->(.*?)<!--\s*google_ad_section_end.*?-->/su', $html, $matches);
            $html = implode("\n", $matches[1]);
        }

        return $html;
    }
874871
public function setHtml($html = null)
    {
        $html = trim($html);
        $this->html = 0 < strlen($html) ? $html : null;

        return $this;
    }
1353265
public function get($html = null)
    {
        if (null === $html) {
            if (file_exists($this->_path)) {
                $html = file_get_contents($this->_path);
            }
        }

        $this->_path = null;

        return $this->parse($html);
    }
761131
public function getEndPoint()
    {
        $html = $this->response->getHtmlContent();

        if ($html && ($url = self::getEndPointFromDom($html))) {
            try {
                $endPoint = $this->response->getUrl()->createAbsolute($url);
            } catch (Exception $exception) {
                return null;
            }

            if ($endPoint->getExtension() !== 'xml' && !$endPoint->hasQueryParameter('format')) {
                return $endPoint->withQueryParameter('format', 'json');
            }

            return $endPoint;
        }
    }
496516
public function info($html)
    {
        $tags = array('{total}', '{start}', '{end}', '{page}', '{pages}');
        $code = array($this->total, $this->start() + 1, $this->end(), $this->get, $this->pages());

        return str_replace($tags, $code, $html);
    }
1169572
public function endpoint(Endpoint $endpoint = null)
    {
        if ($endpoint === null) {
            return $this->getRepository();
        }

        $this->repository($endpoint);

        return $this;
    }
157957
protected function endpoint($id = null)
    {
        return $id === null ? $this->endpoint : $this->endpoint . '/' . $id;
    }
592561
private function parseNoContents(string $tag, string $html): string
    {
        $remainingHtml = \ltrim($html);
        $matchingResult = \preg_match(
            "/(<\/\s*" . $tag . "\s*>)/i",
            $html,
            $endOfScriptMatches
        );
        if ($matchingResult === 0) {
            return '';
        }

        $closingTag = $endOfScriptMatches[1];
        $this->value = \mb_substr($remainingHtml, 0, \mb_strpos($html, $closingTag));

        return \mb_substr(
            \mb_strstr($remainingHtml, $closingTag),
            \mb_strlen($closingTag)
        );
    }
756345
protected function getEndpoint($method)
    {
        $endpoint = "simulate";
        $queryString = array();
        $text = $this->getArgument("text");
        if (!is_null($text)) {
            $queryString['text'] = $text;
            $this->delArgument("text");
        }

        $templateId = $this->getArgument("template_id");
        if (!is_null($templateId)) {
            $queryString['template_id'] = $templateId;
            $this->delArgument("template_id");
        }

        $variables = $this->getArgument("variables");
        if (!is_null($variables)) {
            $queryString['variables'] = json_encode($variables);
            $this->delArgument("variables");
        }

        $country = $this->getArgument("country");
        if (is_null($country)) {
            throw new \InvalidArgumentException("Country cant be null");
        }
        $queryString['country'] = $country;
        $this->delArgument("country");

        $queryString = http_build_query($queryString);
        return "$endpoint?$queryString";
    }
994965
protected function fetchEndpointForUrl($url)
    {
        $endPoint = null;

        try {
            $content = $this->browser->getContent($url);
        } catch (Exception $exception) {
            throw new Exception(
                'Unable to fetch the page body for "' . $url . '": ' . $exception->getMessage(),
                Exception::PAGE_BODY_FETCH_FAILED
            );
        }

        preg_match_all("/<link rel=\"alternate\"[^>]+>/i",
            $content,
            $out);
        if ($out[0]) {
            foreach ($out[0] as $link) {
                if (strpos($link, trim($this->preferredFormat . '+oembed')) !== false) {
                    $endPoint = $this->extractEndpointFromAttributes($link);
                    break;
                }
            }
        } else {
            throw new Exception(
                'No valid oEmbed links found on the document at "' . $url . '".',
                Exception::NO_OEMBED_LINKS_FOUND
            );
        }

        return $endPoint;
    }
440568
public function extractExternalJavaScript($html)
    {
        $matches = array();

        preg_match_all(
            '!' . $this->getExternalJavaScriptRegex() . '!isU',
            $html, $matches
        );

        $links = $this->createJavaScriptPaths($matches['links']);

        return array('tags' => $matches[0], 'links' => $links);
    }
1188414
public function convertToString($html)
    {
        $externalJavaScript = $this->extractExternalJavaScript($html);

        return $this->replaceJavaScriptTags($html, $externalJavaScript);
    }
947559
public function linkUrlsInTrustedHtml($html)
	{
		$reMarkup = '{</?([a-z]+)([^"\'>]|"[^"]*"|\'[^\']*\')*>|&#?[a-zA-Z0-9]+;|$}';

		$insideAnchorTag = false;
		$position = 0;
		$result = '';

		// Iterate over every piece of markup in the HTML.
		while (true)
		{
			$match = array();
			preg_match($reMarkup, $html, $match, PREG_OFFSET_CAPTURE, $position);

			list($markup, $markupPosition) = $match[0];

			// Process text leading up to the markup.
			$text = substr($html, $position, $markupPosition - $position);

			// Link URLs unless we're inside an anchor tag.
			if ( ! $insideAnchorTag )
			{
				$text = $this->linkUrlsAndEscapeHtml($text);
			}

			$result .= $text;

			// End of HTML?
			if ( $markup === '' )
			{
				break;
			}

			// Check if markup is an anchor tag ('<a>', '</a>').
			if ( $markup[0] !== '&' && $match[1][0] === 'a' )
			{
				$insideAnchorTag = ($markup[1] !== '/');
			}

			// Pass markup through unchanged.
			$result .= $markup;

			// Continue after the markup.
			$position = $markupPosition + strlen($markup);
		}

		return $result;
	}