Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Fix for Issue 3339 #3340

Merged
merged 7 commits into from
Apr 9, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 41 additions & 33 deletions src/extensions/default/HtmlEntityCodeHints/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ define(function (require, exports, module) {
* The encoded string
*/
function _encodeValue(value) {
return (value.indexOf("#") === -1) ? value.replace("&", "&") : value.replace("&", "&").replace("#", "#");
return value.replace("&", "&").replace("#", "#");
}

/**
Expand All @@ -59,7 +59,7 @@ define(function (require, exports, module) {
* The decoded string
*/
function _decodeValue(value) {
return value.replace("#", "#").replace("&", "&").replace("&#59;", ";");
return value.replace("&", "&").replace("#", "#");
}

/**
Expand Down Expand Up @@ -89,14 +89,8 @@ define(function (require, exports, module) {
*/
SpecialCharHints.prototype.hasHints = function (editor, implicitChar) {
this.editor = editor;

var query = this._getQuery();

if (implicitChar === null) {
return query !== null;
}

return implicitChar === "&" || query !== null;
return this._getQuery() !== null;
};

/**
Expand All @@ -120,16 +114,19 @@ define(function (require, exports, module) {
var query,
result;

if (this.primaryTriggerKeys.indexOf(implicitChar) !== -1 || implicitChar === null) {
if (implicitChar === null || this.primaryTriggerKeys.indexOf(implicitChar) !== -1) {
this.currentQuery = query = this._getQuery();
result = $.map(specialChars, function (value, index) {
if (value.indexOf(query) === 0) {
var shownValue = _encodeValue(value);
return shownValue + "&#59; <span class='entity-display-character'>" + value + ";</span>";
return shownValue + "; <span class='entity-display-character'>" + value + ";</span>";
}
}).sort(this._internalSort);

query = _encodeValue(query);
if (query !== null) {
query = _encodeValue(query);
}

return {
hints: result,
match: query,
Expand Down Expand Up @@ -157,7 +154,7 @@ define(function (require, exports, module) {
var num1 = parseInt(a.slice(a.indexOf("#") + 1, a.length - 1), 10),
num2 = parseInt(b.slice(b.indexOf("#") + 1, b.length - 1), 10);

return (num1 === num2) ? 0 : (num1 > num2) ? 1 : -1;
return (num1 - num2);
}

return a.localeCompare(b);
Expand All @@ -171,32 +168,34 @@ define(function (require, exports, module) {
*/
SpecialCharHints.prototype._getQuery = function () {
var query,
lineContent,
lineContentBeforeCursor,
startChar,
endChar;
endChar,
cursor = this.editor.getCursorPos();

query = "&";
if (HTMLUtils.getTagInfo(this.editor, cursor).tagName !== "") {
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name lineContent should be more accurate so code is easier to understand, so rename to something like lineContentBeforeCursor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

}

lineContent = this.editor.document.getRange({
line: this.editor.getCursorPos().line,
lineContentBeforeCursor = this.editor.document.getRange({
line: cursor.line,
ch: 0
}, this.editor.getCursorPos());
}, cursor);

startChar = lineContent.lastIndexOf("&");
endChar = lineContent.lastIndexOf(";");
startChar = lineContentBeforeCursor.lastIndexOf("&");
endChar = lineContentBeforeCursor.lastIndexOf(";");

if (endChar < startChar) {
query = this.editor.document.getRange({
line: this.editor.getCursorPos().line,
ch: startChar
}, this.editor.getCursorPos());
}

if (startChar !== -1 && HTMLUtils.getTagInfo(this.editor, this.editor.getCursorPos()).tagName === "") {
return query;
// If no startChar was found or the endChar is greater than the startChar then it is no entity
if (startChar === -1 || endChar > startChar) {
return null;
}

return null;

query = this.editor.document.getRange({
line: cursor.line,
ch: startChar
}, cursor);

return query;
};

/**
Expand All @@ -212,11 +211,20 @@ define(function (require, exports, module) {
SpecialCharHints.prototype.insertHint = function (completion) {
var start = {line: -1, ch: -1},
end = {line: -1, ch: -1},
cursor = this.editor.getCursorPos();
cursor = this.editor.getCursorPos(),
match,
matchSemicolonPos;

end.line = start.line = cursor.line;
start.ch = cursor.ch - this.currentQuery.length;
match = this.editor.document.getLine(cursor.line).slice(cursor.ch);
matchSemicolonPos = match.indexOf(";");
end.ch = start.ch + this.currentQuery.length;

if (matchSemicolonPos !== -1 && /^(#*[0-9]+)|([a-zA-Z]+)$/.test(match.slice(0, matchSemicolonPos))) {
end.ch = this.editor.document.getLine(cursor.line).indexOf(";", start.ch) + 1;
}

completion = completion.slice(0, completion.indexOf(" "));
completion = _decodeValue(completion);
if (start.ch !== end.ch) {
Expand Down