Hello,

I am trying to integrate the Mathematical Formulas (mathjax) CKEditor plugin with Drupal. A sandbox project is available at https://www.drupal.org/sandbox/dinarcon/ckeditor_mathjax

The Mathematical Formulas plugin is implemented as a CKEditor widget. As shown in the documentation, it is possible to create a widget without implementing the editor.ui.addButton() method in the plugin.js.

When integrating such a plugin with Drupal, you can add the button to the toolbar through the Text formats and editors configuration page...

..but when you try to use the WYSIWYG editor the button is missing.

Using the inspector we can see that, in the editor's settings, the toolbar configuration includes a reference for the mathjax button.

Even though the button is not visible, the library is working. For example, if we manually edit source to add content to be rendered as a formulae by the Mathjax library...

...we get the text converted when we exit source editing.

Even the dialog functionality works upon clicking on the widget.

The Drupal Image button was created as a widget, but in this case the editor.ui.addButton() method was implemented.

Also, the Mathematical Formulas plugin does not implement the editor.addCommand() method to define the list of allowed tags as Core's Drupal Image and other plugins like Youtube do. Instead, the Mathematical Formulas implements the editor.widgets.add() method. This might prevent the proper tags from being automatically added to the "Allowed HTML tags" field if the "Limit allowed HTML tags and correct faulty HTML" filter is enabled. Nevertheless, manually adding the tag does not solve the problem either.

The Mathematical Formulas (mathjax) demo on the CKEditor plugin page shows the button in the toolbar using the same code without the editor.ui.addButton() method, yet it works fine there. So the question is, is there anything custom in Drupal that prevents the button to be shown?

I have been trying many things to get this working without success. I even created a custom CKEditor build using Core's build-config.js file and then created another build adding the Mathematical Formulas plugin to the same setup using the CKEditor web builder. After a diff between directories, nothing unexpected came up. The difference was the extra library and its configuration.

For the record, I have tested this with the "Limit allowed HTML tags and correct faulty HTML" filter disabled to allow the plugin's required class to be added to the <span> tag. Although this does not necessarily conflicts with the button visibility, if the class is stripped out by the filter you would not see the library working as expected even when everything else is properly configured.

Steps to reproduce this issue:

1) Download the latest development version of Drupal core.
2) Download this sandbox project: https://www.drupal.org/sandbox/dinarcon/ckeditor_mathjax.
3) Download the Mathematical Formulas (mathjax) plugin and place it in the /libraries folder in your Drupal root folder.
4) Enable the CKEditor Mathematical Formulas module.
5) Go to the Text formats and editors settings (/admin/config/content/formats).
6) Add the Mathematical Formulas Button to the Full HTML text format.
7) Create an Article node and switch to the Full HTML format. You should not be able to see the button, but manually editing source should work.

Example code in source editing mode:

<span class="my-math">\( \left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right) \)</span>

Note that this issue has been reported in a similar context and modifying the library to implement the editor.ui.addButton() method is reported to work. After given that a try, I got the button to show, but the dialog does not work properly. In any case, modifying the library does not seem like a good solution, particularly because it works fine in the plugin demo page without such a change.

Any help on getting this working in much appreciated.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dinarcon created an issue. See original summary.

Wim Leers’s picture

Category: Bug report » Support request
Status: Active » Postponed (maintainer needs more info)

Did you read the documentation?

dinarcon’s picture

Hi Wim, thanks for your time.

Yes, I have read the documentation. Also, I have successfully integrated other CKEditor plugins and reviewed other Drupal integrations. The is why I do not understand why this particular plugin is not working. The only thing that I find different is that Mathematical Formulas (mathjax) was created as a widget and did not implement the editor.ui.addButton() method.

Am I overseeing something? What additional information would be useful to provide?

Wim Leers’s picture

Thanks for your thoughtful reply!

