/** * @license Copyright (c) 2003-2014, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md or http://ckeditor.com/license */ /** * @fileOverview [Mathematical Formulas](http://ckeditor.com/addon/mathjax) plugin. */ 'use strict'; ( function() { var cdn = '\/\/cdn.mathjax.org\/mathjax\/2.4-latest\/MathJax.js?config=TeX-MML-AM_HTMLorMML-full'; CKEDITOR.plugins.add( 'asciimath', { lang: 'ar,ca,cs,cy,de,el,en,en-gb,eo,es,fa,fi,fr,gl,he,hr,hu,it,ja,km,nb,nl,no,pl,pt,pt-br,ro,ru,sk,sl,sv,tr,tt,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE% requires: 'widget,dialog', icons: 'asciimath', hidpi: true, // %REMOVE_LINE_CORE% init: function( editor ) { var cls = editor.config.mathJaxClass || 'math-tex'; editor.widgets.add( 'asciimath', { inline: true, dialog: 'asciimath', button: editor.lang.asciimath.button, mask: true, allowedContent: 'span(!' + cls + ')', // Allow style classes only on spans having mathjax class. styleToAllowedContentRules: function( style ) { var classes = style.getClassesArray(); if ( !classes ) return null; classes.push( '!' + cls ); return 'span(' + classes.join( ',' ) + ')'; }, pathName: editor.lang.asciimath.pathName, template: '', parts: { span: 'span' }, defaults: { math: '\`f(x)=sum_(n=0)^oo(f^((n))(a))/(n!)(x-a)^n\`' }, init: function() { var iframe = this.parts.span.getChild( 0 ); // Check if span contains iframe and create it otherwise. if ( !iframe || iframe.type != CKEDITOR.NODE_ELEMENT || !iframe.is( 'iframe' ) ) { iframe = new CKEDITOR.dom.element( 'iframe' ); iframe.setAttributes( { style: 'border:0;width:0;height:0', scrolling: 'no', frameborder: 0, allowTransparency: true, src: CKEDITOR.plugins.asciimath.fixSrc } ); this.parts.span.append( iframe ); } // Wait for ready because on some browsers iFrame will not // have document element until it is put into document. // This is a problem when you crate widget using dialog. this.once( 'ready', function() { // Src attribute must be recreated to fix custom domain error after undo // (see iFrame.removeAttribute( 'src' ) in frameWrapper.load). if ( CKEDITOR.env.ie ) iframe.setAttribute( 'src', CKEDITOR.plugins.asciimath.fixSrc ); this.frameWrapper = new CKEDITOR.plugins.asciimath.frameWrapper( iframe, editor ); this.frameWrapper.setValue( this.data.math ); } ); }, data: function() { if ( this.frameWrapper ) this.frameWrapper.setValue( this.data.math ); }, upcast: function( el, data ) { if ( !( el.name == 'span' && el.hasClass( cls ) ) ) return; if ( el.children.length > 1 || el.children[ 0 ].type != CKEDITOR.NODE_TEXT ) return; data.math = CKEDITOR.tools.htmlDecode( el.children[ 0 ].value ); // Add style display:inline-block to have proper height of widget wrapper and mask. var attrs = el.attributes; if ( attrs.style ) attrs.style += ';display:inline-block'; else attrs.style = 'display:inline-block'; // Add attribute to prevent deleting empty span in data processing. attrs[ 'data-cke-survive' ] = 1; el.children[ 0 ].remove(); return el; }, downcast: function( el ) { el.children[ 0 ].replaceWith( new CKEDITOR.htmlParser.text( CKEDITOR.tools.htmlEncode( this.data.math ) ) ); // Remove style display:inline-block. var attrs = el.attributes; attrs.style = attrs.style.replace( /display:\s?inline-block;?\s?/, '' ); if ( attrs.style === '' ) delete attrs.style; return el; } } ); // Add dialog. CKEDITOR.dialog.add( 'asciimath', this.path + 'dialogs/mathjax.js' ); // Add MathJax script to page preview. editor.on( 'contentPreview', function( evt ) { evt.data.dataValue = evt.data.dataValue.replace( /<\/head>/, '' + // Load MathJax lib. '' + '' + '
' + '' + // Render everything here and after that copy it to the preview. '' + '' + '