<commit_before><?php

/**
 * This file is part of CodeIgniter 4 framework.
 *
 * (c) CodeIgniter Foundation <<EMAIL>>
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace CodeIgniter\Test;

/**
 * @internal
 */
final class DOMParserTest extends CIUnitTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        if (! extension_loaded('DOM')) {
            $this->markTestSkipped('DOM extension not loaded.');
        }
    }

    public function testCanRoundTripHTML()
    {
        $dom = new DOMParser();

        $html     = '<div><h1>Hello</h1></div>';
        $expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">' . "\n"
                . '<html><body><div><h1>Hello</h1></div></body></html>';

        $this->assertSame($expected . "\n", $dom->withString($html)->getBody());
    }

    public function testParseSelectorWithID()
    {
        $dom = new DOMParser();

        $selector = $dom->parseSelector('div#row');

        $this->assertSame('div', $selector['tag']);
        $this->assertSame('row', $selector['id']);
    }

    public function testParseSelectorWithClass()
    {
        $dom = new DOMParser();

        $selector = $dom->parseSelector('div.row');

        $this->assertSame('div', $selector['tag']);
        $this->assertSame('row', $selector['class']);
    }

    public function testParseSelectorWithClassMultiple()
    {
        $dom = new DOMParser();

        $selector = $dom->parseSelector('div.row.another');

        $this->assertSame('div', $selector['tag']);
        // Only parses the first class
        $this->assertSame('row', $selector['class']);
    }

    public function testParseSelectorWithAttribute()
    {
        $dom = new DOMParser();

        $selector = $dom->parseSelector('a[ href = http://example.com ]');

        $this->assertSame('a', $selector['tag']);
        $this->assertSame(['href' => 'http://example.com'], $selector['attr']);
    }

    public function provideText()
    {
        return [
            ['Hello World'],
            ['<NAME>'],
        ];
    }

    /**
     * @dataProvider provideText
     *
     * @param mixed $text
     */
    public function testSeeText($text)
    {<commit_msg>test: add test case<commit_after><?php

/**
 * This file is part of CodeIgniter 4 framework.
 *
 * (c) CodeIgniter Foundation <<EMAIL>>
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace CodeIgniter\Test;

/**
 * @internal
 */
final class DOMParserTest extends CIUnitTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        if (! extension_loaded('DOM')) {
            $this->markTestSkipped('DOM extension not loaded.');
        }
    }

    public function testCanRoundTripHTML()
    {
        $dom = new DOMParser();

        $html     = '<div><h1>Hello</h1></div>';
        $expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">' . "\n"
                . '<html><body><div><h1>Hello</h1></div></body></html>';

        $this->assertSame($expected . "\n", $dom->withString($html)->getBody());
    }

    public function testParseSelectorWithID()
    {
        $dom = new DOMParser();

        $selector = $dom->parseSelector('div#row');

        $this->assertSame('div', $selector['tag']);
        $this->assertSame('row', $selector['id']);
    }

    public function testParseSelectorWithClass()
    {
        $dom = new DOMParser();

        $selector = $dom->parseSelector('div.row');

        $this->assertSame('div', $selector['tag']);
        $this->assertSame('row', $selector['class']);
    }

    public function testParseSelectorWithClassMultiple()
    {
        $dom = new DOMParser();

        $selector = $dom->parseSelector('div.row.another');

        $this->assertSame('div', $selector['tag']);
        // Only parses the first class
        $this->assertSame('row', $selector['class']);
    }

    public function testParseSelectorWithAttribute()
    {
        $dom = new DOMParser();

        $selector = $dom->parseSelector('a[ href = http://example.com ]');

        $this->assertSame('a', $selector['tag']);
        $this->assertSame(['href' => 'http://example.com'], $selector['attr']);
    }

    public function provideText()
    {
        return [
            'en' => ['Hello World'],
            'sv' => ['Hej, världen'],
            'ja' => ['こんにちは、世界'],
        ];
    }

    /**
     * @dataProvider provideText
     *
     * @param mixed $text
     */
    public function testSeeText($text)
    {
{'fraction_non_alphanumeric': 0.17587939698492464, 'fraction_numerical': 0.0046063651591289785, 'mean_word_length': 2.486861313868613, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2997829', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1384, 'n_words': 296}