My strongest suspicion then is ACF.

  1. What tags does the widget generate?
  2. Are those tags allowed by CKEditor's ACF?
  3. Does your plugin specify the necessary ACF metadata?
  4. To rule out that ACF is causing this, please add the Mathjax button to the Full HTML format/editor. ACF is disabled in that format/editor, precisely because anything is allowed. So, if the button does show up in that format/editor, then the root cause is definitely ACF, otherwise it's probably not.
dinarcon’s picture

Issue summary: View changes

Hi Wim, thanks for reviewing this.

Responding your questions:

1) The module only requires one span tag with a customizable class. When enabling the sandbox module this is <span class="my-math"> by default.

2) The tag is not allowed by default, but manually adding it does not solve the issue either. It is important to note that the class to use is configurable so I am not sure if the custom class name will be automatic added to the "Allowed HTML tags" field.

3) I would say yes. The the plugin adds the tag/class in the editor.widgets.add() method via the allowedContent property. It is important to note that this is different compared to other plugins like Youtube that uses the allowedContent property in the editor.addCommand() method. I am not sure if the lack of implementation of the editor.addCommand() by the Mathematical Formulas plugins might be part of the problem, but using the CKEDitor builder the button appears and works without modifications to the plugin.

4) I also though ACF might be a problem. In fact, without adding <span class="my-math"> to the Allowed HTML tags, ACF keeps the span tag, but removes the custom class. I already tested added the tag/class to the allowed list, removing the "Limit allowed HTML tags and correct faulty HTML" filter, and using the "Full HTML" format without success. So, I think ACF is not the issue here.

To paraphrase myself a bit, it is the button that is missing. Entering the text in source edit mode and switching back to WYSIWYG triggers the library and the text is converted to a Math equation.

Wim Leers’s picture

Assigned: Unassigned » mlewand
Status: Postponed (maintainer needs more info) » Active
  1. You need to specify requiredContent too. allowedContent indicates HTML that this plugin/widget might generate. requiredContent indicates which HTML is the absolute minimum HTML that this plugin/widget needs to function at all. I've pinged the CKEditor guys that this needs to be documented better.
  2. Hm :( https://github.com/ckeditor/ckeditor-dev/blob/master/plugins/mathjax/plu... indeed does not call editor.ui.addButton(), but does specify a button. This is something the CKEditor team needs to answer then for sure!

Assigning to my co-maintainer, @mlewand, who works for CKSource.

thpoul’s picture

Jumping into the discussion to say that I had the same experience as the op, a couple of weeks ago, with an other addon (http://ckeditor.com/addon/chart) which I added via the D8 CKEditor api. It would be great if this could get addressed by CKEditor.

dinarcon’s picture

3) Thank you Wim for the reference! I was not aware of the requiredContent property. The Mathematical Formulas does not define that property. The documentation says that the default value is null and if no value is provided the feature will be always activated. That would explain why other plugins like YouTube works without specifying this property. Although it would be better to provide. Maybe this is something we should suggest to be added upstream.

4) Right, that confuses me a bit. Although the current code is consistent with the documentation where the icon is added as part of the pluginDefinition object passed to CKEDITOR.plugins.add(). The docs even say...

Additionally, as we are going to define a toolbar button, the icons property needs to be set and include the name of the icon file.

Please note the special naming convention for widget toolbar buttons. The Widget API will only be able to automatically add the button to the toolbar if the name of the icon is the same as the widget. In this case this will be simplebox. Do remember that the icons property accepts a PNG icon file name without an extension.

...which the Mathematical Formulas plugin follow.

Let's wait for @mlewand input then. Thanks for the help so far. :-)

mlewand’s picture

Assigned: mlewand » Wim Leers

Ad 3.

Actually MathJax does mark required content but witha shortcut approach, it specifies: allowedContent: 'span(!' + cls + ')', wchich becomes allowedContent: 'span(!my-math)', - in this notation exclamation mark means that given class is required for the feature to work.

An exclamation mark (!) used before an item name (e.g.: [!href]) in the property list means: "This property is required. If an element does not have it, this rule should not be applied to the element (so the element will not be validated by it)".

