Ersun Warncke
and 1 contributors

NAME

HTML::Xit - XML/HTML DOM Manipulation with CSS Selectors

SYNOPSIS

    my $X = HTML::Xit->new("http://mysite.com/mydoc.html");

    $X->("a")->each( sub {
        my($X) = @_;

        print $X->attr("href");
        print $X->text;
    } );

    $X->(".a")->addClass("b c d")->removeClass("c e")->toggleClass("b a");

    print $X->("<a>")->attr("href", "http://mysite.com")->text("My Site")->html;

DESCRIPTION

DOM manipulation in the style of jQuery using XML::LibXML and HTML::Selector::XPath.

METHODS

new

Create a new HTML::Xit instance. The source can be a URL (which will be fetched), a filename, a file handle, or a string.

    HTML::Xit->new("http://www.some.com");
    HTML::Xit->new("somefile.html");
    HTML::Xit->new($html);

The source will be parsed using XML::LibXML. By default the following args are used:

    recover  => 1,
    suppress_errors => 1,
    suppress_warnings => 1,

You can pass arguments for use when instantiating XML::LibXML.

    HTML::Xit->new({
        recover => 0,
        location => "http://www.some.com",
    });

These arguments will be passed to XML::LibXML->load_html which is documented in XML::LibXML::Parser.

addClass
    $X->addClass("a b");

Add one or more classes to the matching elements. Class names must be space separated.

append
    $X->append("<span>Hello</span>");
    $X->append( $X->("<span>")->text("Hello") );

Insert the content passed after the last child node for each matching element.

attr
    $X->attr("href");
    $X->attr("href", "http://www.some.com");

Either get or set an attribute.

children
    $X->children();

Get child nodes for each matching element.

classes
    my $hash_ref = $X->classes();
    $hash_ref->{someClass} ? 1 : 0;

Get hashref containing the names of all of the classes for the matching element.

!! NOT STANDARD JQUERY !!

each
    $X->each(sub {
        my($X) = @_;
    });

Interate over each matching element, calling the callback function for each.

find
    $X->(".foo")->find(".bar");

Search matching elements for additional elements that match the given selector.

first
    my $h1 = $X->("h1")->first;

Get the first matching element.

get
    my $nodes = $X->("p")->get

Get the list of XML nodes that are currently selected.

hasClass
    $X->addClass("a");
    $X->hasClass("a"); # true

Will return true if the selected element has the given class. If multiple elements are selected this will return true if any of the selected elements has the class.

html
    $X->html("<span>Hello</span>");
    $X->append( $X->("<span>")->text("Hello") );
    $X->html(); # "<span>Hello</span>"

If an HTML string or HTML objects are passed this will set the inner HTML for each selected element.

If no HTML is passed this will return the inner HTML of the first selected element as a string.

last
    $X->("a")->last();

Return the last selected element.

prepend
    $X->prepend("<span>Hello</span>");
    $X->prepend( $X->("<span>")->text("Hello") );

Insert the content passed before the first child node for each matching element.

removeClass
    $X->removeClass("a b");

Remove one or more classes from the matching elements. Class names must be space separated.

text
    $X->text("Hello");
    $X->text(); # Hello

If a string is passed this will be set as the text content for all selected elements.

If no text is passed then the text content of the first selected element will be returned.

trimText
    $X->trimText();

Return $X->text() with leading and trailing whitespace removed.

!! NOT STANDARD JQUERY !!

toggleClass
    $X->toggleClass("a b");

Toggle one or more classes from the matching elements. If the class exists it will be removed. If the class does not exist it will be added. Class names must be space separated.

SEE ALSO

XML::LibXML, HTML::Selector::XPath

AUTHOR

Ersun Warncke, <ersun.warncke at outlook.com>

http://ersun.warnckes.com

COPYRIGHT

Copyright (C) 2014 Ersun Warncke

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.