CKEDITOR.htmlWriter
The class used to write HTML data.
var writer = new CKEDITOR.htmlWriter();
writer.openTag( 'p' );
writer.attribute( 'class', 'MyClass' );
writer.openTagClose( 'p' );
writer.text( 'Hello' );
writer.closeTag( 'p' );
alert( writer.getHtml() ); // '<p class="MyClass">Hello</p>'
Filtering
Properties
indentationChars : StringCKEDITOR.htmlWriter#indentationCharsThe characters to be used for each indentation step.
// Use tab for indentation. editorInstance.dataProcessor.writer.indentationChars = '\t';Defaults to
'\t'lineBreakChars : StringCKEDITOR.htmlWriter#lineBreakCharsThe characters to be used for line breaks.
// Use CRLF for line breaks. editorInstance.dataProcessor.writer.lineBreakChars = '\r\n';Defaults to
'\n'selfClosingEnd : StringCKEDITOR.htmlWriter#selfClosingEndThe characters to be used to close "self-closing" elements, like
<br>or<img>.// Use HTML4 notation for self-closing elements. editorInstance.dataProcessor.writer.selfClosingEnd = '>';Defaults to
' />'
Methods
-
-
Writes an attribute. This function should be called after opening the tag with openTagClose.
// Writes ' class="MyClass"'. writer.attribute( 'class', 'MyClass' );Parameters
attName : StringThe attribute name.
attValue : StringThe attribute value.
-
Writes a closer tag.
// Writes '</p>'. writer.closeTag( 'p' );Parameters
tagName : StringThe element name for this tag.
-
Writes a comment.
// Writes '<!-- My comment -->'. writer.comment( ' My comment ' );Parameters
comment : StringThe comment text.
-
Empties the current output buffer.
var html = writer.getHtml();Parameters
reset : BooleanIndicates that the reset method is to be automatically called after retrieving the HTML.
Returns
StringThe HTML written to the writer so far.
indentation()CKEDITOR.htmlWriter#indentationWrites the current indentation character. It uses the indentationChars property, repeating it for the current indentation steps.
// Writes '\t' (e.g.). writer.indentation();lineBreak()CKEDITOR.htmlWriter#lineBreakWrites a line break. It uses the lineBreakChars property for it.
// Writes '\n' (e.g.). writer.lineBreak();-
Writes the tag opening part for a opener tag.
// Writes '<p'. writer.openTag( 'p', { class : 'MyClass', id : 'MyId' } );Parameters
tagName : StringThe element name for this tag.
attributes : ObjectThe attributes defined for this tag. The attributes could be used to inspect the tag.
-
Writes the tag closing part for a opener tag.
// Writes '>'. writer.openTagClose( 'p', false ); // Writes ' />'. writer.openTagClose( 'br', true );Parameters
tagName : StringThe element name for this tag.
isSelfClose : BooleanIndicates that this is a self-closing tag, like
<br>or<img>.
-
setRules( tagName, rules )CKEDITOR.htmlWriter#setRulesSets formatting rules for a given element. Possible rules are:
indent– indent the element content.breakBeforeOpen– break line before the opener tag for this element.breakAfterOpen– break line after the opener tag for this element.breakBeforeClose– break line before the closer tag for this element.breakAfterClose– break line after the closer tag for this element.
All rules default to
false. Each function call overrides rules that are already present, leaving the undefined ones untouched.By default, all elements available in the CKEDITOR.dtd.$block, CKEDITOR.dtd.$listItem, and CKEDITOR.dtd.$tableContent lists have all the above rules set to
true. Additionaly, the<br>element has thebreakAfterOpenrule set totrue.// Break line before and after "img" tags. writer.setRules( 'img', { breakBeforeOpen: true breakAfterOpen: true } ); // Reset the rules for the "h1" tag. writer.setRules( 'h1', {} );Parameters
tagName : StringThe name of the element for which the rules are set.
rules : ObjectAn object containing the element rules.
-
Writes text.
// Writes 'Hello Word'. writer.text( 'Hello Word' );Parameters
text : StringThe text value.
-
Writes any kind of data to the ouput.
writer.write( 'This is an <b>example</b>.' );Parameters
data : String