Ad 4.

As for adding the button in widgets, there's no need to call editor.ui.addButton() as long as you specified correctly plugin icons property, and button for widget definiton. It will happen automatically, just like mentioned in docs:

Please note the special naming convention for widget toolbar buttons. The Widget API will only be able to automatically add the button to the toolbar if the name of the icon is the same as the widget.

Could you summarize what is your suggestion for this?

Wim Leers’s picture

Status: Active » Postponed (maintainer needs more info)

Now blocked on dinarcon's feedback :)

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

johnyfk’s picture

I am facing the same issue with mathjax. Any help would be appreciated

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

zarexogre’s picture

Any news on how to fix this? Comparing this module to codesnippet which works fine, this module is missing a libraries file and doesn't have a preprocess html or .module file, just wondered if this might be a factor, gunna have a play, but would like to get this working. Any advice would be great!

zarexogre’s picture

I have managed to get this fully working in Drupal 8, possibly not best method but at least it works.
- added this code snippet to force the ckeditor plugin to work nice with drupal
line 19 just after the init function

editor.ui.addButton && editor.ui.addButton( 'mathjax', {
        label: editor.lang.mathjax.button,
        command: 'mathjax',
        toolbar: 'insert,10'
      } );

- added a library.yml
- added a module and pre process html for cdn script insertion (frontend doesnt work as cdn isnt added to page)

Happy to provide patch.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

webflo’s picture

I debugged this issue to resolve #2893184: Toolbar button missing and posted a comment on the other issue to.

I think this bug is related to the capitalize of the icon name in https://github.com/ckeditor/ckeditor-dev/blob/master/plugins/widget/plug...

\Drupal\ckeditor\CKEditorPluginButtonsInterface::getButtons should return the capitalized names.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

ryan.gibson’s picture

I'm experiencing the same behavior as OP here as well on a simple custom plugin and widget that I created.

idiaz.roncero’s picture

I've developed a few custom CKEditor widgets and I've found the need to manually declare editor.addButton to make it appear on the editor, even if it shouldb't be necessary as per the official CKeditor documentation.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andeersg’s picture

I encountered this problem and started debugging with the non-minified CKEditor library (checked out from latest git). I believe the problem comes from

  function addWidgetButtons( editor ) {
		var widgets = editor.widgets.registered,
			widget,
			widgetName,
			widgetButton;

		for ( widgetName in widgets ) {
			widget = widgets[ widgetName ];

			// Create button if defined.
			widgetButton = widget.button;
			if ( widgetButton && editor.ui.addButton ) {
				editor.ui.addButton( CKEDITOR.tools.capitalize( widget.name, true ), { // <- This line, 57873
					label: widgetButton,
					command: widget.name,
					toolbar: 'insert,10'
				} );
			}
		}
	}

I'm not sure why this capitalization is here, but the problems happens at line 18281:

	create: function( name ) {

		var item = this.items[ name ], // This line!
			handler = item && this._.handlers[ item.type ],
			command = item && item.command && this.editor.getCommand( item.command );

		var result = handler && handler.create.apply( this, item.args );

		this.instances[ name ] = result;

		// Add reference inside command object.
		if ( command )
			command.uiItems.push( result );

		if ( result && !result.type )
			result.type = item.type;

		return result;
	}

In the create function the lowercase name is used, and that does not match the capitalized version. So the button is removed.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Project: Drupal core » CKEditor 4 - WYSIWYG HTML editor
Version: 9.5.x-dev » 1.0.x-dev
Component: ckeditor.module » Code

CKEditor has been removed from core, CKEditor 4 is removed from Drupal Core in 10.0.0

e0ipso’s picture

FileSize
99.11 KB

I think this issue should be closed with Closed (works as designed)... after this behavior is documented properly. Using a capitalized string for my button fixed the issue, but contradicts CKEditor's documentation:

https://ckeditor.com/docs/ckeditor4/latest/guide/widget_sdk_tutorial_1.html

Docs extract