tinymce.dom.DOMUtils
Utility class for various DOM manipulation and retrieval functions.
Examples
// Add a class to an element by id in the page
tinymce.DOM.addClass('someid', 'someclass');
// Add a class to an element by id inside the editor
tinymce.activeEditor.dom.addClass('someid', 'someclass');
Summary
Methods
Name | Summary | Defined by |
---|---|---|
Adds the specified element to another element or elements. |
||
Adds a class to the specified element or elements. |
||
Adds a style element at the top of the document with the specified cssText content. |
||
Adds an event handler to the specified object. |
||
Creates a new element. |
||
Creates a document fragment out of the specified HTML string. |
||
Creates HTML string for element. The element will be closed unless an empty inner HTML string is passed in. |
||
Creates a new DOM Range object. This will use the native DOM Range API if it’s available. If it’s not, it will fall back to the custom TinyMCE implementation. |
||
Entity decodes a string. This method decodes any HTML entities, such as |
||
Destroys all internal references to the DOM to solve memory leak issues. |
||
Dispatches the specified event name and optional object on the specified target. |
||
Entity encodes a string. This method encodes the most common entities, such as |
||
Find the common ancestor of two elements. This is a shorter method than using the DOM Range logic. |
||
Fires the specified event name and optional object on the specified target. Deprecated in TinyMCE 6.0 and has been marked for removal in TinyMCE 7.0. Use |
||
Returns the specified element by ID or the input element if it isn’t a string. |
||
Returns the specified attribute by name. |
||
Returns a NodeList with attributes for the element. |
||
Returns the next node that matches selector or function |
||
Returns the outer HTML of an element. |
||
Returns a node by the specified selector function. This function will loop through all parent nodes and call the specified function for each node. If the function then returns true indicating that it has found what it was looking for, the loop execution will then end and the node it found will be returned. |
||
Returns a node list of all parents matching the specified selector function or pattern. If the function then returns true indicating that it has found what it was looking for and that node will be collected. |
||
Returns the absolute x, y position of a node. The position will be returned in an object with x, y fields. |
||
Returns the previous node that matches selector or function |
||
Returns the rectangle for a specific element. |
||
Returns the root node of the document. This is normally the body but might be a DIV. Parents like getParent will not go above the point of this root node. |
||
Returns the size dimensions of the specified element. |
||
Returns the current style or runtime/computed value of an element. |
||
Returns the viewport of the window. |
||
Returns true if the specified element has the specified class. |
||
Hides the specified element(s) by ID by setting the "display" style. |
||
Inserts an element after the reference element. |
||
Returns true/false if the specified element matches the specified css pattern. |
||
Returns true/false if the specified element is a block element or not. |
||
Checks if the specified node is editable within the given context of its parents. |
||
Returns true/false if the specified node is to be considered empty or not. |
||
Returns true/false if the element is hidden or not by checking the "display" style. |
||
Imports/loads the specified CSS file into the document bound to the class. |
||
Returns the index of the specified node within its parent. |
||
Parses the specified style value into an object collection. This parser will also merge and remove any redundant items that browsers might have added. It will also convert non-hex colors to hex values. Urls inside the styles will also be converted to absolute/relative based on settings. |
||
Removes/deletes the specified element(s) from the DOM. |
||
Removes all attributes from an element or elements. |
||
Removes a class from the specified element or elements. |
||
Renames the specified element and keeps its attributes and children. |
||
Replaces the specified element or elements with the new element specified. The new element will be cloned if multiple input elements are passed in. |
||
Executes the specified function on the element by id or dom element node or array of elements/id. |
||
Returns a list of the elements specified by the given CSS selector. For example: |
||
Serializes the specified style object into a string. |
||
Sets the specified attribute of an element or elements. |
||
Sets two or more specified attributes of an element or elements. |
||
Sets the specified HTML content inside the element or elements. The HTML will first be processed. This means URLs will get converted, hex color values fixed etc. Check processHTML for details. |
||
Sets the specified outer HTML on an element or elements. |
||
Sets the CSS style value on a HTML element. The name can be a camelcase string or the CSS style name like background-color. |
||
Sets multiple styles on the specified element(s). |
||
Shows the specified element(s) by ID by setting the "display" style. |
||
Splits an element into two new elements and places the specified split
element or elements between the new ones. For example splitting the paragraph at the bold element in
this example |
||
Toggles the specified class on/off. |
||
Removes the specified event handler by name and function from an element or collection of elements. |
||
Returns a unique id. This can be useful when generating elements on the fly. This method will not check if the element already exists. |
Methods
add()
add(parentElm: String | Element | Array, name: String | Element, attrs: Object, html: String, create: Boolean): Element | Array
Adds the specified element to another element or elements.
Examples
// Adds a new paragraph to the end of the active editor
tinymce.activeEditor.dom.add(tinymce.activeEditor.getBody(), 'p', { title: 'my title' }, 'Some content');
Parameters
-
parentElm (String | Element | Array)
- Element id string, DOM node element or array of ids or elements to add to. -
name (String | Element)
- Name of new element to add or existing element to add. -
attrs (Object)
- Optional object collection with arguments to add to the new element(s). -
html (String)
- Optional inner HTML contents to add for each element. -
create (Boolean)
- Optional flag if the element should be created or added.
addClass()
addClass(elm: String | Element | Array, cls: String): String | Array
Adds a class to the specified element or elements.
Examples
// Adds a class to all paragraphs in the active editor
tinymce.activeEditor.dom.addClass(tinymce.activeEditor.dom.select('p'), 'myclass');
// Adds a class to a specific element in the current page
tinymce.DOM.addClass('mydiv', 'myclass');
addStyle()
addStyle(cssText: String)
Adds a style element at the top of the document with the specified cssText content.
bind()
bind(target: Element | Document | Window | Array, name: String, func: Function, scope: Object): Function
Adds an event handler to the specified object.
Parameters
-
target (Element | Document | Window | Array)
- Target element to bind events to. handler to or an array of elements/ids/documents. -
name (String)
- Name of event handler to add, for example: click. -
func (Function)
- Function to execute when the event occurs. -
scope (Object)
- Optional scope to execute the function in.
create()
create(name: String, attrs: Object, html: String): Element
Creates a new element.
Examples
// Adds an element where the caret/selection is in the active editor
var el = tinymce.activeEditor.dom.create('div', { id: 'test', 'class': 'myclass' }, 'some content');
tinymce.activeEditor.selection.setNode(el);
createFragment()
createFragment(html: String): DocumentFragment
Creates a document fragment out of the specified HTML string.
createHTML()
createHTML(name: String, attrs: Object, html: String): String
Creates HTML string for element. The element will be closed unless an empty inner HTML string is passed in.
Examples
// Creates a html chunk and inserts it at the current selection/caret location
tinymce.activeEditor.selection.setContent(tinymce.activeEditor.dom.createHTML('a', { href: 'test.html' }, 'some line'));
createRng()
createRng(): DOMRange
Creates a new DOM Range object. This will use the native DOM Range API if it’s available. If it’s not, it will fall back to the custom TinyMCE implementation.
decode()
decode(s: String): String
Entity decodes a string. This method decodes any HTML entities, such as å
.
dispatch()
dispatch(target: Node | Document | Window, name: String, evt: Object): Event
Dispatches the specified event name and optional object on the specified target.
encode()
encode(text: String): String
Entity encodes a string. This method encodes the most common entities, such as <
, >
, "
and &
.
findCommonAncestor()
findCommonAncestor(a: Element, b: Element): Element
Find the common ancestor of two elements. This is a shorter method than using the DOM Range logic.
fire()
fire(target: Node | Document | Window, name: String, evt: Object): Event
Fires the specified event name and optional object on the specified target.
Deprecated in TinyMCE 6.0 and has been marked for removal in TinyMCE 7.0. Use dispatch
instead.
get()
get(n: String | Element): Element
Returns the specified element by ID or the input element if it isn’t a string.
getAttrib()
getAttrib(elm: String | Element, name: String, defaultVal: String): String
Returns the specified attribute by name.
getAttribs()
getAttribs(elm: HTMLElement | string): NodeList
Returns a NodeList with attributes for the element.
getNext()
getNext(node: Node, selector: String | function): Node
Returns the next node that matches selector or function
getOuterHTML()
getOuterHTML(elm: String | Element): String
Returns the outer HTML of an element.
getParent()
getParent(node: Node | String, selector: Function, root: Node): Node
Returns a node by the specified selector function. This function will loop through all parent nodes and call the specified function for each node. If the function then returns true indicating that it has found what it was looking for, the loop execution will then end and the node it found will be returned.
getParents()
getParents(node: Node | String, selector: Function, root: Node): Array
Returns a node list of all parents matching the specified selector function or pattern. If the function then returns true indicating that it has found what it was looking for and that node will be collected.
getPos()
getPos(elm: Element | String, rootElm: Element): Object
Returns the absolute x, y position of a node. The position will be returned in an object with x, y fields.
getPrev()
getPrev(node: Node, selector: String | function): Node
Returns the previous node that matches selector or function
getRoot()
getRoot(): Element
Returns the root node of the document. This is normally the body but might be a DIV. Parents like getParent will not go above the point of this root node.
getSize()
getSize(elm: Element | String): Object
Returns the size dimensions of the specified element.
getStyle()
getStyle(elm: String | Element, name: String, computed: Boolean): String
Returns the current style or runtime/computed value of an element.
hasClass()
hasClass(elm: String | Element, cls: String): Boolean
Returns true if the specified element has the specified class.
hide()
hide(elm: String | Element | Array)
Hides the specified element(s) by ID by setting the "display" style.
insertAfter()
insertAfter(node: Element, referenceNode: Element | String | Array): Element | Array
Inserts an element after the reference element.
is()
is(elm: Node | NodeList, selector: String)
Returns true/false if the specified element matches the specified css pattern.
isBlock()
isBlock(node: Node | String): Boolean
Returns true/false if the specified element is a block element or not.
isEditable()
isEditable(node: Node): Boolean
Checks if the specified node is editable within the given context of its parents.
isEmpty()
isEmpty(node: Node, elements: Object): Boolean
Returns true/false if the specified node is to be considered empty or not.
isHidden()
isHidden(elm: String | Element): Boolean
Returns true/false if the element is hidden or not by checking the "display" style.
loadCSS()
loadCSS(url: String)
Imports/loads the specified CSS file into the document bound to the class.
Examples
// Loads a CSS file dynamically into the current document
tinymce.DOM.loadCSS('somepath/some.css');
// Loads a CSS file into the currently active editor instance
tinymce.activeEditor.dom.loadCSS('somepath/some.css');
// Loads a CSS file into an editor instance by id
tinymce.get('someid').dom.loadCSS('somepath/some.css');
// Loads multiple CSS files into the current document
tinymce.DOM.loadCSS('somepath/some.css,somepath/someother.css');
nodeIndex()
nodeIndex(node: Node, normalized: Boolean): Number
Returns the index of the specified node within its parent.
parseStyle()
parseStyle(cssText: String): Object
Parses the specified style value into an object collection. This parser will also merge and remove any redundant items that browsers might have added. It will also convert non-hex colors to hex values. Urls inside the styles will also be converted to absolute/relative based on settings.
remove()
remove(node: String | Element | Array, keepChildren: Boolean): Element | Array
Removes/deletes the specified element(s) from the DOM.
Examples
// Removes all paragraphs in the active editor
tinymce.activeEditor.dom.remove(tinymce.activeEditor.dom.select('p'));
// Removes an element by id in the document
tinymce.DOM.remove('mydiv');
removeAllAttribs()
removeAllAttribs(e: Element | String | Array)
Removes all attributes from an element or elements.
removeClass()
removeClass(elm: String | Element | Array, cls: String): String | Array
Removes a class from the specified element or elements.
Examples
// Removes a class from all paragraphs in the active editor
tinymce.activeEditor.dom.removeClass(tinymce.activeEditor.dom.select('p'), 'myclass');
// Removes a class from a specific element in the current page
tinymce.DOM.removeClass('mydiv', 'myclass');
rename()
rename(elm: Element, name: String): Element
Renames the specified element and keeps its attributes and children.
replace()
replace(newElm: Element, oldElm: Element | String | Array, keepChildren: Boolean)
Replaces the specified element or elements with the new element specified. The new element will be cloned if multiple input elements are passed in.
Parameters
-
newElm (Element)
- New element to replace old ones with. -
oldElm (Element | String | Array)
- Element DOM node, element id or array of elements or ids to replace. -
keepChildren (Boolean)
- Optional keep children state, if set to true child nodes from the old object will be added to new ones.
run()
run(elm: String | Element | Array, func: Function, scope: Object): Object | Array
Executes the specified function on the element by id or dom element node or array of elements/id.
select()
select(selector: String, scope: Object): Array
Returns a list of the elements specified by the given CSS selector. For example: div#a1 p.test
Examples
// Adds a class to all paragraphs in the currently active editor
tinymce.activeEditor.dom.addClass(tinymce.activeEditor.dom.select('p'), 'someclass');
// Adds a class to all spans that have the test class in the currently active editor
tinymce.activeEditor.dom.addClass(tinymce.activeEditor.dom.select('span.test'), 'someclass')
serializeStyle()
serializeStyle(styles: Object, name: String): String
Serializes the specified style object into a string.
setAttrib()
setAttrib(elm: Element | String | Array, name: String, value: String)
Sets the specified attribute of an element or elements.
setAttribs()
setAttribs(elm: Element | String | Array, attrs: Object)
Sets two or more specified attributes of an element or elements.
Examples
// Sets class and title attributes on all paragraphs in the active editor
tinymce.activeEditor.dom.setAttribs(tinymce.activeEditor.dom.select('p'), { 'class': 'myclass', title: 'some title' });
// Sets class and title attributes on a specific element in the current page
tinymce.DOM.setAttribs('mydiv', { 'class': 'myclass', title: 'some title' });
setHTML()
setHTML(elm: Element | String | Array, html: String)
Sets the specified HTML content inside the element or elements. The HTML will first be processed. This means URLs will get converted, hex color values fixed etc. Check processHTML for details.
setOuterHTML()
setOuterHTML(elm: Element | String | Array, html: Object)
Sets the specified outer HTML on an element or elements.
setStyle()
setStyle(elm: String | Element | Array, name: String, value: String)
Sets the CSS style value on a HTML element. The name can be a camelcase string or the CSS style name like background-color.
Examples
// Sets a style value on all paragraphs in the currently active editor
tinymce.activeEditor.dom.setStyle(tinymce.activeEditor.dom.select('p'), 'background-color', 'red');
// Sets a style value to an element by id in the current document
tinymce.DOM.setStyle('mydiv', 'background-color', 'red');
setStyles()
setStyles(elm: Element | String | Array, styles: Object)
Sets multiple styles on the specified element(s).
Examples
// Sets styles on all paragraphs in the currently active editor
tinymce.activeEditor.dom.setStyles(tinymce.activeEditor.dom.select('p'), { 'background-color': 'red', 'color': 'green' });
// Sets styles to an element by id in the current document
tinymce.DOM.setStyles('mydiv', { 'background-color': 'red', 'color': 'green' });
show()
show(elm: String | Element | Array)
Shows the specified element(s) by ID by setting the "display" style.
split()
split(parentElm: Element, splitElm: Element, replacementElm: Element): Element
Splits an element into two new elements and places the specified split
element or elements between the new ones. For example splitting the paragraph at the bold element in
this example <p>abc<b>abc</b>123</p>
would produce <p>abc</p><b>abc</b><p>123</p>
.
toggleClass()
toggleClass(elm: Element, cls: String, state: Boolean)
Toggles the specified class on/off.
unbind()
unbind(target: Element | Document | Window | Array, name: String, func: Function): Boolean | Array
Removes the specified event handler by name and function from an element or collection of elements.