Bug#790303 - Fix javascript or external URL insertion in style= attributes
[mediawiki.git] / stylesheets / wikibits.js
blob21c679ab10b86860af5b5854a31897bf3bf07cf8
1 // Wikipedia JavaScript support functions
3 var clientVer = parseInt(navigator.appVersion); // Get browser version
4 var clientPC = navigator.userAgent.toLowerCase(); // Get client info
5 var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
6 var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
7                 && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
8                 && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));
9 var is_moz = 0;
10 var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
11 var is_mac = (clientPC.indexOf("mac")!=-1);
13 // for enhanced RecentChanges
14 function toggleVisibility( _levelId, _otherId, _linkId) {
15         var thisLevel = document.getElementById( _levelId );
16         var otherLevel = document.getElementById( _otherId );
17         var linkLevel = document.getElementById( _linkId );
18         if ( thisLevel.style.display == 'none' ) {
19                 thisLevel.style.display = 'block';
20                 otherLevel.style.display = 'none';
21                 linkLevel.style.display = 'inline';
22         } else {
23                 thisLevel.style.display = 'none';
24                 otherLevel.style.display = 'inline';
25                 linkLevel.style.display = 'none';
26                 }
27         }
29 // Timezone stuff
30 // tz in format [+-]HHMM
31 function checkTimezone( tz, msg ) {
32         var localclock = new Date();
33         // returns negative offset from GMT in minutes
34         var tzRaw = localclock.getTimezoneOffset();
35         var tzHour = Math.floor( Math.abs(tzRaw) / 60);
36         var tzMin = Math.abs(tzRaw) % 60;
37         var tzString = ((tzRaw >= 0) ? "-" : "+") + ((tzHour < 10) ? "0" : "") + tzHour + ((tzMin < 10) ? "0" : "") + tzMin;
38         if( tz != tzString ) {
39                 var junk = msg.split( '$1' );
40                 document.write( junk[0] + "UTC" + tzString + junk[1] );
41         }
44 // in [-][H]H format...
45 // won't yet work with non-even tzs
46 function fetchTimezone() {
47         // FIXME: work around Safari bug
48         var localclock = new Date();
49         // returns negative offset from GMT in minutes
50         var tzRaw = localclock.getTimezoneOffset();
51         var tzHour = Math.floor( Math.abs(tzRaw) / 60);
52         var tzString = ((tzRaw >= 0) ? "-" : "") + ((tzHour < 10) ? "" : "0") + tzHour;
53         return tzString;
56 function guessTimezone(box) {
57         document.preferences.wpHourDiff.value = fetchTimezone();
60 function showTocToggle(show,hide) {
61         if(document.getElementById) {
62                 document.writeln('<small>[<a href="javascript:toggleToc()" class="internal">' +
63                 '<span id="showlink" style="display:none;">' + show + '</span>' +
64                 '<span id="hidelink">' + hide + '</span>'
65                 + '</a>]</small>');
66         }
69 function toggleToc() {
70         var toc = document.getElementById('tocinside');
71         var showlink=document.getElementById('showlink');
72         var hidelink=document.getElementById('hidelink');
73         if(toc.style.display == 'none') {
74                 toc.style.display = tocWas;
75                 hidelink.style.display='';
76                 showlink.style.display='none';
78         } else {
79                 tocWas = toc.style.display;
80                 toc.style.display = 'none';
81                 hidelink.style.display='none';
82                 showlink.style.display='';
84         }
87 // apply tagOpen/tagClose to selection in textarea,
88 // use sampleText instead of selection if there is none
89 // copied and adapted from phpBB
90 function insertTags(tagOpen, tagClose, sampleText) {
92         var txtarea = document.editform.wpTextbox1;
93         if ((clientVer >= 4) && is_ie && is_win) {
94                 theSelection = document.selection.createRange().text;
95                 if (!theSelection) {
96                         txtarea.value += tagOpen + sampleText + tagClose;
97                         txtarea.focus();
98                         return;
99                 }
100                 document.selection.createRange().text = tagOpen + theSelection + tagClose;
101                 txtarea.focus();
102                 return;
103         }
104         else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
105         {
106                 mozWrap(txtarea, tagOpen, tagClose, sampleText);
107                 return;
108         }
109         else
110         {
111                 txtarea.value += tagOpen + sampleText + tagClose;
112                 txtarea.focus();
113         }
114         storeCaret(txtarea);
117 // From http://www.massless.org/mozedit/
118 function mozWrap(txtarea, open, close)
120         var txtarea = document.editform.wpTextbox1;
121         var selLength = txtarea.textLength;
122         var selStart = txtarea.selectionStart;
123         var selEnd = txtarea.selectionEnd;
124         if (selEnd == 1 || selEnd == 2)
125                 selEnd = selLength;
127         var s1 = (txtarea.value).substring(0,selStart);
128         var s2 = (txtarea.value).substring(selStart, selEnd)
129         var s3 = (txtarea.value).substring(selEnd, selLength);
130         txtarea.value = s1 + open + s2 + close + s3;
131         return;
134 // Insert at Claret position. Code from
135 // http://www.faqts.com/knowledge_base/view.phtml/aid/1052/fid/130
136 function storeCaret(textEl) {
137         if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();