ExpressionEngine® 3 User Guide

Legacy Documentation

You are using the documentation for version 3.5.17. Go here for the latest version or check here for your available upgrades to the latest version.

WysiHat API Reference

This documentation is for developers who wish to dig a little deeper. It outlines most of the core WysiHat classes and methods.

jQuery Reference

This is the main public method to use when dealing with WysiHat. It will handle most of the setup for you. It can also be used to get access to instances of some of the public classes without going through $.data().

Initial Setup


Creates a new instance of the editor on a textarea.

    'buttons': ['bold', 'italic', 'underline']

Existing Editors


Attempting to setup multiple editors will fail. Subsequent calls to $.wysihat() will return the existing editor. You can also access the editor instance bound to an element using $.data():


Helper Classes

Since the helper classes are all bound on the editor instance you can retrieve any of them from an editor instance

editor = $('textarea').data('wysihat');
selection = editor.Selection;

Alternatively you can use the WysiHat constructor to get access to any of the helper classes directly. This is provided mainly for debugging, to quickly look up information about the undo stack or selection.

$('textarea').wysihat('undo'); // undo stack
$('textarea').wysihat('selection'); // selection utility

Publicly Accessible Classes

Just because they exist does not mean it’s always a good idea to use them. Use common sense and try to find a way to use the more general button API where possible.


class WysiHat()

The editor name. Always “WysiHat” in ExpressionEngine. This is used to prefix editor specific events.

addButton(name, config)

Register a button for use. Not shown in the editor, until it is actually added to the toolbar. This is usually accomplished through the button option when attaching the editor.

inherit(proto, props)

Convenience method for prototypal inheritance. Returns a new object with proto in its prototype chain and an additional parent property which contains proxies to the prototype’s methods.


class WysiHat.Editor()

An object of this class is returned by attach() It implements the CommandExpando as well as the following methods:


Updates the textarea from the editor markup. Called automatically for most changes.


Updates the editor from the textarea markup. Automatically called when switching from source to editor view.


Utility method to help select an empty editor. Most browsers will not create a paragraph tag for the first paragraph otherwise.


class WysiHat.Event()

Main event handling class. Takes care of all internal and external editor events.

add(eventName, handler)

Add an event handler for a given event name.


Checks if a handler exists for eventName.

run(eventName, state, finalize)

Runs an event handler and calls finalize. Usually you will want fire().


Run all the required code to dispatch the event. This function understands all built in commands, such as undo, redo, and paste.

textChange(before[, after])

Marks a chunk of text changes as undoable.

isKeyCombo(strName, evt)

Identifies if the current event matches a specified key event name. The name must take on the form: ctrl-shfit-c.

isEvent(name, evt)

Identifies a named key event such as paste or undo.


Returns the editors current html contents and selection.


class WysiHat.Undo()

A simple undo stack. Specifically made to handle text changes, it will try to find the smallest difference in two strings rather than saving the whole thing.

push(before, after, selBefore, selAfter)

Adds a diff of the before and after strings as well as the selection positions to the undo stack.


Check for available undos.


Check for available redos.


Undo the last change.


Redo the last undo.


class WysiHat.Selection()

A small helper class that abstracts the very basic range manipulations into a text offset based system. For advanced stuff you will still have to fall back on ranges for node level granularity.


Get a selection. Returns an ordered pair of [start, end]. These offsets are text based. Not html based.

set(start[, end])

Sets up a new selection to surround the text that range. Can either accept the 2-tuple returned by get() or separate start and end offsets.


Returns the contents of the current selection.


class WysiHat.Commands()

Singleton that contains all of the available editor commands. They are split up into query commands (is), modifying commands ( make), as well as a variety of utility methods.

You can retrieve a list of make and is commands by simply dumping and WysiHat.Commands.make. These functions are also available through the shortcut methods on the CommandsMixin.


A list of styles that you may need to access in your tool. Mainly provided to smooth out strange mappings.

execCommand(command, ui, value)

Works just like the browser native execCommand, but handles errors gracefully so you don’t have to.


Utility method to check if the command is available as a WysiHat version. Used in the button handler to decide what handler to return. You probably won’t ever need this.


