Guest User

Untitled

a guest
Jul 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. /**
  2. * Method for taking a string formatted as a css selector and breaking it down into id/classes/attributes/in-line styles
  3. * to use in the creatin of an element. I.E. "#id.class.class2[attribute=value]{ border: 1px solid blue; }"
  4. *
  5. * @ignore this is the original regex i wrote, which was awesome, but broke on some edge cases ...
  6. * "!(\#(.+?)(\.|\[|\{)){1,}!" => ' id="$2" $3', //ID
  7. * "!(\.(.*?)(\[|\{)){1,}!" => ' class="$2" $3', //CLASS
  8. * "!\[(.*?)=([^\[]*)\]!" => ' $1="$2" ', //ATTRS
  9. * "!\{(.*)\}!" => ' style="$1" ', //INLINE STYLE
  10. * "!\.([a-zA-Z_]+[\w\-]*)!" => ' $1', //SPECIFIC CLASSES
  11. *
  12. */
  13. protected static function selectors($str){
  14. $selectors = $attrs = $styles = "";
  15.  
  16. //grab everything before the first attr "[" or the first style "{" as square brackets are not used in ids/classes
  17. $selectors = substr($str, 0, strcspn($str, "[{"));
  18. if(!empty($selectors)){
  19. //if an id is declared via "#" grab it
  20. if(strpos($selectors, "#") !== false){
  21. // $attrs["id"] = sprintf('id="%s"', trim(substr($selectors, 0, strcspn($selectors, ".")), "#"));
  22. $attrs["id"] = trim(substr($selectors, 0, strcspn($selectors, ".")), "#");
  23. }
  24. //if there are classes declared via "."
  25. if(strpos($selectors, ".") !== false){
  26. // $classes = sprintf('class="%s"', substr($selectors, strcspn($selectors, ".")));
  27. $classes = trim(strtr(substr($selectors, strcspn($selectors, ".")), '.', ' '));
  28. // $attrs["class"] = sprintf('class="%s"', $classes);
  29. $attrs["class"] = $classes;
  30. }
  31. }
  32.  
  33. //break up the sumbitted string by the first "[" and the last "]" as square brackets are not used in in-line styles or ids/classes
  34. $attributes_string = substr($str, strpos($str, "["), (strrpos($str, "]") - strpos($str, "[") + 1));
  35. if(!empty($attributes_string)){
  36. //break the isolated attrs string by the "keys" i.e. "[key=" because in JSON text must be quoted and html arrays shouln't use "="'s in key names
  37. $attributes_array = preg_split("!\[([\w]+?)=!", $attributes_string, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  38. if(!empty($attributes_array) && is_array($attributes_array)){
  39. //loop through our string pieces using the value of one as the key and the value of the next as the value
  40. while( ($key = current($attributes_array)) && ($value = next($attributes_array)) ){
  41. //our values will have a trailing "]" and we can't use trim() because our value might actually end in its own "]"
  42. // $attrs[$key] = sprintf('%s="%s"', $key, str_replace('"', '\"', substr($value, 0, (strlen($value) - 1))));
  43. $attrs[$key] = substr($value, 0, (strlen($value) - 1));
  44. //advance the array so we're not using previous values as current keys
  45. next($attributes_array);
  46. }
  47. }
  48. }
  49.  
  50. //styles are easy as we simply breaking the main string at the LAST "{"
  51. // $attrs["style"] = sprintf('style="%s"', trim(strrchr($str, "{"), " {}"));
  52. $attrs["style"] = trim(strrchr($str, "{"), " {}");
  53.  
  54. return $attrs;
  55. // return implode(" ", $attrs);
  56. }
Add Comment
Please, Sign In to add comment