From baa6fa95572156f59c9faa9a2a15ff83bfea6ac5 Mon Sep 17 00:00:00 2001 From: Frans de Jonge Date: Thu, 7 Apr 2016 14:54:34 +0200 Subject: [PATCH] General fixes Fixed Elk language string Fixed smiley detection, align detection, lists Added [tt], [pre] BBC to lowercase General code quality and style --- package-info.xml | 2 +- quickQuote.english.php | 2 +- quickQuote.hooks.php | 3 +- quickQuote.js | 536 +++++++++++++++++++++-------------------- 4 files changed, 279 insertions(+), 264 deletions(-) diff --git a/package-info.xml b/package-info.xml index 8ce896a..f070ad7 100644 --- a/package-info.xml +++ b/package-info.xml @@ -3,7 +3,7 @@ Frenzie:quick_quote Quick Quote - 0.1 + 0.2 addon readme.txt diff --git a/quickQuote.english.php b/quickQuote.english.php index 1a151e1..aef803e 100644 --- a/quickQuote.english.php +++ b/quickQuote.english.php @@ -1,3 +1,3 @@ =0 ){ + return {start: start, end: end}; + } + /* + * treeToBBCode() - parses the tree into bbcode + */ + function treeToBBCode(node){ + var k, n, + checked, start, end, + bb = [], props = []; + + if (typeof node.item == 'function') { + for (k=0; n=node[k++];) { + bb.push(treeToBBCode(n)); + } + return bb.join(''); + } + + if (node.getAttribute && node.getAttribute('userjsishidden') == 'true') { + return; + } + + switch(node.nodeType){ + case Node.ELEMENT_NODE: + var nname = node.nodeName.toLowerCase(); + var def = treeToBBCode.defaults[nname]; + if (def) { + //generic behavior + bb.push(def.before||''); + bb.push(treeToBBCode(node.childNodes)); + bb.push(def.after||''); + } + else { + //special cases + switch(nname){ + case 'a': + if (node.href.indexOf('mailto:') === 0) { + bb.push('[email='+node.href.substring(7)+']'); + bb.push(treeToBBCode(node.childNodes)); + bb.push('[/email]'); + } + else if (node.className.indexOf("attach") >= 0) { bb.push('[ATTACH='+node.href+']'); bb.push(treeToBBCode(node.childNodes)); bb.push('[/ATTACH]'); } - else{ - bb.push('[url='+node.href+']'); - bb.push(treeToBBCode(node.childNodes)); - bb.push('[/url]'); - } - break; - case 'img': - var smileyCode = getSmileyCode(node); - bb.push( smileyCode ? ' '+smileyCode+' ' : '[img]'+node.src+'[/img]'); - break; - case 'ol': - var type = node.className.indexOf("alpha")>=0 ? 'a' : '1'; + else { + bb.push('[url='+node.href+']'); + bb.push(treeToBBCode(node.childNodes)); + bb.push('[/url]'); + } + break; + case 'div': + props = [ + {name:'textAlign', forceValue:'left', before:'[align=left]', after:'[/align]'}, + {name:'textAlign', forceValue:'right', before:'[align=right]', after:'[/align]'}, + ]; + checked = checkCSSProps(node, props); + + bb.push(checked.start); + bb.push(treeToBBCode(node.childNodes)); + bb.push(checked.end); + break; + case 'img': + var smileyCode = getSmileyCode(node); + bb.push(smileyCode ? ' '+smileyCode+' ' : '[img]'+node.src+'[/img]'); + break; case 'ul': - bb.push('[list'+(type?'='+type:'')+']'); - var lis = node.selectNodes('li'); - for(var k=0,li;li=lis[k++];) - bb.push('\n [*] '+trim(treeToBBCode(li))); - bb.push('[/list]'); - break; - case 'span': - //check for css properties - var props=[ - {name:'textDecoration',forceValue:'underline',before:'[u]',after:'[/u]'}, - {name:'color',before:'[color=@value]',after:'[/color]'}, - {name:'fontFamily',before:'[font=@value]',after:'[/font]'}, - {name:'fontSize',before:'[size=@value]',after:'[/size]',values:{ - 'xx-small':1, - 'x-small':2, - 'small':3, - 'medium':4, - 'large':5, - 'x-large':6, - 'xx-large':7 - }} - ]; - var start='', end=''; - for(var k=0,p;p=props[k++];){ - var value = trim(node.style[p.name]||'',' "'); - if( ( p.forceValue && value==p.forceValue ) || ( !p.forceValue && value ) ){ - start += p.before.replace('@value',(p.values ? p.values[value]:null) || value); - end += p.after; - }; - }; - //check for class attribute - props=[ - {name:'alignleft',before:'[ALIGN=left]',after:'[/ALIGN]'}, - {name:'aligncenter',before:'[ALIGN=center]',after:'[/ALIGN]'}, - {name:'alignright',before:'[ALIGN=right]',after:'[/ALIGN]'}, - {name:'alignjustify',before:'[ALIGN=justify]',after:'[/ALIGN]'} - ]; - for(var k=0,p;p=props[k++];){ - if( node.className.indexOf(p.name)>=0 ){ - start += p.before; - end += p.after; - }; - }; - bb.push(start); - bb.push(treeToBBCode(node.childNodes)); - bb.push(end); - break; + props = [ + {name:'listStyleType', forceValue:'decimal', before:'[list type=decimal]', after:'[/list]'}, + ]; + checked = checkCSSProps(node, props); + + bb.push((checked.start !== '') ? checked.start : '[list]'); + var li, lis = node.querySelectorAll('li'); + for (k=0; li=lis[k++];) { + bb.push('\n [*] '+trim(treeToBBCode(li))); + } + bb.push('[/list]'); + break; + case 'span': + //check for css properties + props=[ + {name:'textDecoration', forceValue:'underline', before:'[u]',after:'[/u]'}, + {name:'color', before:'[color=@value]', after:'[/color]'}, + {name:'fontFamily', before:'[font=@value]', after:'[/font]'}, + {name:'fontSize', before:'[size=@value]', after:'[/size]', values:{ + 'xx-small':1, + 'x-small':2, + 'small':3, + 'medium':4, + 'large':5, + 'x-large':6, + 'xx-large':7 + }} + ]; + checked = checkCSSProps(node, props); + start = checked.start; + end = checked.end; + + //check for class attribute + props = [ + {name:'centertext',before:'[align=center]',after:'[/align]'}, + {name:'bbc_tt',before:'[tt]',after:'[/tt]'}, + ]; + var p; + for (k=0; p=props[k++];) { + if (node.className.indexOf(p.name) >= 0) { + start += p.before; + end += p.after; + } + } + bb.push(start); + bb.push(treeToBBCode(node.childNodes)); + bb.push(end); + break; case 'p': var ns = node.nextElementSibling||node.nextSibling; //detect quote @@ -180,7 +217,7 @@ function initializeQuickQuote() { default: bb.push(treeToBBCode(node.childNodes)); break; - }; + } } break; case Node.DOCUMENT_NODE:// 9 @@ -188,55 +225,55 @@ function initializeQuickQuote() { bb.push(treeToBBCode(node.childNodes)); break; case Node.TEXT_NODE://3 - case Node.CDATA_SECTION_NODE:// 4 - var text = node.nodeValue; - if (!node.selectSingleNode('ancestor::pre')) - text = text.replace(/\n[ \t]+/g,'\n') - bb.push(text); - break; - } - return bb.join(''); - }; - treeToBBCode.defaults = { - strong:{before:'[B]',after:'[/B]'}, - b:{before:'[B]',after:'[/B]'}, - i:{before:'[I]',after:'[/I]'}, - em:{before:'[I]',after:'[/I]'}, - s:{before:'[S]',after:'[/S]'}, - sup:{before:'[SUP]',after:'[/SUP]'}, - sub:{before:'[SUB]',after:'[/SUB]'}, - pre:{before:'[CODE]',after:'[/CODE]'}, - br:{before:'\n',after:''} - }; - - /* - * trim() - trim string - */ - function trim(str, charToReplace) { - //if char is specified, use that one else clear whitespace - if( charToReplace ) { - return String(str).replace(new RegExp('^['+charToReplace+']+|['+charToReplace+']+$','g'),''); - } - else{ - return String(str).replace(/^\s+|\s+$/g, ''); - } - } - - /* - * getSmileyCode() - returns smiley code - */ - function getSmileyCode(img) { + case Node.CDATA_SECTION_NODE:// 4 + var text = node.nodeValue; + if (!node.selectSingleNode('ancestor::pre')) + text = text.replace(/\n[ \t]+/g,'\n'); + bb.push(text); + break; + } + return bb.join(''); + } + treeToBBCode.defaults = { + strong:{before:'[b]',after:'[/b]'}, + b:{before:'[b]',after:'[/b]'}, + i:{before:'[i]',after:'[/i]'}, + em:{before:'[i]',after:'[/i]'}, + s:{before:'[s]',after:'[/s]'}, + sup:{before:'[sup]',after:'[/sup]'}, + sub:{before:'[sub]',after:'[/sub]'}, + pre:{before:'[code]',after:'[/code]'}, + br:{before:'\n',after:''} + }; + + /* + * trim() - trim string + */ + function trim(str, charToReplace) { + //if char is specified, use that one else clear whitespace + if ( charToReplace ) { + return String(str).replace(new RegExp('^['+charToReplace+']+|['+charToReplace+']+$','g'),''); + } + else { + return String(str).replace(/^\s+|\s+$/g, ''); + } + } + + /* + * getSmileyCode() - returns smiley code + */ + function getSmileyCode(img) { var re = '^' + location.protocol + '\/\/' + location.hostname + '.+' + '\/smileys\/\\w+\/(\\w+)\\.gif$'; - re = re.replace(/\//g, '\\/') + re = re.replace(/\//g, '\\/'); re = new RegExp(re); - if (!img.src.match(re)) - return ''; // Event not spawned by a forum smiley (else match smiley name below) - + if (!img.src.match(re)) { + return ''; // Event not spawned by a forum smiley (else match smiley name below) + } else { // Alternative text corresponds to smiley code. return img.alt; } - + /* var smileyName = RegExp.$1; @@ -244,31 +281,65 @@ function initializeQuickQuote() { return buildSmileyTooltip.buildSmileyTooltipMatches[smileyName]; else // Use filename to obtain smiley code. return ':'+smileyName+':';*/ - } - - function getParents(el) { - var parents = []; + } - var p = el.parentNode; + function executeQuickQuote(e) { + e.preventDefault(); - while (p !== null) { - var o = p; - parents.push(o); - p = o.parentNode; - } - return parents; // returns an Array [] - } - - function getPostAncestor(selectionAncestor) { - var parents = getParents(selectionAncestor); - for(var i=0; i