diff --git a/lib/ace/document.js b/lib/ace/document.js index 4bc170c2..0b621b31 100644 --- a/lib/ace/document.js +++ b/lib/ace/document.js @@ -46,6 +46,11 @@ var Document = function(text) { if (Array.isArray(text)) { this.insertLines(0, text); + } + // There has to be one line at least in the document. If you pass an empty + // string to the insert function, nothing will happen. Workaround. + else if (text.length == 0) { + this.$lines = [""]; } else { this.insert({row: 0, column:0}, text); } @@ -57,7 +62,7 @@ var Document = function(text) { this.setValue = function(text) { var len = this.getLength(); - this.remove(new Range(0, 0, len, this.getLine(len-1).length)); + this.remove(new Range(0, 0, len, this.getLine(len-1).length)); this.insert({row: 0, column:0}, text); }; @@ -262,16 +267,16 @@ var Document = function(text) { var firstRow = range.start.row; var lastRow = range.end.row; - if (range.isMultiLine()) { + if (range.isMultiLine()) { var firstFullRow = range.start.column == 0 ? firstRow : firstRow + 1; var lastFullRow = lastRow - 1; - + if (range.end.column > 0) this.removeInLine(lastRow, 0, range.end.column); - + if (lastFullRow >= firstFullRow) this.removeLines(firstFullRow, lastFullRow); - + if (firstFullRow != firstRow) { this.removeInLine(firstRow, range.start.column, this.$lines[firstRow].length); this.removeNewLine(range.start.row); diff --git a/lib/ace/test/document_test.js b/lib/ace/test/document_test.js index cdc78732..7b9c625c 100644 --- a/lib/ace/test/document_test.js +++ b/lib/ace/test/document_test.js @@ -279,19 +279,24 @@ var Test = { doc.replace(new Range(0, 0, 2, 1), ["4", "5", "6"].join("\n")); assert.equal(["4", "5", "6"].join("\n"), doc.getValue()); }, - + "test: set value": function() { var doc = new Document("1"); assert.equal("1", doc.getValue()); - + doc.setValue(doc.getValue()); assert.equal("1", doc.getValue()); - + var doc = new Document("1\n2"); assert.equal("1\n2", doc.getValue()); - + doc.setValue(doc.getValue()); assert.equal("1\n2", doc.getValue()); + }, + + "test: empty document has to contain one line": function() { + var doc = new Document(""); + assert.equal(doc.$lines.length, 1); } };