Skip to content

Commit 22841a1

Browse files
committed
Reduced complexity of Syntax.innerText function.
1 parent e385854 commit 22841a1

File tree

1 file changed

+12
-38
lines changed

1 file changed

+12
-38
lines changed

source/jquery.syntax.core.js

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,18 @@ if (!String.prototype.repeat) {
2020
};
2121
}
2222

23-
// The jQuery version of container.text() is broken on IE6.
24-
// This version fixes it... for pre elements only. Other elements
25-
// in IE will have the whitespace manipulated.
26-
Syntax.innerText = function (elems) {
27-
var cdata = '', elem;
23+
Syntax.innerText = function(element) {
24+
var text;
2825

29-
(function (elems) {
30-
for (var i = 0; elems[i]; i++) {
31-
elem = elems[i];
32-
33-
// Get the text from text nodes and CDATA nodes
34-
if (elem.nodeType === 3 || elem.nodeType === 4) {
35-
cdata += elem.nodeValue;
36-
37-
// Use textContent || innerText for elements
38-
} else if (elem.nodeType === 1) {
39-
if (elem.nodeName.toUpperCase() == 'BR')
40-
cdata += "\n";
41-
else if (typeof(elem.innerText) === 'string')
42-
cdata += elem.innerText;
43-
//else if (typeof(elem.textContent) === 'string')
44-
// cdata += elem.textContent;
45-
else
46-
arguments.callee(elem.childNodes);
47-
48-
// If we encounter a <div>, this must be a complete line.
49-
// So we normalise this back to whitespace:
50-
if (elem.nodeName.toUpperCase() == 'DIV' && cdata[cdata.length-1] != '\n')
51-
cdata += '\n';
52-
53-
// Traverse everything else, except comment nodes
54-
} else if (elem.nodeType !== 8) {
55-
arguments.callee(elem.childNodes);
56-
}
57-
}
58-
})(elems);
26+
if (element.nodeName == 'BR') {
27+
return '\n';
28+
} else if (document.body.innerText) {
29+
text = element.innerText;
30+
} else {
31+
text = element.innerHTML.replace(/<br\/?>/gi,'\n').replace(/<[^>]+>/gi, "");
32+
}
5933

60-
return cdata.replace(/\r\n?/g, '\n');
34+
return text.replace(/\r\n?/g, '\n');
6135
}
6236

6337
// Convert to stack based implementation
@@ -74,7 +48,7 @@ Syntax.extractElementMatches = function (elems, offset, tabWidth) {
7448
offset += elem.nodeValue.length;
7549

7650
} else if (elem.nodeType === 1) {
77-
var text = Syntax.innerText(elem.childNodes);
51+
var text = Syntax.innerText(elem);
7852
var expr = {klass: elem.className, force: true, element: elem};
7953

8054
matches.push(new Syntax.Match(offset, text.length, expr, text));
@@ -975,7 +949,7 @@ Syntax.highlight = function (elements, options, callback) {
975949
// We can augment the plain text to extract existing annotations (e.g. <span class="foo">...</span>).
976950
options.matches = options.matches.concat(Syntax.extractElementMatches(container));
977951

978-
var text = Syntax.innerText(container);
952+
var text = Syntax.innerText(this);
979953

980954
var match = text.match(/-\*- mode: (.+?);(.*?)-\*-/i);
981955
var endOfSecondLine = text.indexOf("\n", text.indexOf("\n") + 1);

0 commit comments

Comments
 (0)