Content deleted Content added
rv to 0.9.144a (October 28, 2014) adding multiple spaces bug |
0.9.145 (November 06, 2014) added nbsp support, preview code polishing |
||
Line 14:
// ==UserScript==
// @name wikEd
// @version 0.9.
// @date
// @namespace https://en.wikipedia.org/wiki/User:Cacycle/
// @description A full-featured in-browser editor for Wikipedia and other MediaWikis
Line 367:
'thinSpace': 'Thin space',
'ideographicSpace': 'Ideographic space',
'nbSpace': 'Non-breaking space',
// highlighting
Line 472 ⟶ 473:
'logo': '6/67/WikEd_logo.png',
'minusDash': 'b/ba/WikEd_minus_dash.png',
'nbSpace': '3/37/WikEd_nb_space.png',
'noFile': '8/88/WikEd_no_file.png',
'nowiki': '5/5a/WikEd_nowiki.png',
Line 782 ⟶ 784:
'.wikEdEnSpace': 'background-image: url({wikEdImage:enSpace}); background-position: bottom left; background-repeat: no-repeat; margin: 0 1px; padding: 0 3px;',
'.wikEdThinSpace': 'background-image: url({wikEdImage:thinSpace}); background-position: bottom left; background-repeat: no-repeat; margin: 0 1px; padding: 0 3px;',
'.wikEdIdeographicSpace': 'background-image: url({wikEdImage:ideographicSpace}); background-position: bottom left; background-repeat: no-repeat; margin: 0 1px; padding: 0 3px;',
'.wikEdNbSpace': 'background-image: url({wikEdImage:nbSpace}); background-position: top left; background-repeat: no-repeat;'
});
};
Line 908 ⟶ 911:
// input wrapper
'.wikEdInputWrapper': 'position: relative
'.wikEdFullscreen .wikEdInputWrapper ': 'position: fixed; margin-top: 0; top: 0; left: 0; right: 0; background: #f0f0f0;',
'body.wikEdFullscreen': 'overflow: hidden;',
Line 1,341 ⟶ 1,344:
if (wikEd.config.frameBackgroundColor === undefined) { wikEd.config.frameBackgroundColor = false; }
// fix buttons convert nbsp to character entity
if (wikEd.config.
return;
Line 1,636 ⟶ 1,639:
wikEd.frameScrollTop = null;
wikEd.textareaUpdated = true;
wikEd.buttonKeyCode = [];
wikEd.direction = null;
Line 1,898 ⟶ 1,900:
// redirect WED shortcut to wikEd.Debug(objectName, object, popup)
// extract version info from wikEd.Meta()
Line 2,636 ⟶ 2,638:
if (
section !== null &&
section.length > 0
) {
wikEd.editSection = section[0].value;
Line 7,803 ⟶ 7,804:
// textify, not changing obj.html
wikEd.Textify(obj.changed);
wikEd.paste.last = 'textify';
Line 7,810:
wikEd.WikifyHTML(obj.changed, false);
obj.changed.html = obj.changed.html.replace(/\s*<br\b[^>]*>\s*/g, '\n');
// move content before br after paste at end of line, part 2
Line 8,313 ⟶ 8,312:
// highlight the syntax
obj.html = obj.changed.plain;
obj.html = obj.html.replace(/(\xa0)/g, '<span class="wikEdNbSpace">$1</span><!--wikEdNbSpace-->');
if (highlightSyntax === true) {
if (obj.changed.from == 'whole') {
Line 8,323 ⟶ 8,324:
// at least highlight tab characters
else {
obj.html = obj.html.replace(/(\xa0)/g, '<span class="wikEdNbSpace">$1</span><!--wikEdNbSpace-->');
obj.html = obj.html.replace(/(\t)/g, '<span class="wikEdTabPlain">$1</span><!--wikEdTabPlain-->');
}
Line 8,491 ⟶ 8,493:
wikEd.previewDiff.style.display = 'none';
wikEd.localPrevWrapper.style.display = 'block';
// prepare ajax preview
var bodyData = wikEd.textarea.value;
if ( wikEd.config.useAjaxPreview === true ) {
Line 8,613 ⟶ 8,599:
}
// GeSHi syntax highlighting support
// CSS is only provided dynamically and not for Live preview // request a full preview and attach CSS to page, remember already loaded languages
if ( wikEd.loader === false ) {
var regExp = /<(syntaxhighlight|source)\b[^>]*?lang\s*=\s*("|')(\w+)\2/gi;
var regExpMatch;
while ( (regExpMatch = regExp.exec(bodyData)) !== null) { var lang = regExpMatch[3];
if (wikEd.syntaxHighlightTagCSS['wikEd' + lang] === undefined) {
livePreview = false;
wikEd.syntaxHighlightTagCSS['wikEd' + lang] = true;
break;
}
}
}
Line 8,628 ⟶ 8,617:
// make the AJAX request
wikEd.AjaxPreview( bodyData, wikEd.LocalPreviewAjaxHandler, livePreview );
}
return;
};
//
// wikEd.AjaxPreview: get rendered page text using an Ajax non-API POST call
//
wikEd.AjaxPreview = function (textValue, ResponseHandler, livePreview, format ) {
// get format, json preferred for module updates and summary parsing
if ( format === undefined ) {
format = 'json';
}
if ( format === 'json' && typeof JSON !== 'object' ) {
format = 'xml';
}
// API request
if ( ( wikEd.wikiGlobals.wgEnableAPI === true || wikEd.wikiGlobals.wgEnableAPI === 'true' ) && livePreview === true ) {
// get summary
var summary = '';
if (wikEd.summaryText !== null) {
summary = wikEd.summaryText.value;
}
// set request parameters
var postFields = {
'format': format,
'action': 'parse',
'prop': 'text|modules',
'disablepp': '1',
'pst': '1',
'title': wikEd.pageName,
'text': textValue,
'summary': summary
};
var requestUrl = wikEd.scriptURL + 'api.php';
// AJAX API request
wikEd.AjaxRequest( 'POST', requestUrl, postFields, 'text/plain', ResponseHandler );
}
// legacy support: non-API request, use Live preview if possible
else {
// prepare the url
var requestUrl;
if ( wikEd.editForm !== null && wikEd.editUpload !== true && wikEd.editWatchlist !== true && wikEd.viewDeleted !== true ) {
requestUrl = wikEd.editForm.action.replace( /\?.*()/, '' );
if ( /:\/\/()/.test(requestUrl) === false ) {
requestUrl = window.___location.protocol + '//' + window.___location.host + requestUrl;
}
}
else if ( wikEd.wikiGlobals.wgScriptPath !== undefined ) {
requestUrl = wikEd.wikiGlobals.wgScriptPath + '/index.php';
}
else {
requestUrl = window.___location.href;
requestUrl = requestUrl.replace( /\?.*()/, '' );
requestUrl = requestUrl.replace( /\/[\w\.]*$/, '/index.php' );
}
// prepare the form fields
var postFields = {};
if ( wikEd.pageName !== null && wikEd.wikiGlobals.wgCanonicalNamespace != 'Special' ) {
postFields['title'] = wikEd.pageName;
}
else {
postFields['title'] = 'wikEd_preview';
}
postFields['action'] = 'submit';
postFields['wpTextbox1'] = textValue;
if ( wikEd.starttime !== null ) {
postFields['wpStarttime'] = wikEd.starttime;
}
if ( wikEd.edittime !== null ) {
postFields['wpEdittime'] = wikEd.edittime;
}
if ( wikEd.editToken !== null ) {
postFields['wpEditToken'] = wikEd.editToken;
}
if ( wikEd.autoSummary !== null ) {
postFields['wpAutoSummary'] = wikEd.autoSummary;
}
postFields['wpPreview'] = 'true';
if (livePreview !== false) {
postFields['live'] = 'true';
}
// AJAX non-API request
wikEd.AjaxRequest( 'POST', requestUrl, postFields, 'text/plain', ResponseHandler );
}
return;
Line 8,639 ⟶ 8,723:
wikEd.LocalPreviewAjaxHandler = function ( ajax ) {
// get preview html
var html = '';
//
var responseObj = JSON.parse( ajax.responseText );
if ( responseObj.parse !== undefined ) {
var parse = responseObj.parse;
html = parse.text['*'];
// load module js and css
if ( wikEd.loader === true && parse.modules !== undefined ) {
var modules = parse.modules.concat(
parse.modulescripts, parse.modulestyles, parse.modulemessages
);
window.mw.loader.load( modules );
}
// add heading to new section
if ( parse.parsedsummary !== undefined && wikEd.editSection === 'new') {
html = '<h2>' + parse.parsedsummary['*'] + '</h2>' + html;
}
}
}
// xml API reponse
else if (
html = wikEd.StringGetInnerHTML(
.replace( /</g, '<' )
.replace( />/g, '>' )
Line 8,653 ⟶ 8,757:
}
// non-API livepreview (https://www.mediawiki.org/wiki/Manual:Live_preview)
else if (
html = wikEd.StringGetInnerHTML(
.replace( /</g, '<' )
.replace( />/g, '>' )
Line 8,667 ⟶ 8,771:
// full preview page
else {
html = ajax.responseText;
// attach <style> stylesheet declarations to document (<source>, <syntaxhighlight>)
Line 8,702 ⟶ 8,807:
);
wikEd.previewArticle.innerHTML = html;
// apply dynamic changes to preview
// init sortable tables (wikibits.js)
if ( typeof window.sortables_init === 'function' ) {
window.sortables_init();
}
// init collapsible tables (common.js)
if ( typeof window.createCollapseButtons === 'function' ) {
window.createCollapseButtons();
}
// collapsible divs (common.js on ru)
if (typeof window.collapsibleDivs === 'function' ) {
window.collapsibleDivs();
}
// collapsible tables (common.js on ru)
if (typeof window.collapsibleTables === 'function' ) {
window.collapsibleTables();
}
// load MathJax modules and render math elements
if ( wikEd.loader === true ) {
if ( $( '.tex' ).length > 0 && window.mw.user.options.get( 'math' ) === '6' ) {
window.mw.loader.using( 'ext.math.mathjax.enabler', function () {
$( '#wikiPreview' ).renderTex();
} );
}
}
// load media player modules and activate media elements, see https://bugzilla.wikimedia.org/show_bug.cgi?id=72675
if ( wikEd.loader === true ) {
window.mw.loader.load( ['mw.MwEmbedSupport', 'mw.EmbedPlayer', 'mw.PopUpMediaTransform'] );
}
Line 8,767 ⟶ 8,898:
wikEd.GetArticleTextAjaxHandler = function ( ajax ) {
// get response
Line 9,696 ⟶ 9,825:
obj.whole.code = obj.whole.html;
wikEd.RemoveHighlightingWikify(obj.whole, wikify);
}
}
Line 9,719 ⟶ 9,847:
obj.selection.code = obj.selection.html;
wikEd.RemoveHighlightingWikify(obj.selection, wikify);
}
}
Line 9,754 ⟶ 9,881:
obj.focusWord.code = obj.focusWord.html;
wikEd.RemoveHighlightingWikify(obj.focusWord, wikify);
// get the wikified plain text for the line under the cursor
Line 9,761 ⟶ 9,887:
obj.focusLine.code = obj.focusLine.html;
wikEd.RemoveHighlightingWikify(obj.focusLine, wikify);
// get the wikified plain text for the paragraph under the cursor
Line 9,768 ⟶ 9,893:
obj.focusPara.code = obj.focusPara.html;
wikEd.RemoveHighlightingWikify(obj.focusPara, wikify);
}
}
Line 9,805 ⟶ 9,929:
obj.selectionWord.code = obj.selectionWord.html;
wikEd.RemoveHighlightingWikify(obj.selectionWord, wikify);
// get the wikified plain text for the lines under the selection
Line 9,812 ⟶ 9,935:
obj.selectionLine.code = obj.selectionLine.html;
wikEd.RemoveHighlightingWikify(obj.selectionLine, wikify);
// get the wikified plain text for the paragraph under the selection
Line 9,819 ⟶ 9,941:
obj.selectionPara.code = obj.selectionPara.html;
wikEd.RemoveHighlightingWikify(obj.selectionPara, wikify);
}
}
Line 9,877 ⟶ 9,998:
if (useRegExp !== true) {
found = wikEd.frameWindow.find(findText, caseSensitive, backwards, wrap, false, true, false);
if (found === true) {
Line 10,288 ⟶ 10,409:
obj.plain = obj.plain.replace(/<\/?(table|caption)\b[^>]*>/g, '\x00');
obj.plain = obj.plain.replace(/<\/(tr|th|td)>/g, '\x00');
// nbsp
obj.plain = obj.plain.replace(/<span\b[^>]*?\bclass="wikEdNbSpace"[^>]*>((.|\n)*?)<\/span>(<!--wikEdNbSpace-->)?/gi,
function(p, p1, p2) {
p1 = p1.replace(/ /g, '\xa0');
return p1;
}
);
// finish html to plain conversion
Line 10,369 ⟶ 10,498:
wikEd.FixBasic = function (obj) {
// convert highlighted nbsp to character entity
if ( wikEd.config.fixNbsp === true ) {
obj.plain = obj.plain.replace(/\xa0/g, ' ' );
}
// preserve spaces and content in pre, syntaxhighlight, source, and nowiki
Line 10,381 ⟶ 10,515:
}
);
// tab to space
Line 11,327 ⟶ 11,458:
wikEd.FixBasic(obj);
// preserve highlighted nbsp
obj.html = obj.plain;
obj.html = obj.html.replace(/(\xa0)/g, '<span class="wikEdNbSpace">$1</span><!--wikEdNbSpace-->');
// get html from plain, keep leading spaces, \n to <br>
obj.html = obj.html.replace(/(^|\n) +/g,
function(p, p1) {
Line 11,336 ⟶ 11,470:
);
obj.html = obj.html.replace(/\n/g, '<br>');
// trailing, leading, and multi spaces to nbsp
obj.html = obj.html.replace(/^ | $/gm, '\xa0');
obj.html = obj.html.replace(/(\n|\xa0 | ) /g, '$1\xa0');
// preserve double spaces after dot
Line 11,807 ⟶ 11,945:
}
}
obj.plain = obj.html;
obj.plain = obj.plain.replace(/<br\b[^>]*>/g, '\n');
return;
};
Line 11,814 ⟶ 11,955:
// wikEd.WikifyHTML:
// obj.html contains the text to be wikified
// expects < > < > &
// returns <br> (not \n)
// wikiCode === true: allow extended set of attributes for existing wikicode, keep leading spaces
Line 12,951 ⟶ 13,092:
obj.html = obj.html.replace(/(<(syntaxhighlight|source|pre)\b[^\/]*?>)((.|\n)*?)(<\/\2>)/gi,
function(p, p1, p2, p3, p4, p5) {
p3 = p3.replace(/ /g, '\
p3 = p3.replace(/\n/g, '\x00');
return p1 + p3 + p5;
Line 12,957 ⟶ 13,098:
);
// preserve multiple spaces
obj.
// preserve highlighted nbsp
obj.html = obj.html.replace(/<span\b[^>]*?\bclass="wikEdNbSpace"[^>]*>((.|\n)*?)<\/span>(<!--wikEdNbSpace-->)?/gi,
function(p, p1, p2) {
}
);
Line 12,971 ⟶ 13,111:
// remove highlighting error messages
if (wikEd.config.highlightError === true) {
obj.html = obj.html.replace(/<span\b[^>]*?\bclass="wikEdHighlightError"[^>]*>(.|\n)*?<\/span>(<!--wikEdHighlightError-->)?/g, '');
}
Line 12,993 ⟶ 13,133:
var isRemove = [];
// 12
obj.html = obj.html.replace(/((
function(p, p1, p2, p3, p4, p5) {
if (p3 === '') {
Line 13,014 ⟶ 13,154:
}
);
obj.html = obj.html.replace(/
// preserve spaces and newlines in pre tag
obj.html = obj.html.replace(/(<pre\b[^>]*>)((.|\n)*?)(<\/pre>)/g,
function(p, p1, p2, p3, p4) {
p2 = p2.replace(/ /g, '\
p2 = p2.replace(/\n/g, '\x00');
return p1 + p2 + p4;
}
Line 13,032 ⟶ 13,171:
// non-breaking spaces
obj.html = obj.html.replace(/ /g, '\xa0');
// preserved spaces
obj.html = obj.html.replace(/\x02/g, ' ');
// check for pasted html content
Line 16,368 ⟶ 16,510:
// remove leading spaces in lines
obj.html = obj.html.replace(/(<br\b[^>]*>)[\n\r]* *()/g, '$1');
// nbsp to spaces
obj.html = obj.html.replace(/ | |\xa0/g, ' ');
// textify so that no html formatting is submitted
wikEd.Textify(obj);
obj.plain = obj.plain.replace(/</g, '<');
obj.plain = obj.plain.replace(/>/g, '>');
obj.plain = obj.plain.replace(/&/g, '&');
// copy to textarea
Line 16,406 ⟶ 16,545:
obj.html = wikEd.EscapeHtml(obj.html);
// preserve \xa0 (nbsp) characters
obj.html = obj.html.replace(/(\xa0)/g, '
}
Line 16,416 ⟶ 16,555:
}
// at least display tabs and nbsp;
else {
obj.html = obj.html.replace(/(\xa0)/g, '<span class="wikEdNbSpace">$1</span><!--wikEdNbSpace-->');
obj.html = obj.html.replace(/(\t)/g, '<span class="wikEdTabPlain">$1</span><!--wikEdTabPlain-->');
}
Line 16,477 ⟶ 16,617:
wikEd.LinkInfoCall();
}
return;
};
Line 17,727 ⟶ 17,853:
wikEd.ParseDOMRecursive(obj, topNode, anchorNode, anchorOffset, focusNode, focusOffset);
obj.plain = obj.plainArray.join('');
// preserve highlighted nbsp
obj.plain = obj.plain.replace(/\xa0/g, ' ');
obj.plain = obj.plain.replace(/\x00/g, '\xa0');
return;
};
Line 17,737 ⟶ 17,866:
wikEd.ParseDOMRecursive = function (obj, currentNode, anchorNode, anchorOffset, focusNode, focusOffset) {
// preserve highlighted nbsp
var nbsp = false;
if ( currentNode.className.indexOf( 'wikEdNbSpace' ) !== -1 ) {
nbsp = true;
}
// cycle through the child nodes of currentNode
Line 17,779 ⟶ 17,914:
case childNode.TEXT_NODE:
value = childNode.nodeValue;
// preserve highlighted nbsp
if ( nbsp == true ) {
value = value.replace( /[\xa0 ]/g, '\x00' );
}
value = value.replace( /\n/g, ' ' );
break;
case childNode.ENTITY_REFERENCE_NODE:
Line 18,088 ⟶ 18,228:
}
return style;
};
|