Closed Bug 1173770 Opened 9 years ago Closed 8 years ago

Provide an easy way to use createFixupURI to determine if a word is a search term or URL

Categories

(Core :: DOM: Navigation, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: mkaply, Unassigned)

Details

(Keywords: dev-doc-complete)

We're looking at trying to emulate the behavior of Firefox with relation to determining if something should be searched or opened as a URL (handling protocols, quotes, dots, etc.)

createFixupURI does this, but your only choice to get the full functionality is to pass FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP which will generate an actual keyword URL.

It would be nice if there was a way to call this function and have it return a URL if it fixes up to a URL and null if it will result in a keyword search. Right now the only way to accomplish this (that I could find) is something hacky like this:

var fakeURI = Services.uriFixup.createFixupURI("NOONEWILLEVERSEARCHONTHIS", Services.uriFixup.FIXUP_FLAG_USE_UTF8 | Services.uriFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP);
var splitFakeURI = fakeURI.spec.split("NOONEWILLEVERSEARCHONTHIS");
var u = Services.uriFixup.createFixupURI(term, Services.uriFixup.FIXUP_FLAG_USE_UTF8 | Services.uriFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP);
  if (u) {
    var url = u.spec.replace(splitFakeURI[0], "").replace(splitFakeURI[1], "");
    if (url == u.spec) {
      // It's a URL, load ir
    } else {
      // It's a search term, search on it.
    }
  }
  // It's a search term, search on it.
we just removed that flag in bug 1107883 cause it was introducing mistakes in the result.
But I guess you might do something like we do in UnifiedComplete.js (see the patch there)
(In reply to Marco Bonardo [::mak] from comment #1)
> we just removed that flag in bug 1107883 cause it was introducing mistakes
> in the result.
> But I guess you might do something like we do in UnifiedComplete.js (see the
> patch there)

That was great advice. Something as simple as this works:

  var flags = Ci.nsIURIFixup.FIXUP_FLAG_FIX_SCHEME_TYPOS |
             Ci.nsIURIFixup.FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP;
  var fixupInfo = null;
  try {
    fixupInfo = Services.uriFixup.getFixupURIInfo(term, flags);
  } catch (e) {
    // If fixup failed, it's definitely not a URL
    return null;
  }
  // If keywordAsSent is set, the term got turned into a keyword
  if (fixupInfo.keywordAsSent)  {
    return null;
  }
  // I couldn't find a case where fixedURI wouldn't be set, but
  // just to be safe
  if (fixupInfo.fixedURI) {
    return fixupInfo.fixedURI.spec;  
  }
  return null;

I'm going to move this to a doc bug since getFixupURIInfo appears to not be documented.
Keywords: dev-doc-needed
Added to:

https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIURIFixup
Status: NEW → RESOLVED
Closed: 8 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.