function test_itShouldParseCssClasses() { $this->assertEquals(['block' => 'button', 'blockMod' => 'disabled', 'blockModVal' => true, 'elem' => 'control', 'elemMod' => null, 'elemModVal' => null], BH::parseBemCssClasses('button_disabled__control')); $this->assertEquals(['block' => 'button', 'blockMod' => 'mod', 'blockModVal' => 'val', 'elem' => 'elem', 'elemMod' => 'modelem', 'elemModVal' => 'valelem'], BH::parseBemCssClasses('button_mod_val__elem_modelem_valelem')); $this->assertEquals(['block' => 'button', 'blockMod' => 'disabled', 'blockModVal' => true, 'elem' => 'control', 'elemMod' => null, 'elemModVal' => null], BH::parseBemCssClasses('button--disabled__control', '--')); $this->assertEquals(['block' => 'button', 'blockMod' => 'mod', 'blockModVal' => 'val', 'elem' => 'elem', 'elemMod' => 'modelem', 'elemModVal' => 'valelem'], BH::parseBemCssClasses('button--mod_val__elem--modelem_valelem', '--')); $this->assertEquals(['block' => 'button', 'blockMod' => null, 'blockModVal' => null], BH::parseBemCssClasses('button', '--')); $this->assertEquals(['block' => 'button', 'blockMod' => null, 'blockModVal' => null, 'elem' => 'control', 'elemMod' => 'type', 'elemModVal' => 'span'], BH::parseBemCssClasses('button__control_type_span', '_')); $this->assertEquals(['block' => 'button', 'blockMod' => null, 'blockModVal' => null, 'elem' => 'control', 'elemMod' => 'type', 'elemModVal' => true], BH::parseBemCssClasses('button__control_type')); $this->assertEquals(['block' => 'button', 'blockMod' => null, 'blockModVal' => null, 'elem' => 'control', 'elemMod' => null, 'elemModVal' => null], BH::parseBemCssClasses('button__control')); }
public function test_matcher_list() { $bemjson = ['block' => 'button', 'content' => ['block' => 'nested', 'content' => ['elem' => 'elem', 'mods' => ['red']]]]; $list = ['green' => new \BEM\Matcher('button', function (\BEM\Context $ctx) { $ctx->mod('green', true); }), 'blue' => new \BEM\Matcher('button', function (\BEM\Context $ctx) { $ctx->mod('blue', true); })]; $ids = $this->bh->addMatcherList($list); static::assertSame([0, 1], $ids); $testWithBlueGreen = ' <div class="button button--blue button--green"> <div class="nested"> <div class="nested__elem nested__elem--red"> </div> </div> </div> '; $out = $this->bh->apply($bemjson); static::assertSame($testWithBlueGreen, $out); // change closure $list['blue']->matcher = function (\BEM\Context $ctx) { $ctx->mod('red', true); }; $testWithRedGreen = ' <div class="button button--red button--green"> <div class="nested"> <div class="nested__elem nested__elem--red"> </div> </div> </div> '; $out = $this->bh->apply($bemjson); static::assertSame($testWithRedGreen, $out); static::assertTrue($this->bh->removeMatcherById($list['blue']->id)); $testWithGreen = ' <div class="button button--green"> <div class="nested"> <div class="nested__elem nested__elem--red"> </div> </div> </div> '; $out = $this->bh->apply($bemjson); static::assertSame($testWithGreen, $out); static::assertTrue($this->bh->removeMatcherById([$list['green']->id])); static::assertFalse($this->bh->removeMatcherById([0, 1])); }
function testAttrEscape() { $this->assertEquals(BH::attrEscape('<b id="a">&</b>'), '<b id="a">&</b>'); }
public function testOne() { $this->assertTrue(true); return; $tree = [['label' => 'One', 'children' => [['label' => '1.1'], ['label' => '1.2', 'children' => [['label' => '1.2.1', 'red' => true], ['label' => '1.2.2']]]]]]; $data = ['foo' => 'bar', 'tree' => $tree]; $bemJson = ['block' => 'test', 'content' => [['block' => 'foo-tst', 'content' => "Hello, <?= \$data['foo'] ?>"], ['block' => 'nav', 'tag' => 'ul', 'recursive' => '$data["tree"]', 'itemTemplate' => ['elem' => 'item', 'tag' => 'li', 'content' => '<?= $item["label"] ?>'], 'wrapTemplate' => ['elem' => 'subnav', 'tag' => 'ul', 'mods' => ['nest' => '<?=$recursiveNestingLevel?>']], 'childrenAttribute' => 'children']]]; // $this->bh->match('$before', function (\BEM\Context $ctx, \BEM\Json $json) { if ($ctx->param('recursive') !== null && $ctx->param('itemTemplate') !== null) { $recursive = (string) $ctx->param('recursive'); $itemTemplate = $ctx->bh->apply($ctx->process($ctx->param('itemTemplate'))); $childrenAttribute = $ctx->param('childrenAttribute') ?: 'children'; $uniq = '$recursive_' . $ctx->generateId(); $wrapChildrenJson = $ctx->param('wrapTemplate') ?: ['bem' => false]; /** * @todo itemTemplate - контекстуальный или нет * @todo wrapTemplate - контекстуальный или нет(применять $ctx->process?) */ $wrapChildrenJson['content'] = "<?php {$uniq}(\$item['{$childrenAttribute}'], \$recursiveNestingLevel+1); ?>"; $goRecursive = $ctx->bh->apply($ctx->process($wrapChildrenJson)); $php = <<<PHP <?php // automatically generated function {$uniq} = function (\$iterator, \$recursiveNestingLevel = 1) use (&{$uniq}) { foreach (\$iterator as \$key => \$item) { ?>{$itemTemplate}<?php if (isset(\$item['{$childrenAttribute}'])) { // go recursive ?>{$goRecursive}<?php } } }; // run first loop {$uniq}({$recursive}); ?> PHP; $ctx->json()->content = $php; } }); // $processed = $this->bh->processBemJson($bemJson); $html = $this->bh->apply($bemJson); echo $html . "\n\n===========\n\n\n"; $dumped = var_export($data, true); file_put_contents('/tmp/c.php', <<<php <?php \$data = {$dumped}; ?> {$html} php ); include '/tmp/c.php'; echo "\n\n=11=====\n\n"; self::assertFalse(true); }
/** * @param string $fileName * * @return array */ public function loadMatchersFile($fileName) { return $this->bh->addMatcherList(include $fileName); }