Revised version to establish htmlTagArray

Uses a method from Marijn Haverbeke to establish an array of nested
tokens
This means chars in the document don't need to be scanned and the array
worked out in a more complex way
It means quite a few lines can be condensed down to just 3
Also means it's far less expensive on the CPU, meaning there is no
sluggishness onCursorActivity on large documents (ie typing etc)
This commit is contained in:
Matt Pass
2012-09-28 17:35:42 +01:00
parent e0a7893578
commit c717dd7bba

View File

@@ -108,71 +108,17 @@ var ICEcoder = {
// Work out the nesting depth location on demand and update our display if required
getNestLocation: function(updateNestDisplay) {
var cM, openTag, nestCheck, startPos, tagStart, canDoTheEndTag, tagEnd, tagEndJS, fileName;
var cM, state, nestCheck, startPos, fileName;
cM = ICEcoder.getcMInstance();
// Set up array to store nest data
ICEcoder.htmlTagArray = [];
state = cM.getTokenAt(cM.getCursor()).state, ICEcoder.htmlTagArray = [];
for (var cx = state.curState.context; cx; cx = cx.prev) ICEcoder.htmlTagArray.unshift(cx.tagName);
nestCheck = cM.getValue();
// Set up array to store nest data, a var to establish if a tag is open and another to establish if we're in a code block
ICEcoder.htmlTagArray = [], openTag = false, ICEcoder.codeBlock = false;
// For every character from the start to our caret position
for(var i=0;i<=ICEcoder.caretPos;i++) {
// If we find a < tag and we're not within a tag, change the open tag state & set our start position
if(nestCheck.charAt(i)=="<" && openTag==false) {
openTag=true;
startPos=i+1;
// Get the tag name and if it's the start of a code block, set the var for that
tagStart=nestCheck.substr(startPos,nestCheck.length).split(" ")[0].split(">")[0].split("\n")[0];
if (tagStart=="script"||tagStart=="?php"||tagStart=="?"||tagStart=="%") {ICEcoder.codeBlock=true}
if (tagStart!="") {ICEcoder.tagStart=tagStart}
};
// If we find a > tag and we're within a tag or codeblock
if(nestCheck.charAt(i)==">" && (openTag||ICEcoder.codeBlock)) {
// Get the tag name
tagString=nestCheck.substr(0,i);
tagString=tagString.substr(tagString.lastIndexOf('<')+1,tagString.length);
tagString=tagString.split(" ")[0];
ICEcoder.tagString = tagString;
canDoTheEndTag=true;
// Check it's not on our list of exceptions
if (ICEcoder.tagNestExceptions.indexOf(tagString)>-1) {
canDoTheEndTag=false;
}
if (canDoTheEndTag) {
// Get this end tag name
tagEnd=nestCheck.substr(0,i);
tagEndJS=tagEnd.substr(tagEnd.lastIndexOf('<'));
tagEnd=tagEnd.substr(tagEnd.lastIndexOf('<')+1);
tagEnd=tagEnd.substr(tagEnd.lastIndexOf(' ')+1);
tagEnd=tagEnd.substr(tagEnd.lastIndexOf('\t')+1);
tagEnd=tagEnd.substr(tagEnd.lastIndexOf('\n')+1);
tagEnd=tagEnd.substr(tagEnd.lastIndexOf(';')+1);
if (!ICEcoder.codeBlock) {
// OK, we can do something further as we're not in a code block
// If it's the same as the previously logged tag preceeded by /, it's the equivalent end tag
// so remove the last logged tag, thereby going up one in the nest, otherwise it's a different tag, add it to the end
tagEnd=="/"+ICEcoder.htmlTagArray[ICEcoder.htmlTagArray.length-1] ? ICEcoder.htmlTagArray.pop() : ICEcoder.htmlTagArray.push(tagString);
} else if (
((ICEcoder.tagStart=="script"||ICEcoder.tagStart=="/script")&&tagEndJS=="</script")||
((ICEcoder.tagStart=="?php"||ICEcoder.tagStart=="?")&&tagEnd=="?")||
(ICEcoder.tagStart=="%"&&tagEnd=="%")) {
ICEcoder.codeBlock=false;
}
}
// Reset our open tag state ready for next time
openTag=false;
}
}
// Now we've built up our nest depth array, if we're due to show it in the display
if (updateNestDisplay && !top.ICEcoder.dontUpdateNest) {
// Clear the display