public function set($name, $value = null) { if (is_array($name)) { foreach ($name as $n => $v) { $this->set($n, $v); } } elseif (2 === func_num_args()) { if (method_exists($this, $name)) { $this->{$name}($value); } else { $this->setOption($name, $value); } } else { if ($firstSpacePos = strpos($name, ' ')) { $stringOpt = substr($name, $firstSpacePos + 1); $name = substr($name, 0, $firstSpacePos); // DMS STYLE - string opt in name dmString::retrieveOptFromString($stringOpt, $this->options); } // JQUERY STYLE - css expression dmString::retrieveCssFromString($name, $this->options); } return $this; }
public function tag($tagName, $opt = array(), $content = false, $openAndClose = true) { if (!($tagName = trim($tagName))) { return ''; } $tagOpt = array(); // separate tag name from attribues in $tagName if ($firstSpacePos = strpos($tagName, ' ')) { $tagNameOpt = substr($tagName, $firstSpacePos + 1); $tagName = substr($tagName, 0, $firstSpacePos); // DMS STYLE - string opt in name dmString::retrieveOptFromString($tagNameOpt, $tagOpt); } // JQUERY STYLE - css expression dmString::retrieveCssFromString($tagName, $tagOpt); // ARRAY STYLE - array opt if (is_array($opt) && !empty($opt)) { if (isset($opt['json'])) { $tagOpt['class'][] = json_encode($opt['json']); unset($opt['json']); } if (isset($opt['class'])) { $tagOpt['class'][] = is_array($opt['class']) ? implode(' ', $opt['class']) : $opt['class']; unset($opt['class']); } $tagOpt = array_merge($tagOpt, $opt); } elseif (is_string($opt) && $content) { $opt = sfToolkit::stringToArray($opt); if (isset($opt['class'])) { $tagOpt['class'][] = explode(' ', $opt['class']); unset($opt['class']); } $tagOpt = array_merge($tagOpt, $opt); } if (!$content) { if (!is_array($opt)) { $content = $opt; } else { $content = null; } } $class = isset($tagOpt['class']) ? $tagOpt['class'] : array(); if ($this->options['use_beaf'] && (in_array('beafh', $class) || in_array('beafv', $class))) { $isBeaf = true; $tagOpt['class'][] = 'clearfix'; $beafTag = in_array($tagName, array('span', 'a', 'p')) ? 'span' : 'div'; } else { $isBeaf = false; } if (isset($tagOpt['lang'])) { if ($tagOpt['lang'] === $this->context->getUser()->getCulture()) { unset($tagOpt['lang']); } } if (isset($tagOpt['class']) && is_array($tagOpt['class'])) { $tagOpt['class'] = implode(' ', array_unique($tagOpt['class'])); } $optHtml = ''; foreach ($tagOpt as $key => $val) { $optHtml .= ' ' . $key . '="' . htmlentities($val, ENT_COMPAT, 'UTF-8') . '"'; } if (in_array($tagName, $this->options['empty_elements'])) { $tag = '<' . $tagName . $optHtml . ' />'; } elseif ($openAndClose) { if ($isBeaf) { $tag = '<' . $tagName . $optHtml . '><' . $beafTag . ' class="beafore"></' . $beafTag . '><' . $beafTag . ' class="beafin">' . $content . '</' . $beafTag . '><' . $beafTag . ' class="beafter"></' . $beafTag . '></' . $tagName . '>'; } else { $tag = '<' . $tagName . $optHtml . '>' . $content . '</' . $tagName . '>'; } } else { if ($isBeaf) { $tag = '<' . $tagName . $optHtml . '><' . $beafTag . ' class="beafore"></' . $beafTag . '><' . $beafTag . ' class="beafin">'; } else { $tag = '<' . $tagName . $optHtml . '>'; } } return $tag; }
$opt = array('aa' => 'bb'); dmString::retrieveOptFromString($string, $opt); $t->is_deeply($opt, array('aa' => 'bb', 'x' => 'y'), '::retrieveOptFromString() merges the options'); $t->is_deeply($string, '', '::retrieveOptFromString() sets the string parameter to an empty string'); // string overwrites opt $t->diag(' ::retrieveOptFromString() overwriting'); $string = 'x=string'; $opt = array('x' => 'opt'); dmString::retrieveOptFromString($string, $opt); $t->is_deeply($opt, array('x' => 'string'), '::retrieveOptFromString() string has the precedence over opt'); // ::retrieveCssFromString() $t->diag('::retrieveCssFromString'); $cssFromStringsTests = array(array('', array(), '', array(), 'empty string'), array('#an_id', array(), '', array('id' => 'an_id'), 'one id only'), array('#an_id', array('id' => 'old'), '', array('id' => 'an_id'), 'id in opts is overridden'), array('.a_class', array(), '', array('class' => array('a_class')), 'one class only'), array('.a_class.another_class', array(), '', array('class' => array('a_class', 'another_class')), 'multiple classes'), array('#an_id.a_class', array(), '', array('id' => 'an_id', 'class' => array('a_class')), 'an id and a class'), array('#an_id.a_class href="/page"', array(), ' href="/page"', array('id' => 'an_id', 'class' => array('a_class')), 'garbage string after'), array('href="/page" a#an_id.a_class', array(), 'href="/page" a#an_id.a_class', array(), 'garbage string before'), array('#an_id alt="I am. Are you?"', array(), ' alt="I am. Are you?"', array('id' => 'an_id'), 'dots are not taken into account if not classes'), array('#an_id.imaclass alt="I am. Are you?"', array(), ' alt="I am. Are you?"', array('id' => 'an_id', 'class' => array('imaclass')), 'dots are not taken into account if not classes'), array('#an_id.imaclass alt="I am. Are you? and.imaclass"', array(), ' alt="I am. Are you? and.imaclass"', array('id' => 'an_id', 'class' => array('imaclass')), 'dots are not taken into account if not classes, and a class with same name exists'), array('.cls href="#anchor"', array(), ' href="#anchor"', array('class' => array('cls')), '# are not taken into account if not ids'), array('.cls href="page#anchor"', array(), ' href="page#anchor"', array('class' => array('cls')), '# are not taken into account if not ids, even if they have text before')); foreach ($cssFromStringsTests as $cssFromStringsTest) { list($str, $opts, $expectedStr, $expectedOpts, $msg) = $cssFromStringsTest; dmString::retrieveCssFromString($str, $opts); $t->comment(' ::retrieveCssFromString() ' . $msg); $t->is_deeply($str, $expectedStr, '::retrieveCssFromString() ' . $msg . ': testing resulting string'); $t->is_deeply($opts, $expectedOpts, '::retrieveCssFromString() ' . $msg . ': testing resulting opts'); } // ::toArray() $t->diag('::toArray()'); $t->is_deeply(dmString::toArray($arr = array('some' => 'array')), $arr, '::toArray() with an array returns the array'); $t->is_deeply(dmString::toArray(''), array(), '::toArray() with an empty string returns an empty array'); $t->is_deeply(dmString::toArray('#an_id.a_class.another_class'), array('id' => 'an_id', 'class' => array('a_class', 'another_class')), '::toArray() jquery style'); $t->is_deeply(dmString::toArray('an_option=a_value other_option=other_value'), array('an_option' => 'a_value', 'other_option' => 'other_value'), '::toArray() symfony style'); $t->is_deeply(dmString::toArray('#an_id.a_class.another_class an_option=a_value other_option=other_value'), array('id' => 'an_id', 'class' => array('a_class', 'another_class'), 'an_option' => 'a_value', 'other_option' => 'other_value'), '::toArray() with jquery AND symfony styles'); $t->is_deeply(dmString::toArray('#jquery id=symfony'), array('id' => 'symfony'), '::toArray() symfony style has precedence over jquery style'); $t->is_deeply(dmString::toArray('#an_id.a_class.another_class href=page#anchor'), array('id' => 'an_id', 'class' => array('a_class', 'another_class'), 'href' => 'page#anchor'), '::toArray() if a symfony style option contains a #'); $t->is_deeply(dmString::toArray('#an_id.a_class.another_class an_option=a_value other_option=other_value', true), array('id' => 'an_id', 'class' => 'a_class another_class', 'an_option' => 'a_value', 'other_option' => 'other_value'), '::toArray() with implodeClasses = true'); $t->is_deeply(dmString::toArray('action="http://site.com/url"'), array('action' => 'http://site.com/url'), 'correctly extract action');