Fix syntax class whitelist test

The syntax class whitelist is a feature that restricts the DOM elements
created for syntax highlighting only to classes which have CSS styles.
However, the test for this whitelist was being applied at the wrong
place, and would ignore whitelisted classes that were nested inside
non-whitelisted classes.

With this change, the location of the whitelist test is fixed, and a
unit test is added to encode the correct behavior.

Bug: Issue 4578
Change-Id: Ic3631b0fb44dc2c691069b8134ad88a55feb977a
This commit is contained in:
Wyatt Allen
2016-10-11 12:57:23 -07:00
parent 8f5d1b1f4e
commit 6c8fd1e781
2 changed files with 17 additions and 7 deletions

View File

@@ -255,13 +255,14 @@
var nodeLength = GrAnnotation.getLength(node);
// Note: HLJS may emit a span with class undefined when it thinks there
// may be a syntax error.
if (node.tagName === 'SPAN' && node.className !== 'undefined' &&
CLASS_WHITELIST.hasOwnProperty(node.className)) {
result.push({
start: offset,
length: nodeLength,
className: node.className,
});
if (node.tagName === 'SPAN' && node.className !== 'undefined') {
if (CLASS_WHITELIST.hasOwnProperty(node.className)) {
result.push({
start: offset,
length: nodeLength,
className: node.className,
});
}
if (node.children.length) {
result = result.concat(this._rangesFromElement(node, offset));
}

View File

@@ -370,6 +370,15 @@ limitations under the License.
assert.equal(result[1].className, className);
});
test('_rangesFromString whitelist allows recursion', function() {
var str = [
'<span class="non-whtelisted-class">',
'<span class="gr-diff gr-syntax gr-syntax-keyword">public</span>',
'</span>'].join('');
var result = element._rangesFromString(str);
assert.notEqual(result.length, 0);
});
test('_isSectionDone', function() {
var state = {sectionIndex: 0, lineIndex: 0};
assert.isFalse(element._isSectionDone(state));