CKEDITOR.htmlParser.fragment
A lightweight representation of an HTML DOM structure.
Filtering
Properties
-
children : Array
CKEDITOR.htmlParser.fragment#children
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
CKEDITOR.htmlParser.fragment#parent
-
The node type. This is a constant value set to CKEDITOR.NODE_DOCUMENT_FRAGMENT.
Defaults to
CKEDITOR.NODE_DOCUMENT_FRAGMENT
-
Defaults to
{isBlockLike: true, hasInlineStarted: false}
Methods
-
constructor() → fragment
CKEDITOR.htmlParser.fragment#constructor
-
add( node, [ index ] )
CKEDITOR.htmlParser.fragment#add
Adds a node to this fragment.
Parameters
node : node
The node to be added.
[ index ] : Number
From where the insertion happens.
-
-
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
.
-
Execute callback on each node (of a 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.[ 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 ] )
CKEDITOR.htmlParser.fragment#writeChildrenHtml
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 ] )
CKEDITOR.htmlParser.fragment#writeHtml
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
-
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.
-
static
fromHtml( fragmentHtml, [ parent ], [ fixingBlock ] ) → fragment | element
CKEDITOR.htmlParser.fragment#fromHtml
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
fragment | element
The created fragment or passed
parent
.