XML-LibXML-jQuery
view release on metacpan or search on metacpan
lib/XML/LibXML/jQuery.pm view on Meta::CPAN
package XML::LibXML::jQuery;
use 5.008001;
use strict;
use warnings;
use Exporter qw(import);
use Scalar::Util qw/ blessed /;
use XML::LibXML;
use HTML::Selector::XPath qw/selector_to_xpath/;
use Carp qw/ confess /;
use JSON qw/ decode_json /;
our $VERSION = "0.08";
our @EXPORT_OK = qw/ j fn /;
our @EXPORT = qw/ j /;
use constant {
XML_ELEMENT_NODE => 1,
XML_TEXT_NODE => 3,
XML_COMMENT_NODE => 8,
XML_DOCUMENT_NODE => 9,
XML_DOCUMENT_FRAG_NODE => 11,
XML_HTML_DOCUMENT_NODE => 13
};
our ($PARSER);
# plugin functions
my %fn;
# for data()
my $data = {};
sub fn($$) {
my ($name, $sub) = @_;
die sprintf("fn '$name' already defined by %s (at %s line %s)", @{$fn{$name}->{caller}})
if exists $fn{$name};
$fn{$name} = {
sub => $sub,
caller => [caller]
};
}
#*j = \&jQuery;
sub j {
__PACKAGE__->new(@_);
}
sub new {
my ($class, $stuff, $before) = @_;
my ($self, $document, $nodes);
# instance method, reuse document
if (ref $class) {
$self = $class;
$class = ref $self;
$document = $self->{document};
}
if (defined $stuff) {
$nodes = _stuff_to_nodes($stuff);
# catch bugs :)
# confess "undefined node" if grep { !defined } @$nodes;
# adopt nodes to existing document
# - if its not in the same dorcument already
# - if its not a document node
# - testing only first node for better performance
if (defined $document
&& defined $nodes->[0]
&& $nodes->[0] ->nodeType != XML_DOCUMENT_NODE
&& !$nodes->[0]->ownerDocument->isSameNode($document)) {
# my $doc_id = $existing_document->unique_key;
foreach my $n (@$nodes) {
$document->adoptNode($n);
}
}
}
# resolve document
unless (defined $document) {
$document = defined $nodes->[0] ? $nodes->[0]->ownerDocument
: XML::LibXML->createDocument;
}
( run in 0.839 second using v1.01-cache-2.11-cpan-524268b4103 )