   dom.A = ->
     props = @props
     if @props.href
+      href = @props.href
 
       # Earl will call a click handler that the programmer passes in
-      onClick = @props.onClick or (-> null)
+      onClick = @props.onClick or (-> null)      
+
+      # Earl rewrites the address when necessary 
+      internal_link = !href.match('//') || !!href.match(location.origin)
+      is_mailto = !!href.toLowerCase().match('mailto')
+      if internal_link && !is_mailto
+        if href.substring(0, Earl.root.length) != Earl.root
+          rel = href[0] != '/'
+          if rel 
+            href = loc.path + '/' + href 
+          else  
+            href = Earl.root + href 
+          href = @props.href = href.replace('//', '/')
+
+
 
       handle_click = (event) =>
-        href = @props.href
-
-        internal_link = !href.match('//') || !!href.match(location.origin)
-        is_mailto = !!href.toLowerCase().match('mailto')
         opened_in_new_tab = event.altKey  || \
                             event.ctrlKey || \
                             event.metaKey || \
                             event.shiftKey
-        
+
+
         # In his wisdom, Earl sometimes just lets the default behavior occur
-        if !internal_link || opened_in_new_tab || is_mailto \
-           || @props.target == '_blank'
+        if !internal_link || opened_in_new_tab || is_mailto || @props.target == '_blank'
           onClick event
 
         # ... but other times Earl is history aware
         else 
           event.preventDefault()
           event.stopPropagation()
+
           Earl.load_page href
           onClick event
 

  # Earl, don't fall asleep on the job!
  react_to_location()

if window.addEventListener
  window.addEventListener 'load', onload, false 
else if window.attachEvent
  window.attachEvent 'onload', onload




  search = location.search?.replace(/\%2[fF]/g, '/')
  loc = location.pathname?.replace(/\%20/g, ' ')

  "#{loc}#{search}#{location.hash}"

url_from_statebus = ->
  loc = fetch 'location'

  url = loc.path or '/'

  if loc.query_params && Object.keys(loc.query_params).length > 0
    query_params = ("#{k}=#{v}" for own k,v of loc.query_params)
    url += "?#{query_params.join('&')}" 
  if loc.hash?.length > 0
    url += "##{loc.hash}"

  url


# For handling device-specific annoyances
