refactor HTMLUtils.findStyleBlocks to find arbitrary blocks.

The js code hinting code needed to find script blocks, so refactor the style block code to find any kind of block, and call that instead of having another copy of the code.  findStyleBlocks now just calls the generic method, looking for "css" blocks.

Also fixed up the block finding code to handle empty blocks, which I ran into when looking for "javascript" blocks.
This commit is contained in:
Erik Tierney committed 2013-05-03 11:29:43 -04:00
1 parent 046cb6bb63
commit 5d7dc76dfb
2 files changed
+52 -66

No files matched your search

@@ -481,41 +481,10 @@ define(function (require, exports, module) {
// <script> tags
var text = "",
offset = this.getOffset(),
ctx = TokenUtils.getInitialContext(this.editor._codeMirror, {line: 0, ch: 0 }),
tokenModeName,
inScriptBlock = false,
currentScriptBlock,
scriptBlocks = [],
cursor = this.getCursor(),
editor = this.editor;
editor = this.editor,
scriptBlocks = HTMLUtils.findBlocks(editor, "javascript");
// Loop over the tokens in the html file, and extract the blocks
// that are the javascript code
while (TokenUtils.moveNextToken(ctx)) {
tokenModeName = TokenUtils.getModeAt(ctx.editor, ctx.pos).name;
if (inScriptBlock) {
// Check for end of this <script> block
if (tokenModeName !== "javascript") {
if (!currentScriptBlock.end) {
// Handle empty script blocks
currentScriptBlock.end = currentScriptBlock.start;
}
currentScriptBlock.text = editor.document.getRange(currentScriptBlock.start, currentScriptBlock.end);
inScriptBlock = false;
} else {
currentScriptBlock.end = { line: ctx.pos.line, ch: ctx.pos.ch };
}
} else {
// Check for start of a <script> block
if (tokenModeName === "javascript") {
currentScriptBlock = {
start: { line: ctx.pos.line, ch: ctx.pos.ch }
};
scriptBlocks.push(currentScriptBlock);
inScriptBlock = true;
}
}
}
// Add all the javascript text
// For non-javascript blocks we replace everything except for newlines
// with whitespace. This is so that the offset and cursor positions