var _ = require("underscore")
/**
 * Returns a unique selector from the root element
 * to el  
 *
 * I tried memoizing selectors and it didn't give much
 * of a performance gain, so I got rid of it.
 **/
function getSelector(el, root) {
  var id 
    , className
    , name

  if(el.is(root))
    return ""

  if(id = el.attr("id")) {
    name = "#" + id
  } else {
    className = (el.attr("class") || "")
      .replace(/#\{.*\}/g, "")
      .replace(/(^\s+|\s+$)/g, "")

    if(className) {
      name = "." + className.replace(/\s+/, ".")

      if($(el).parent().children(name).length !== 1)
        name = el[0].tagName.toLowerCase()
    } else {
      name = el[0].tagName.toLowerCase()
    }
  }

  var siblingsAndSelf = el.parent().children()
    , baseOneIndex = (el.parent().children().index(el) + 1)
    , selector = name
    , recur

  if(siblingsAndSelf.length > 1)
    selector += ":nth-child(" + baseOneIndex + ")"

  if(recur = getSelector(el.parent(), root))
    selector = recur + " > " + selector

  return selector
}

function trim(str) {
  return (str || "").replace(/^\s+|\s+$/g, "") 
}

function jqts(element) {
  return $("<div>").append(element.clone()).html()
}

function findDescendantsAndSelf(element, selector) {
  if(!selector)
    return element

  var matches = element.find(selector)
  if(!matches.length) {
    matches = matches.filter(selector)
  }
  return matches
}

function get(obj, key) {
  var val
  if(!obj) return
  if(typeof obj.get === "function") {
    return obj.get(key)
  } else {
    return obj[key]
  }
}

module.exports = {
  findDescendantsAndSelf: findDescendantsAndSelf, 
  getSelector: getSelector,
  get: get,
  trim: trim,
  jqts: jqts
}

@@ -86,18 +86,16 @@
  * to el
-  
 %0A *%0A * I
@@ -237,17 +237,16 @@
   var id
- 
 %0A    , c
@@ -322,16 +322,36 @@
 tr(%22id%22)
+ && !id.match(/#%7B/g)
 ) %7B%0A    
@@ -1117,17 +1117,16 @@
 $/g, %22%22)
- 
 %0A%7D%0A%0Afunc
@@ -1636,17 +1636,16 @@
 AndSelf,
- 
 %0A  getSe

Fix for selectors when id attribute is dynamic