Skip to content

Commit

Permalink
OPTIMISATION - changed the routine that replaces non-CDATA wrapped, i…
Browse files Browse the repository at this point in the history
…n-parameter line breaks to prevent XML stripping them.

Shorter code and faster - uses split/replace/join rather than previous method of string mapping/parsing.

Saving 50-100 milliseconds on a bloated 560 test object...

git-svn-id: https://xerteonlinetoolkits.googlecode.com/svn/trunk@1062 912cdd6b-5c7d-d5a7-a2ba-d0f0cdb91641
  • Loading branch information
JohnSmith-LT committed Jul 25, 2013
1 parent 4fba41e commit d456e1c
Showing 1 changed file with 16 additions and 49 deletions.
65 changes: 16 additions & 49 deletions modules/xerte/parent_templates/Nottingham/common_html5/js/xenith.js
Expand Up @@ -119,58 +119,25 @@ $(document).ready(function() {

// replace all line breaks in attributes with ascii code - otherwise these are replaced with spaces when parsed to xml
function x_fixLineBreaks(text) {
var indexAttr = [], // contains objects [startIndex, endIndex] of attributes
indexCData = [], // contains objects [startIndex, endIndex] of CDATA
start = true,
pos = text.indexOf('<![CDATA['),
i, len;

while(pos > -1) { // find all CDATA and ignore them when searching for attributes
if (start == true) {
indexCData.push({start:pos});
start = false;
pos = text.indexOf(']]>', pos+1);
}
else {
indexCData[indexCData.length - 1].end = pos;
start = true;
pos = text.indexOf('<![CDATA[', pos+1);
}
}
start = true;
pos = text.indexOf('"');
while(pos > -1) {
var attribute = true;
for (i=0, len=indexCData.length; i<len; i++) {
if (indexCData[i].start < pos && indexCData[i].end > pos) {
attribute = false; // ignore as in CDATA
}
}
if (attribute == true) {
if (start == true) {
start = false;
indexAttr.push({start:pos});
} else {
start = true;
indexAttr[indexAttr.length-1].end = pos;
}
var split_up = text.split(/<\!\[CDATA\[|\]\]>/),
temp, i, j, len, len2;

for (i=0, len=split_up.length; i<len; i+=2) {
temp = split_up[i].split('"');
for (j=1, len2=temp.length; j<len2; j+=2) {
temp[j] = temp[j].replace(/(\n|\r|\r\n)/g, "&#10;");
}
pos = text.indexOf('"', pos+1);
split_up[i] = temp.join('"');
}

var newString = "";
for (i=0, len=indexAttr.length; i<len; i++) {
if (i == 0) {
newString += text.substring(0, indexAttr[i].start);
} else {
newString += text.substring(indexAttr[i - 1].end, indexAttr[i].start);
}
newString += text.substring(indexAttr[i].start, indexAttr[i].end).replace(/(\n|\r|\r\n)/g, "&#10;");
if (i == indexAttr.length - 1) {
newString += text.substring(indexAttr[i].end, text.length);
}

// Put the CDATA blocks back...
temp = [];
for (i=0, len=split_up.length-1; i<len; i+=2) {
temp.push(split_up[i] + "<![CDATA[" + split_up[i+1]);
}
return newString;
temp.push(split_up[i]);

return temp.join("]]>");
}

// function gets data from language file
Expand Down

0 comments on commit d456e1c

Please sign in to comment.