Chromium Code Reviews| Index: chrome/renderer/resources/extension_process_bindings.js |
| diff --git a/chrome/renderer/resources/extension_process_bindings.js b/chrome/renderer/resources/extension_process_bindings.js |
| index 3eeb81d01c15872f103b8db4dda345c46b25ce67..f64aa1fe28ce36b4dcdeb697f313c6168f41cd58 100644 |
| --- a/chrome/renderer/resources/extension_process_bindings.js |
| +++ b/chrome/renderer/resources/extension_process_bindings.js |
| @@ -285,10 +285,75 @@ var chrome = chrome || {}; |
| }; |
| } |
| + // Parses the xml syntax supported by omnibox suggestion results. Returns an |
| + // object with two properties: 'description', which is just the text content, |
| + // and 'descriptionStyles', which is an array of style objects in a format |
| + // understood by the C++ backend. |
| + function parseOmniboxDescription(input) { |
| + var domParser = new DOMParser(); |
| + |
| + // The XML parser requires a single top-level element, but we want to |
| + // support things like 'hello, <match>world</match>!'. So we wrap the |
| + // provided text in generated root level element. |
| + var root = domParser.parseFromString( |
| + '<fragment>' + input + '</fragment>', 'text/xml'); |
| + |
| + // DOMParser has a terrible error reporting facility. Errors come out nested |
| + // inside the returned document. |
| + var error = root.querySelector('parsererror div'); |
| + if (error) { |
| + throw new Error(error.textContent); |
| + } |
| + |
| + // Otherwise, it's valid, so build up the result. |
| + var result = { |
| + description: '', |
| + descriptionStyles: [] |
| + }; |
| + |
| + // Recursively walk the tree. |
| + (function(node) { |
| + for (var i = 0, child; child = node.childNodes[i]; i++) { |
| + // Append text nodes to our description. |
| + if (child.nodeType == Node.TEXT_NODE) { |
| + result.description += child.nodeValue; |
| + continue; |
| + } |
| + |
| + // Process and descend into a subset of recognized tags. |
| + if (child.nodeType == Node.ELEMENT_NODE && |
| + (child.nodeName == 'dim' || child.nodeName == 'match' || |
| + child.nodeName == 'url')) { |
| + var style = { |
| + 'type': child.nodeName, |
| + 'offset': result.description.length |
| + }; |
| + result.descriptionStyles.push(style); |
| + arguments.callee(child); |
| + style.length = result.description.length - style.offset; |
| + continue; |
| + } |
| + |
| + // Descend into all other nodes, even if they are unrecognized, for |
| + // forward compat. |
| + arguments.callee(child); |
| + } |
| + })(root); |
| + |
| + return result; |
| + } |
| + |
| function setupOmniboxEvents(extensionId) { |
| chrome.omnibox.onInputChanged.dispatch = |
| function(text, requestId) { |
| - var suggestCallback = function(suggestions) { |
| + var suggestCallback = function(userSuggestions) { |
| + var suggestions = []; |
| + for (var i = 0; i < userSuggestions.length; i++) { |
|
Matt Perry
2010/11/29 22:06:52
maybe we should run the sendSuggestions validator
Aaron Boodman
2010/11/30 01:13:57
Done.
|
| + var parseResult = parseOmniboxDescription( |
| + userSuggestions[i].description); |
| + parseResult.content = userSuggestions[i].content; |
| + suggestions.push(parseResult); |
| + } |
| chrome.omnibox.sendSuggestions(requestId, suggestions); |
| }; |
| chrome.Event.prototype.dispatch.apply(this, [text, suggestCallback]); |
| @@ -640,6 +705,12 @@ var chrome = chrome || {}; |
| return id; |
| }; |
| + apiFunctions["omnibox.setDefaultSuggestion"].handleRequest = |
| + function(details) { |
| + var parseResult = parseOmniboxDescription(details.description); |
| + sendRequest(this.name, [parseResult], this.definition.parameters); |
| + }; |
| + |
| apiFunctions["contextMenus.create"].customCallback = |
| function(name, request, response) { |
| if (chrome.extension.lastError) { |
| @@ -703,17 +774,6 @@ var chrome = chrome || {}; |
| return newArgs; |
| }; |
| - apiFunctions["omnibox.styleUrl"].handleRequest = function(offset, length) { |
| - return {type: "url", offset: offset, length: length}; |
| - }; |
| - apiFunctions["omnibox.styleMatch"].handleRequest = |
| - function(offset, length) { |
| - return {type: "match", offset: offset, length: length}; |
| - }; |
| - apiFunctions["omnibox.styleDim"].handleRequest = function(offset, length) { |
| - return {type: "dim", offset: offset, length: length}; |
| - }; |
| - |
| if (chrome.test) { |
| chrome.test.getApiDefinitions = GetExtensionAPIDefinition; |
| } |