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.
6 * @typedef {{afterHighlight: string,
7 * beforeHighlight: string,
11 var ExtensionHighlight;
13 cr.define('extensions', function() {
17 * ExtensionCode is an element which displays code in a styled div, and is
18 * designed to highlight errors.
20 * @extends {HTMLDivElement}
22 function ExtensionCode(div) {
23 div.__proto__ = ExtensionCode.prototype;
27 ExtensionCode.prototype = {
28 __proto__: HTMLDivElement.prototype,
31 * Populate the content area of the code div with the given code. This will
32 * highlight the erroneous section (if any).
33 * @param {?ExtensionHighlight} code The 'highlight' strings represent the
34 * three portions of the file's content to display - the portion which
35 * is most relevant and should be emphasized (highlight), and the parts
36 * both before and after this portion. The title is the error message,
37 * which will be the mouseover hint for the highlighted region. These
39 * @param {string} emptyMessage The message to display if the code
40 * object is empty (e.g., 'could not load code').
42 populate: function(code, emptyMessage) {
43 // Clear any remnant content, so we don't have multiple code listed.
46 // If there's no code, then display an appropriate message.
48 (!code.highlight && !code.beforeHighlight && !code.afterHighlight)) {
49 var span = document.createElement('span');
50 span.classList.add('extension-code-empty');
51 span.textContent = emptyMessage;
52 this.appendChild(span);
56 var sourceDiv = document.createElement('div');
57 sourceDiv.classList.add('extension-code-source');
58 this.appendChild(sourceDiv);
61 var createSpan = function(source, isHighlighted) {
62 lineCount += source.split('\n').length - 1;
63 var span = document.createElement('span');
64 span.className = isHighlighted ? 'extension-code-highlighted-source' :
65 'extension-code-normal-source';
66 span.textContent = source;
70 if (code.beforeHighlight)
71 sourceDiv.appendChild(createSpan(code.beforeHighlight, false));
74 var highlightSpan = createSpan(code.highlight, true);
75 highlightSpan.title = code.message;
76 sourceDiv.appendChild(highlightSpan);
79 if (code.afterHighlight)
80 sourceDiv.appendChild(createSpan(code.afterHighlight, false));
82 // Make the line numbers. This should be the number of line breaks + 1
83 // (the last line doesn't break, but should still be numbered).
85 for (var i = 1; i < lineCount + 1; ++i)
87 var span = document.createElement('span');
88 span.textContent = content;
90 var linesDiv = document.createElement('div');
91 linesDiv.classList.add('extension-code-line-numbers');
92 linesDiv.appendChild(span);
93 this.insertBefore(linesDiv, this.firstChild);
97 * Clears the content of the element.
100 while (this.firstChild)
101 this.removeChild(this.firstChild);
105 * Scrolls to the error, if there is one. This cannot be called when the
108 scrollToError: function() {
109 var errorSpan = this.querySelector('.extension-code-highlighted-source');
111 this.scrollTop = errorSpan.offsetTop - this.clientHeight / 2;
117 ExtensionCode: ExtensionCode