User:Remember the dot/Syntax highlighter



I've created a script that adds color to the edit box. Unlike other syntax highlighter scripts such as wikEd and AceWikiEditor, this one:
 * Updates the highlighting automatically as you type.
 * Does not break the Undo and Redo buttons.
 * Does not break spellcheck.
 * Is compatible with most other scripts that affect the edit box.

If you'd like to give it a shot, add this line to your common.js page:  mw.loader.load('//meta.wikimedia.org/w/index.php?title=User:Remember_the_dot/Syntax_highlighter.js&action=raw&ctype=text/javascript&maxage=86400');

On the English Wikipedia you would add it to wikipedia:Special:Mypage/common.js. Please make sure to read the "Known issues" section below before installing the script.

Known issues

 * 1) The highlighter only works at default page zoom.
 * 2) The highlighter works best in the latest version of Firefox.
 * 3) The highlighter works almost all of the time in Opera, but does not work right with text written in certain scripts, notably Thai and Tibetan.
 * 4) The highlighter works most of the time in Chrome, but is slow, does not work right with text in non-Latin scripts (caused by WebKit bug 17427), and can break in other instances also (WebKit bug 76131). I was not able to test Safari, though it should work the same as Chrome.
 * 5) The highlighter does not work in Internet Explorer (its bugs are too severe).
 * 6) The highlighter does not perform well when editing long articles and will automatically disable itself if it detects that it is running too slowly.
 * 7) To maximize performance, the highlighter is not forgiving of sloppy syntax. For example, make sure that if you start a   tag you end it with , and use   instead of.
 * 8) A   tag created by putting a space at the beginning of a line will not be highlighted. This is because the highlighter is not smart enough to tell when a space at the beginning of a line counts as whitespace inside a template.
 * 9)   is not highlighted.
 * 10) The highlighter does not work when uploading files.
 * 11) The highlighter may override your user styles related to the editing textbox.
 * 12) The colors I chose may not be very appealing. Feedback from a graphic designer would be appreciated ;-)

Customizing
It's easy to change the highlighter to use different colors or to not highlight certain syntaxes. For example, to make wikilinks cyan and external links orange you could use:  mw.loader.load('//meta.wikimedia.org/w/index.php?title=User:Remember_the_dot/Syntax_highlighter.js&action=raw&ctype=text/javascript&maxage=86400');

syntaxHighlighterConfig = { externalLinkColor: "#FFCC66", //orange wikilinkColor: "#E6FFFF", //cyan }

To not highlight a syntax, set its color to. For example, to disable external link highlighting:  mw.loader.load('//meta.wikimedia.org/w/index.php?title=User:Remember_the_dot/Syntax_highlighter.js&action=raw&ctype=text/javascript&maxage=86400');

syntaxHighlighterConfig = { externalLinkColor: "", }

To not highlight any syntax except those you explicitly want, set  to   and set the color of each syntax you want to highlight. If you just want the usual color, set the color to "normal". For example, to only highlight tags:  mw.loader.load('//meta.wikimedia.org/w/index.php?title=User:Remember_the_dot/Syntax_highlighter.js&action=raw&ctype=text/javascript&maxage=86400');

syntaxHighlighterConfig = { defaultColor: "", tagColor: "normal", }

The following customizations are available:
 * defaultColor
 * boldColor
 * entityColor
 * externalLinkColor
 * italicColor
 * headingColor
 * hrColor
 * listAndIndentColor
 * parameterColor
 * signatureColor
 * tagColor
 * tableColor
 * templateColor
 * wikilinkColor

Reporting bugs
When reporting bugs to me, please include:
 * Lots of detail about what circumstances the problem occurs in.
 * Your browser's User-Agent information.
 * The MediaWiki skin you use.

Overview of approach
This script creates a background div, named wpTextbox0, that is inserted behind wpTextbox1, the editing textbox. wpTextbox0 and wpTextbox1 are synchronized in style, and the background of wpTextbox1 is made transparent so that wpTextbox0 shows through. Then, blocks of text are added to wpTextbox0 as span elements. The text on the blocks is transparent, but the backgrounds of the blocks are colored. By using the same text in wpTextbox0 as wpTextbox1, any positioning anomalies from dynamically composed characters are eliminated. Because wpTextbox0 and wpTextbox1 are (theoretically) perfectly synchronized, the colored backgrounds appear to the user as though they have been added directly to wpTextbox1.

The text in wpTextbox0 is not actually added to the textContents of the span elements. Instead, it is added to the :before and :after pseudo-elements of each span using a dynamically generated CSS stylesheet. This avoids problems when trying to use the browser's find-in-page feature, because if textContent were used instead of CSS content, the browser would perceive both the real text in wpTextbox1 and the transparent text in wpTextbox0 to be on the page.