Utility method to check if the command is valid as an option to the browser’s execCommand. Used in the button handler to decide what handler to return. You probably won’t ever need this.


Works just like the browser native queryCommandState, but will first look for custom command state queries on Also handles errors for you.


Checks if the current selection is contained in any of the provided tags.


Returns all styles in the styleSelectors() map with their values in the current selection context.

replaceElement($el, tagName)

Takes the current element and turns it into a different one. Does not change the contents of the element.


Replaces the element with its contents, similar to jQuery’s unwrap.


This function is likely to change or be removed in the future.


Completely strips the selection of formatting.


Utility function that takes a callback and calls it with each available range in the editor in the context of WysiHat.Commands(). It will restore the ranges to their original state once all callbacks have been called.

getRangeElements(range, tagNames)

Returns all elements in the range that match the tagNames selector.


Returns an array of all ranges. Utility method to avoid calling getRangeAt in a loop.


Takes an array of ranges and creates an editor selection from them.


Similar to replaceElement(), but applies to all block elements in the selection.


Changes all block elements in the selection into paragraphs.


Removes all links in the selection.


This function may be moved in the future.


Wraps the current selection in HTML. Can be called with multiple parameters to consecutively wrap the selection further.


Toggles the editors source view and flips the button state.


This function may be re-moved in the future.


Replaces the current range with a given piece of HTML.


Turns the current line or closest block element into a blockquote. Please use toggle('blockquote') instead.


Removes the blockquote closest to the current selection. Please use toggle('blockquote') instead.


Toggles the list type of the current line. Removes the list if it is already a list of the given type. Please use toggle('li/ul') instead.


class WysiHat.Toolbar()

Add a button to the current editor toolbar. The button must already be registered with WysiHat through addButton().


Creates the main button markup. Odds are you don’t need to call this. Ever.


Sets up the event handler for the button. As with creating the button, this is done completely automatically.


Binds editor selection change events to the button’s queryStateHandler and update the button’s state when the cursor enters or exits text controllable with that button.

updateButtonState(button, state)

Toggles the buttons controls on or off. Identical to calling setOn() or setOff().

Publicly Accessible Objects

Most of these are used as utilities and have better abstractions elsewhere. Use with care.


class WysiHat.Element()

A helper object that provides easy access to types of elements.


class WysiHat.Paster()

A helper object that provides the event handler for paste events


class WysiHat.Formatting()

A helper object that contains most of the functions to keep editor markup clean and consistent. Please use the editor’s updateField() and updateEditor() methods when syncing the editor and textarea.


Removes browser added markup such as b and i tags. It also removes comments, scripts, empty paragraphs, and inline style tags.

cleanupPaste($element, parentTagName)

Cleans up a paste container. This includes everything in cleanup() as well as resolving newlines into paragraphs and br tags. When given a parentTagName it will also remove that tag from the pasted content. This is done to prevent nesting blocks such as h1 tags.

Most times you will want to use WysiHat.Paster() or'paste').


Prettifies the HTML markup to ease in readability and debugging.


Returns the raw markup form the textarea. Please use updateField() to sync.


Returns the raw markup form the editor. Please use updateEditor() to sync.

Non-public Internal Classes


class BlankButton()

This is the parent class for all buttons.

init(name, $editor)

The main constructor. If you extend it, it should always return this.


Links the button instance with its clickable element.


Returns the buttons event handler.


Returns the buttons state handler. This is called frequently. If you extend it, make sure it can handle the load.


Change the button state to indicate that it is active.


Usually you do not need to call this yourself. Look into overriding the state handler instead.


Change the button state to indicate that it is inactive. The same warnings as for setOn() apply.


class CommandsMixin()

This mixin is provided to ensure consistency across buttons and editor instances at all times.


Use this to check for the current state of the selection. Returns a boolean of the current state:'bold')

Changes the state of the current selection. Also understand some simple aliases for ease of use.

this.make('italicize'); // native name
this.make('italic'); // alias

Alias to make().

Browser Compatibility Shims

Currently not documented. These provide cross browser support for ranges and selections.