Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 cr.define('extensions', function() { | |
| 6 'use strict'; | |
| 7 | |
| 8 /** | |
| 9 * An object which holds util methods used by more than one extensions module. | |
| 10 */ | |
| 11 var ExtensionUtil = {}; | |
| 12 | |
|
not at google - send to devlin
2014/04/25 20:15:52
util is a bit of a misnomer at the moment since it
Devlin
2014/04/25 20:48:40
Done.
| |
| 13 /** | |
| 14 * Clear all the content of a given element. | |
| 15 * @param {HTMLElement} element The element to be cleared. | |
| 16 */ | |
| 17 ExtensionUtil.clearElement = function(element) { | |
| 18 while (element.firstChild) | |
| 19 element.removeChild(element.firstChild); | |
| 20 }; | |
| 21 | |
| 22 /** | |
| 23 * Populate the content area of the code div with the given code. This will | |
| 24 * highlight the erroneous section (if any). | |
| 25 * @param {HTMLDivElement} div The div element to populate. | |
| 26 * @param {Object} code An object with four strings: beforeHighlight, | |
| 27 * afterHighlight, highlight, and the message. The 'highlight' strings | |
| 28 * represent the three portions of the file's content to display - the | |
| 29 * portion which is most relevant and should be emphasized (highlight), | |
| 30 * and the parts both before and after this portion. The message is the | |
| 31 * error message, which will be the mouseover hint for the highlighted | |
| 32 * region. These may be empty. | |
| 33 * @param {string} emptyMessage The message to post to the div if the code | |
| 34 * object is empty (e.g., 'could not load code'). | |
| 35 */ | |
| 36 ExtensionUtil.populateCodeDiv = function(div, code, emptyMessage) { | |
| 37 // Clear any remnant content, so we don't have multiple code listed. | |
| 38 ExtensionUtil.clearElement(div); | |
| 39 | |
| 40 var sourceDiv = document.createElement('div'); | |
| 41 sourceDiv.classList.add('extension-code-source'); | |
| 42 div.appendChild(sourceDiv); | |
| 43 | |
| 44 // If there's no code, then display an appropriate message. | |
| 45 if (!code) { | |
| 46 var span = document.createElement('span'); | |
| 47 span.textContent = emptyMessage; | |
| 48 sourceDiv.appendChild(span); | |
| 49 return; | |
| 50 } | |
| 51 | |
| 52 var lineCount = 0; | |
| 53 var createSpan = function(source, isHighlighted) { | |
| 54 lineCount += source.split('\n').length - 1; | |
| 55 var span = document.createElement('span'); | |
| 56 span.className = isHighlighted ? 'extension-code-highlighted-source' : | |
| 57 'extension-code-normal-source'; | |
| 58 span.textContent = source; | |
| 59 return span; | |
| 60 }; | |
| 61 | |
| 62 if (code.beforeHighlight) | |
| 63 sourceDiv.appendChild(createSpan(code.beforeHighlight, false)); | |
| 64 | |
| 65 if (code.highlight) { | |
| 66 var highlightSpan = createSpan(code.highlight, true); | |
| 67 highlightSpan.title = code.message; | |
| 68 sourceDiv.appendChild(highlightSpan); | |
| 69 } | |
| 70 | |
| 71 if (code.afterHighlight) | |
| 72 sourceDiv.appendChild(createSpan(code.afterHighlight, false)); | |
| 73 | |
| 74 // Make the line numbers. This should be the number of line breaks + 1 | |
| 75 // (the last line doesn't break, but should still be numbered). | |
| 76 var content = ''; | |
| 77 for (var i = 1; i < lineCount + 1; ++i) | |
| 78 content += i + '\n'; | |
| 79 var span = document.createElement('span'); | |
| 80 span.textContent = content; | |
| 81 | |
| 82 var linesDiv = document.createElement('div'); | |
| 83 linesDiv.classList.add('extension-code-line-numbers'); | |
| 84 linesDiv.appendChild(span); | |
| 85 div.insertBefore(linesDiv, div.firstChild); | |
| 86 | |
| 87 // Ensure the div is visible. | |
| 88 div.hidden = false; | |
| 89 }; | |
| 90 | |
| 91 // Export | |
| 92 return { | |
| 93 ExtensionUtil: ExtensionUtil | |
| 94 }; | |
| 95 }); | |
| OLD | NEW |