-
Notifications
You must be signed in to change notification settings - Fork 368
Class Applier Module
This module adds, removes and toggles classes on all text nodes and optionally empty elements within a Range or Selection.
You can see a live example of this module in action here.
<script type="text/javascript">
var applier;
window.onload = function() {
rangy.init();
applier = rangy.createClassApplier("someClass");
});
</script>
<input type="button" value="Toggle someClass on selection"
onclick="applier.toggleSelection();">
Square brackets around one or more function or method parameters indicate that the parameter(s) is/are optional.
Creates and returns a ClassApplier
object. Elements created by this object are given the class theClass
. Each property of the optional options
parameter object is optional and uses the appropriate default value if not specified. The available properties are:
-
elementTagName
: The tag name of the element to use for surrounding text. This may be any inline element that may contain text. The default is "span". -
elementProperties
: An object containing properties that are copied to each element created to surround text. -
elementAttributes
: An object containing attributes that are copied to each element created to surround text. -
ignoreWhiteSpace
: Boolean value indicating whether to ignore (i.e. not to wrap) insignificant white space text nodes (such as a line break and/or indentation between<p>
tags in the HTML source of the page). The default istrue
. -
applyToEditableOnly
: Boolean value indicating whether to only apply the class to editable content (contentEditable
true ordesignMode
"on"). The default isfalse
. -
tagNames
: a list of tag names corresponding to elements to which the class can be applied. This may either be an array (such as["span", "b", "strong", "a"]
) or a comma-separated string (such as"span, b, strong, a"
). If omitted, the default is"span"
. You may alternatively specify"*"
to indicate that the class may be applied to any element. However, block level elements (such as<p>
and<div>
) are ignored: when an element is partially selected, Rangy splits and copies the element, and if the element is block level, the layout will change. -
normalize
: Boolean value indicating whether theClassApplier
will merge adjacent text nodes and adjacent spans marked with its own unique class whenever it acts on a range or selection. The default istrue
. -
onElementCreate
: function that is called whenever a new element is created. Two parameters are passed to it: a reference to the element being created and a reference to the current class applier. Note that any changes to attributes or properties made by this function may prevent it from being merged with other elements. -
useExistingElements
: Boolean value indicating whether theClassApplier
will reuse existing elements when applying a class. The default istrue
.
- When using both
elementProperties
andelementAttributes
, avoid setting the same property/attribute in both. Changing an attribute usually sets a property. Changing a property sometimes sets an attribute. Rangy does not check for this, so doing so will almost certainly lead to some kind of undesirable behaviour. - In Internet Explorer <= 7 and emulation modes in later versions,
getAttribute()
andsetAttribute()
do not behave properly: they get/set a property rather than an attribute. This means that, for example, trying to add an event handler attribute such asonclick
or an attribute represented by an object property such asstyle
inelementAttributes
will work inconsistently between IE <= 7 and other browsers. Rangy makes no attempt to correct for this. - There is a special case in
elementProperties
forstyle
: all properties of thestyle
property inelementProperties
are copied to the element'sstyle
property. -
className
is also a special case inelementProperties
: you can use it to add extra classes to elements created by Rangy. Don't include the main class for the applier. - Don't try to set
class
orclassName
inelementAttributes
. They will be ignored.
Surrounds all text nodes within the selection for the specified window with <span>
elements with this ClassApplier's class. If no window is provided, the current window's selection is used.
Removes this ClassApplier's class from all text within the selection for the specified window. If no window is provided, the current window's selection is used.
Returns a Boolean indicating whether all of the text contained within the selection for the specified window is contained within <span>
elements with this ClassApplier's class. If no window is provided, the current window's selection is used.
Applies this ClassApplier's class to all of the text contained within the selection for the specified window unless all the text already has the class, in which case it is removed. If no window is provided, the current window's selection is used.
Surrounds all text nodes within the specified Rangy range with <span>
elements with this ClassApplier's class.
Removes this ClassApplier's class from all text within the specified Rangy range.
Returns a Boolean indicating whether all of the text contained within the specified Rangy range is contained within <span>
elements with this ClassApplier's class.
Applies this ClassApplier's class to all of the text contained within the specified Rangy range unless all the text already has the class, in which case it is removed.
Removes this ClassApplier's unique class from all elements within the specified document. If no document is provided, the current document is used.
Since no unique classes are used from version 1.1, this method no longer has any effect.