class Formatter extends SimpleModule @pluginName: 'Formatter' opts: allowedTags: [] allowedAttributes: {} allowedStyles: {} _init: -> @editor = @_module @_allowedTags = $.merge( ['br', 'span', 'a', 'img', 'b', 'strong', 'i', 'strike', 'u', 'font', 'p', 'ul', 'ol', 'li', 'blockquote', 'pre', 'code', 'h1', 'h2', 'h3', 'h4', 'hr'], @opts.allowedTags ) @_allowedAttributes = $.extend img: ['src', 'alt', 'width', 'height', 'data-non-image'] a: ['href', 'target'] font: ['color'] code: ['class'] , @opts.allowedAttributes @_allowedStyles = $.extend span: ['color'] b: ['color'] i: ['color'] strong: ['color'] strike: ['color'] u: ['color'] p: ['margin-left', 'text-align'] h1: ['margin-left', 'text-align'] h2: ['margin-left', 'text-align'] h3: ['margin-left', 'text-align'] h4: ['margin-left', 'text-align'] , @opts.allowedStyles @editor.body.on 'click', 'a', (e) -> false decorate: ($el = @editor.body) -> @editor.trigger 'decorate', [$el] $el undecorate: ($el = @editor.body.clone()) -> @editor.trigger 'undecorate', [$el] $el autolink: ($el = @editor.body) -> linkNodes = [] findLinkNode = ($parentNode) -> $parentNode.contents().each (i, node) -> $node = $(node) if $node.is('a') or $node.closest('a, pre', $el).length return if !$node.is('iframe') and $node.contents().length findLinkNode $node else if (text = $node.text()) and /https?:\/\/|www\./ig.test(text) linkNodes.push $node findLinkNode $el re = /(https?:\/\/|www\.)[\w\-\.\?&=\/#%:,@\!\+]+/ig for $node in linkNodes text = $node.text() replaceEls = [] match = null lastIndex = 0 while (match = re.exec(text)) != null subStr = text.substring(lastIndex, match.index) replaceEls.push document.createTextNode(subStr) lastIndex = re.lastIndex uri = if /^(http(s)?:\/\/|\/)/.test(match[0]) match[0] else 'http://' + match[0] $link = $("").text(match[0]) replaceEls.push $link[0] replaceEls.push document.createTextNode(text.substring(lastIndex)) $node.replaceWith $(replaceEls) $el # make sure the direct children is block node format: ($el = @editor.body) -> if $el.is ':empty' $el.append '

' + @editor.util.phBr + '

' return $el @cleanNode(n, true) for n in $el.contents() for node in $el.contents() $node = $(node) if $node.is('br') blockNode = null if blockNode? $node.remove() else if @editor.util.isBlockNode(node) if $node.is('li') if blockNode and blockNode.is('ul, ol') blockNode.append node else blockNode = $('