<commit_before><commit_msg>Added      tests/DomCssToXpathTest.php<commit_after><?php
namespace Tests;

use ManaPHP\Dom\CssToXpath;

class DomCssToXpathTest extends \PHPUnit_Framework_TestCase
{
    public function test_transform()
    {
        $cssToXpath = new CssToXpath();
        $css_xpaths = [

            //all selector
            '*' => '//*',

            //element types
            'div' => '//div',
            'a' => '//a',
            'span' => '//span',
            'h2' => '//h2',

            //style attributes
            '.error' => "//*[contains(concat(' ', normalize-space(@class), ' '), ' error ')]",
            'div.error' => "//div[contains(concat(' ', normalize-space(@class), ' '), ' error ')]",
            'label.required' => "//label[contains(concat(' ', normalize-space(@class), ' '), ' required ')]",

            //id attributes
            '#content' => "//*[@id='content']",
            'div#nav' => "//div[@id='nav']",

            //arbitrary attributes
            'div[bar="baz"]' => "//div[@bar='baz']",//exact match
            'div[bar~="baz"]' => "//div[contains(concat(' ', normalize-space(@bar), ' '), ' baz ')]",//word match
            'div[bar*="baz"]' => "//div[contains(@bar, 'baz')]",//substring match

            //has attributes
        //    'div[bar]' => '//div[@bar]',//has bar attributes

            //direct descendents
            'div > span' => '//div/span',

            //descendents
            'div .foo span #one' => "//div//*[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]//span//*[@id='one']|//div[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]//span//*[@id='one']",

            'foo|bar' => '//foo|bar',

            //https://stackoverflow.com/questions/1604471/how-can-i-find-an-element-by-css-class-with-xpath
            'div>.foo' => "//div/[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]",
            'div > .foo' => "//div/[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]",
            'div>.foo.bar' => "//div/[contains(concat(' ', normalize-space(@class), ' '), ' foo ')][contains(concat(' ', normalize-space(@class), ' '), ' bar ')]",
            'div.bar span' => "//div[contains(concat(' ', normalize-space(@class), ' '), ' bar ')]//span",
            'div > p.first' => "//div/p[contains(concat(' ', normalize-space(@class), ' '), ' first ')]",
            'a[rel="include"]' => "//a[@rel='include']",
            "a[rel='include']" => "//a[@rel='include']",

        ];

        foreach ($css_xpaths as $css => $xpath) {
            $this->assertEquals($xpath, $cssToXpath->transform($css), json_encode(['css'=>$css,'xpath'=>$xpath], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT));
        }
    }
}
{'fraction_non_alphanumeric': 0.24963872832369943, 'fraction_numerical': 0.0032514450867052024, 'mean_word_length': 2.2808056872037916, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 37, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, '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': '1434764', 'n_tokens_mistral': 934, 'n_tokens_neox': 891, 'n_words': 130}