Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit 5719845

Browse files
simplified to use if instead of switch which produces marginally smaller code
1 parent ad3a772 commit 5719845

File tree

2 files changed

+58
-68
lines changed

2 files changed

+58
-68
lines changed

js-modules/numberLines.js

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,37 @@ function numberLines(node, opt_startLineNum, isPreformatted) {
2424
var listItems = [li];
2525

2626
function walk(node) {
27-
switch (node.nodeType) {
28-
case 1: // Element
29-
if (nocode.test(node.className)) { break; }
30-
if ('br' === node.nodeName) {
31-
breakAfter(node);
32-
// Discard the <BR> since it is now flush against a </LI>.
33-
if (node.parentNode) {
34-
node.parentNode.removeChild(node);
35-
}
36-
} else {
37-
for (var child = node.firstChild; child; child = child.nextSibling) {
38-
walk(child);
39-
}
27+
var type = node.nodeType;
28+
if (type == 1 && !nocode.test(node.className)) { // Element
29+
if ('br' === node.nodeName) {
30+
breakAfter(node);
31+
// Discard the <BR> since it is now flush against a </LI>.
32+
if (node.parentNode) {
33+
node.parentNode.removeChild(node);
4034
}
41-
break;
42-
case 3: case 4: // Text
43-
if (isPreformatted) {
44-
var text = node.nodeValue;
45-
var match = text.match(lineBreak);
46-
if (match) {
47-
var firstLine = text.substring(0, match.index);
48-
node.nodeValue = firstLine;
49-
var tail = text.substring(match.index + match[0].length);
50-
if (tail) {
51-
var parent = node.parentNode;
52-
parent.insertBefore(
53-
document.createTextNode(tail), node.nextSibling);
54-
}
55-
breakAfter(node);
56-
if (!firstLine) {
57-
// Don't leave blank text nodes in the DOM.
58-
node.parentNode.removeChild(node);
59-
}
60-
}
35+
} else {
36+
for (var child = node.firstChild; child; child = child.nextSibling) {
37+
walk(child);
6138
}
62-
break;
39+
}
40+
} else if ((type == 3 || type == 4) && isPreformatted) { // Text
41+
var text = node.nodeValue;
42+
var match = text.match(lineBreak);
43+
if (match) {
44+
var firstLine = text.substring(0, match.index);
45+
node.nodeValue = firstLine;
46+
var tail = text.substring(match.index + match[0].length);
47+
if (tail) {
48+
var parent = node.parentNode;
49+
parent.insertBefore(
50+
document.createTextNode(tail), node.nextSibling);
51+
}
52+
breakAfter(node);
53+
if (!firstLine) {
54+
// Don't leave blank text nodes in the DOM.
55+
node.parentNode.removeChild(node);
56+
}
57+
}
6358
}
6459
}
6560

src/prettify.js

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -984,42 +984,37 @@ var REGEXP_PRECEDER_PATTERN = '(?:^^\\.?|[+-]|[!=]=?=?|\\#|%=?|&&?=?|\\(|\\*=?|[
984984
var listItems = [li];
985985

986986
function walk(node) {
987-
switch (node.nodeType) {
988-
case 1: // Element
989-
if (nocode.test(node.className)) { break; }
990-
if ('br' === node.nodeName) {
991-
breakAfter(node);
992-
// Discard the <BR> since it is now flush against a </LI>.
993-
if (node.parentNode) {
994-
node.parentNode.removeChild(node);
995-
}
996-
} else {
997-
for (var child = node.firstChild; child; child = child.nextSibling) {
998-
walk(child);
999-
}
987+
var type = node.nodeType;
988+
if (type == 1 && !nocode.test(node.className)) { // Element
989+
if ('br' === node.nodeName) {
990+
breakAfter(node);
991+
// Discard the <BR> since it is now flush against a </LI>.
992+
if (node.parentNode) {
993+
node.parentNode.removeChild(node);
1000994
}
1001-
break;
1002-
case 3: case 4: // Text
1003-
if (isPreformatted) {
1004-
var text = node.nodeValue;
1005-
var match = text.match(lineBreak);
1006-
if (match) {
1007-
var firstLine = text.substring(0, match.index);
1008-
node.nodeValue = firstLine;
1009-
var tail = text.substring(match.index + match[0].length);
1010-
if (tail) {
1011-
var parent = node.parentNode;
1012-
parent.insertBefore(
1013-
document.createTextNode(tail), node.nextSibling);
1014-
}
1015-
breakAfter(node);
1016-
if (!firstLine) {
1017-
// Don't leave blank text nodes in the DOM.
1018-
node.parentNode.removeChild(node);
1019-
}
1020-
}
995+
} else {
996+
for (var child = node.firstChild; child; child = child.nextSibling) {
997+
walk(child);
998+
}
999+
}
1000+
} else if ((type == 3 || type == 4) && isPreformatted) { // Text
1001+
var text = node.nodeValue;
1002+
var match = text.match(lineBreak);
1003+
if (match) {
1004+
var firstLine = text.substring(0, match.index);
1005+
node.nodeValue = firstLine;
1006+
var tail = text.substring(match.index + match[0].length);
1007+
if (tail) {
1008+
var parent = node.parentNode;
1009+
parent.insertBefore(
1010+
document.createTextNode(tail), node.nextSibling);
1011+
}
1012+
breakAfter(node);
1013+
if (!firstLine) {
1014+
// Don't leave blank text nodes in the DOM.
1015+
node.parentNode.removeChild(node);
10211016
}
1022-
break;
1017+
}
10231018
}
10241019
}
10251020

0 commit comments

Comments
 (0)