Report an issue
Class

CKEDITOR.htmlParser.fragment

class

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

  • readonly

    type : Number

    The node type. This is a constant value set to CKEDITOR.NODE_DOCUMENT_FRAGMENT.

    Defaults to CKEDITOR.NODE_DOCUMENT_FRAGMENT

  • private

    _ : Object

    Defaults to {isBlockLike: true, hasInlineStarted: false}

Methods

  • constructor() → fragment

    Creates a fragment class instance.

    Returns

    fragment
  • add( node, [ index ] )

    Adds a node to this fragment.

    Parameters

    node : node

    The node to be added.

    [ index ] : Number

    From where the insertion happens.

  • since 4.1.0

    filter( filter )

    Filter this fragment's content with given filter.

    Parameters

    filter : filter
  • since 4.1.0

    filterChildren( filter, [ filterRoot ] )

    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.

  • since 4.1.0

    forEach( callback, [ type ], [ skipRoot ] )

    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 returned false 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 ] )

    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

  • static

    fromBBCode( source ) → fragment

    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

    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 than false, 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. when fixingBlock specified as p, 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.