CKEDITOR.htmlParser.fragment
A lightweight representation of an HTML DOM structure.
Filtering
Properties
-
children : Array
The nodes contained in the root of this fragment.
var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<b>Sample</b> Text' ); alert( fragment.children.length ); // 2
Defaults to
[]
-
parent : Object
Get the fragment parent. Should always be null.
Defaults to
null
-
type : Number
readonly
The node type. This is a constant value set to CKEDITOR.NODE_DOCUMENT_FRAGMENT.
Defaults to
CKEDITOR.NODE_DOCUMENT_FRAGMENT
-
_ : Object
private
Defaults to
{isBlockLike: true, hasInlineStarted: false}
Methods
-
constructor() → fragment
Creates a fragment class instance.
Returns
-
add( node, [ index ] )
Adds a node to this fragment.
Parameters
node : node
The node to be added.
[ index ] : Number
From where the insertion happens.
-
filter( filter )
since 4.1.0
Filter this fragment's content with given filter.
Parameters
filter : filter
-
filterChildren( filter, [ filterRoot ] )
since 4.1.0
Filter this fragment's children with given filter.
Element's children may only be filtered once by one instance of filter.
Parameters
filter : filter
[ filterRoot ] : Boolean
Whether to apply the "root" filter rule specified in the
filter
.
-
forEach( callback, [ type ], [ skipRoot ] )
since 4.1.0
Execute callback on each node (of given type) in this document fragment.
var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<p>foo<b>bar</b>bom</p>' ); fragment.forEach( function( node ) { console.log( node ); } ); // Will log: // 1. document fragment, // 2. <p> element, // 3. "foo" text node, // 4. <b> element, // 5. "bar" text node, // 6. "bom" text node.
Parameters
callback : Function
Function to be executed on every node. Since 4.3.0 if
callback
returnedfalse
descendants of current node will be ignored.Propertiesnode : node
Node passed as argument.
[ type ] : Number
If specified
callback
will be executed only on nodes of this type.[ skipRoot ] : Boolean
Don't execute
callback
on this fragment.
-
writeChildrenHtml( writer, [ filter ], [ filterRoot ] )
Write and filtering the child nodes of this fragment.
Parameters
writer : basicWriter
The writer to which write the HTML.
[ filter ] : filter
The filter to use when writing the HTML.
[ filterRoot ] : Boolean
Whether to apply the "root" filter rule specified in the
filter
.
-
writeHtml( writer, [ filter ] )
Writes the fragment HTML to a CKEDITOR.htmlParser.basicWriter.
var writer = new CKEDITOR.htmlWriter(); var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<P><B>Example' ); fragment.writeHtml( writer ); alert( writer.getHtml() ); // '<p><b>Example</b></p>'
Parameters
writer : basicWriter
The writer to which write the HTML.
[ filter ] : filter
The filter to use when writing the HTML.
Static methods
-
fromBBCode( source ) → fragment
static
Creates a CKEDITOR.htmlParser.fragment from an HTML string.
var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<b>Sample</b> Text' ); alert( fragment.children[ 0 ].name ); // 'b' alert( fragment.children[ 1 ].value ); // ' Text'
Parameters
source : String
The HTML to be parsed, filling the fragment.
Returns
fragment
The fragment created.
-
Creates a CKEDITOR.htmlParser.fragment from an HTML string.
var fragment = CKEDITOR.htmlParser.fragment.fromHtml( '<b>Sample</b> Text' ); alert( fragment.children[ 0 ].name ); // 'b' alert( fragment.children[ 1 ].value ); // ' Text'
Parameters
fragmentHtml : String
The HTML to be parsed, filling the fragment.
[ parent ] : element | String
Optional contextual element which makes the content been parsed as the content of this element and fix to match it. If not provided, then CKEDITOR.htmlParser.fragment will be used as the parent and it will be returned.
[ fixingBlock ] : String | Boolean
When
parent
is a block limit element, and the param is a string value other thanfalse
, it is to avoid having block-less content as the direct children of parent by wrapping the content with a block element of the specified tag, e.g. whenfixingBlock
specified asp
, the content<body><i>foo</i></body>
will be fixed into<body><p><i>foo</i></p></body>
.
Returns