Fail nicely with a malformed language definition, play by Crockford's rules
This commit is contained in:
@@ -107,11 +107,12 @@
|
|||||||
* Used by the definitions in langs/ to register language definitions.
|
* Used by the definitions in langs/ to register language definitions.
|
||||||
*/
|
*/
|
||||||
$.GollumEditor.defineLanguage = function( language_name, languageObject ) {
|
$.GollumEditor.defineLanguage = function( language_name, languageObject ) {
|
||||||
if ( typeof languageObject == 'object' )
|
if ( typeof languageObject == 'object' ) {
|
||||||
LanguageDefinition.define( language_name, languageObject );
|
LanguageDefinition.define( language_name, languageObject );
|
||||||
else
|
} else {
|
||||||
debug('GollumEditor.defineLanguage: definition for ' + language_name
|
debug('GollumEditor.defineLanguage: definition for ' + language_name +
|
||||||
+ ' is not an object');
|
' is not an object');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -123,8 +124,8 @@
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
var debug = function(m) {
|
var debug = function(m) {
|
||||||
if ( ActiveOptions.Debug
|
if ( ActiveOptions.Debug &&
|
||||||
&& typeof console != 'undefined' ) {
|
typeof console != 'undefined' ) {
|
||||||
console.log( m );
|
console.log( m );
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -164,15 +165,19 @@
|
|||||||
LanguageDefinition.loadFor( name, function(x, t) {
|
LanguageDefinition.loadFor( name, function(x, t) {
|
||||||
if ( t != 'success' ) {
|
if ( t != 'success' ) {
|
||||||
debug('Failed to load language definition for ' + name);
|
debug('Failed to load language definition for ' + name);
|
||||||
|
// well, fake it and turn everything off for this one
|
||||||
|
LanguageDefinition.define( name, {} );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// update features that rely on the language definition
|
// update features that rely on the language definition
|
||||||
if ( EditorHas.functionBar() )
|
if ( EditorHas.functionBar() ) {
|
||||||
FunctionBar.refresh();
|
FunctionBar.refresh();
|
||||||
|
}
|
||||||
|
|
||||||
if ( LanguageDefinition.isValid() && EditorHas.formatSelector() )
|
if ( LanguageDefinition.isValid() && EditorHas.formatSelector() ) {
|
||||||
FormatSelector.updateSelected();
|
FormatSelector.updateSelected();
|
||||||
|
}
|
||||||
|
|
||||||
} );
|
} );
|
||||||
} else {
|
} else {
|
||||||
@@ -236,12 +241,15 @@
|
|||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
isLoadedFor: function( markup_name ) {
|
isLoadedFor: function( markup_name ) {
|
||||||
if ( LanguageDefinition._LOADED_LANGS.length == 0 ) return false;
|
if ( LanguageDefinition._LOADED_LANGS.length === 0 ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
for ( var i=0; i < LanguageDefinition._LOADED_LANGS.length; i++ ) {
|
for ( var i=0; i < LanguageDefinition._LOADED_LANGS.length; i++ ) {
|
||||||
if ( LanguageDefinition._LOADED_LANGS[i] == markup_name )
|
if ( LanguageDefinition._LOADED_LANGS[i] == markup_name ) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -406,8 +414,9 @@
|
|||||||
$(this).click( FunctionBar.evtFunctionButtonClick );
|
$(this).click( FunctionBar.evtFunctionButtonClick );
|
||||||
$(this).removeClass('disabled');
|
$(this).removeClass('disabled');
|
||||||
}
|
}
|
||||||
else if ( $(this).attr('id') != 'function-help' )
|
else if ( $(this).attr('id') != 'function-help' ) {
|
||||||
$(this).addClass('disabled');
|
$(this).addClass('disabled');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// show bar as active
|
// show bar as active
|
||||||
@@ -487,11 +496,11 @@
|
|||||||
// remove any excess backreferences from the replace string
|
// remove any excess backreferences from the replace string
|
||||||
rt = rt.replace( /\$[\d]/g, '' );
|
rt = rt.replace( /\$[\d]/g, '' );
|
||||||
repText = rt;
|
repText = rt;
|
||||||
} else if ( repText == '' ) {
|
} else if ( repText === '' ) {
|
||||||
debug('Search string is empty');
|
debug('Search string is empty');
|
||||||
|
|
||||||
// find position of $1 - this is where we will place the cursor
|
// find position of $1 - this is where we will place the cursor
|
||||||
var cursor = rt.indexOf('$1');
|
cursor = rt.indexOf('$1');
|
||||||
|
|
||||||
// we have an empty string, so just remove backreferences
|
// we have an empty string, so just remove backreferences
|
||||||
repText = rt.replace( /\$[\d]/g, '' );
|
repText = rt.replace( /\$[\d]/g, '' );
|
||||||
@@ -513,9 +522,10 @@
|
|||||||
repText += definitionObject.append;
|
repText += definitionObject.append;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( repText )
|
if ( repText ) {
|
||||||
FunctionBar.replaceFieldSelection( $('#gollum-editor-body'),
|
FunctionBar.replaceFieldSelection( $('#gollum-editor-body'),
|
||||||
repText, reselect, cursor );
|
repText, reselect, cursor );
|
||||||
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -548,10 +558,13 @@
|
|||||||
// 2 characters in TextRange.
|
// 2 characters in TextRange.
|
||||||
var s = start;
|
var s = start;
|
||||||
var lb = 0;
|
var lb = 0;
|
||||||
|
var i;
|
||||||
debug('IE: start position is currently ' + s);
|
debug('IE: start position is currently ' + s);
|
||||||
for ( var i=0; i < s; i++ )
|
for ( i=0; i < s; i++ ) {
|
||||||
if ( el.value.charAt(i).match(/\r/) )
|
if ( el.value.charAt(i).match(/\r/) ) {
|
||||||
++lb;
|
++lb;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( lb ) {
|
if ( lb ) {
|
||||||
debug('IE start: compensating for ' + lb + ' line breaks');
|
debug('IE start: compensating for ' + lb + ' line breaks');
|
||||||
@@ -560,9 +573,11 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
var e = end;
|
var e = end;
|
||||||
for ( var i=0; i < e; i++ )
|
for ( i=0; i < e; i++ ) {
|
||||||
if ( el.value.charAt(i).match(/\r/) )
|
if ( el.value.charAt(i).match(/\r/) ) {
|
||||||
++lb;
|
++lb;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( lb ) {
|
if ( lb ) {
|
||||||
debug('IE end: compensating for ' + lb + ' line breaks');
|
debug('IE end: compensating for ' + lb + ' line breaks');
|
||||||
@@ -592,8 +607,8 @@
|
|||||||
if ( $field.length ) {
|
if ( $field.length ) {
|
||||||
selPos = FunctionBar.getFieldSelectionPosition( $field );
|
selPos = FunctionBar.getFieldSelectionPosition( $field );
|
||||||
selStr = $field.val().substring( selPos.start, selPos.end );
|
selStr = $field.val().substring( selPos.start, selPos.end );
|
||||||
debug('Selected: ' + selStr + ' (' + selPos.start + ', '
|
debug('Selected: ' + selStr + ' (' + selPos.start + ', ' +
|
||||||
+ selPos.end + ')');
|
selPos.end + ')');
|
||||||
return selStr;
|
return selStr;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@@ -609,8 +624,9 @@
|
|||||||
if ( LanguageDefinition.isValid() ) {
|
if ( LanguageDefinition.isValid() ) {
|
||||||
$('#gollum-editor-function-bar a.function-button').unbind('click');
|
$('#gollum-editor-function-bar a.function-button').unbind('click');
|
||||||
FunctionBar.activate();
|
FunctionBar.activate();
|
||||||
if ( Help )
|
if ( Help ) {
|
||||||
Help.setActiveHelp( LanguageDefinition.getActiveLanguage() );
|
Help.setActiveHelp( LanguageDefinition.getActiveLanguage() );
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
debug('Language definition is invalid.');
|
debug('Language definition is invalid.');
|
||||||
if ( FunctionBar.isShown() ) {
|
if ( FunctionBar.isShown() ) {
|
||||||
@@ -639,7 +655,7 @@
|
|||||||
var fullStr = $field.val();
|
var fullStr = $field.val();
|
||||||
var selectNew = true;
|
var selectNew = true;
|
||||||
if ( reselect === false) {
|
if ( reselect === false) {
|
||||||
var selectNew = false;
|
selectNew = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var scrollTop = null;
|
var scrollTop = null;
|
||||||
@@ -819,12 +835,14 @@
|
|||||||
|
|
||||||
// go go inefficient algorithm
|
// go go inefficient algorithm
|
||||||
for ( var i=0; i < helpData.length; i++ ) {
|
for ( var i=0; i < helpData.length; i++ ) {
|
||||||
if ( typeof helpData[i] != 'object' ) break;
|
if ( typeof helpData[i] != 'object' ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
var $newLi = $('<li><a href="#" rel="' + i + '">' +
|
var $newLi = $('<li><a href="#" rel="' + i + '">' +
|
||||||
helpData[i].menuName + '</a></li>');
|
helpData[i].menuName + '</a></li>');
|
||||||
$('#gollum-editor-help-parent').append( $newLi );
|
$('#gollum-editor-help-parent').append( $newLi );
|
||||||
if ( i == 0 ) {
|
if ( i === 0 ) {
|
||||||
// select on first run
|
// select on first run
|
||||||
$newLi.children('a').addClass('selected');
|
$newLi.children('a').addClass('selected');
|
||||||
}
|
}
|
||||||
@@ -851,7 +869,9 @@
|
|||||||
$('#gollum-editor-help-list').html('');
|
$('#gollum-editor-help-list').html('');
|
||||||
$('#gollum-editor-help-content').html('');
|
$('#gollum-editor-help-content').html('');
|
||||||
for ( var i=0; i < subData.content.length; i++ ) {
|
for ( var i=0; i < subData.content.length; i++ ) {
|
||||||
if ( typeof subData.content[i] != 'object' ) break;
|
if ( typeof subData.content[i] != 'object' ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
var $subLi = $('<li><a href="#" rel="' + index + ':' + i + '">' +
|
var $subLi = $('<li><a href="#" rel="' + index + ':' + i + '">' +
|
||||||
subData.content[i].menuName + '</a></li>');
|
subData.content[i].menuName + '</a></li>');
|
||||||
@@ -986,8 +1006,8 @@
|
|||||||
*/
|
*/
|
||||||
evtHelpButtonClick: function( e ) {
|
evtHelpButtonClick: function( e ) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if ( Help.isShown() ) Help.hide();
|
if ( Help.isShown() ) { Help.hide(); }
|
||||||
else Help.show();
|
else { Help.show(); }
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
@@ -1003,7 +1023,7 @@
|
|||||||
evtParentMenuClick: function( e ) {
|
evtParentMenuClick: function( e ) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
// short circuit if we've selected this already
|
// short circuit if we've selected this already
|
||||||
if ( $(this).hasClass('selected') ) return;
|
if ( $(this).hasClass('selected') ) { return; }
|
||||||
|
|
||||||
// populate from help data for this
|
// populate from help data for this
|
||||||
var helpIndex = $(this).attr('rel');
|
var helpIndex = $(this).attr('rel');
|
||||||
@@ -1026,7 +1046,7 @@
|
|||||||
*/
|
*/
|
||||||
evtSubMenuClick: function( e ) {
|
evtSubMenuClick: function( e ) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if ( $(this).hasClass('selected') ) return;
|
if ( $(this).hasClass('selected') ) { return; }
|
||||||
|
|
||||||
// split index rel data
|
// split index rel data
|
||||||
var rawIndex = $(this).attr('rel').split(':');
|
var rawIndex = $(this).attr('rel').split(':');
|
||||||
@@ -1044,7 +1064,7 @@
|
|||||||
$.GollumEditor.Dialog = $.GollumDialog;
|
$.GollumEditor.Dialog = $.GollumDialog;
|
||||||
$.GollumEditor.replaceSelection = function( repText ) {
|
$.GollumEditor.replaceSelection = function( repText ) {
|
||||||
FunctionBar.replaceFieldSelection( $('#gollum-editor-body'), repText );
|
FunctionBar.replaceFieldSelection( $('#gollum-editor-body'), repText );
|
||||||
}
|
};
|
||||||
|
|
||||||
// Placeholder exists as its own thing now
|
// Placeholder exists as its own thing now
|
||||||
$.GollumEditor.Placeholder = $.GollumPlaceholder;
|
$.GollumEditor.Placeholder = $.GollumPlaceholder;
|
||||||
|
|||||||
Reference in New Issue
Block a user