Skip to content

Commit ca971eb

Browse files
author
Carlos Fernando Avila Gratz
committed
Checking in changes prior to tagging of version 0.02.
Changelog diff is: diff --git a/Changes b/Changes index f67c264..1248d36 100644 --- a/Changes +++ b/Changes @@ -2,8 +2,11 @@ Revision history for Perl extension XML::LibXML::jQuery {{$NEXT}} - - improved data() implementation and tests - - not using // operator to support perls 5.10 +0.02 2016-06-26T15:07:06Z + + - implemented all add() signatures + - implemented all data() signatures + - not using // operator to support perls < 5.10 0.01 2016-06-22T18:46:06Z
1 parent 3e8537b commit ca971eb

File tree

6 files changed

+115
-33
lines changed

6 files changed

+115
-33
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ cover_db/
2929
MYMETA.*
3030

3131
/XML::LibXML::jQuery-*
32+
/sandbox/

Changes

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ Revision history for Perl extension XML::LibXML::jQuery
22

33
{{$NEXT}}
44

5-
- improved data() implementation and tests
6-
- not using // operator to support perls 5.10
5+
0.02 2016-06-26T15:07:06Z
6+
7+
- implemented all add() signatures
8+
- implemented all data() signatures
9+
- not using // operator to support perls < 5.10
710

811
0.01 2016-06-22T18:46:06Z
912

META.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@
6666
"web" : "https://github.com/cafe01/xml-libxml-jquery"
6767
}
6868
},
69-
"version" : "0.01",
69+
"version" : "0.02",
7070
"x_authority" : "cpan:CAFEGRATZ"
7171
}

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ Unless otherwise noted, all methods behave exactly like the javascript version.
7575

7676
## add
7777

78+
Implemented signatures:
79+
80+
- **add**(Str **selector**)
81+
- **add**(Str **selector**, XML::LibXML::jQuery **context**)
82+
- **add**(Str **html**)
83+
- **add**(ArrayRef\[[XML::LibXML::Node](https://metacpan.org/pod/XML::LibXML::Node)\] **elements**)
84+
- **add**(XML::LibXML::jQuery **selection**)
85+
86+
Documentation and examples at [http://api.jquery.com/add/](http://api.jquery.com/add/).
87+
7888
## add\_class
7989

8090
## after
@@ -97,6 +107,14 @@ Unless otherwise noted, all methods behave exactly like the javascript version.
97107

98108
## data
99109

110+
Implemented signatures:
111+
112+
- **data**(Str **key**, Any **value**)
113+
- **data**(Str **key**)
114+
- **data**(HashRef **obj**)
115+
116+
Documentation and examples at [http://api.jquery.com/data/](http://api.jquery.com/data/).
117+
100118
## detach
101119

102120
## document

lib/XML/LibXML/jQuery.pm

Lines changed: 73 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use HTML::Selector::XPath qw/selector_to_xpath/;
1010
use Carp qw/ confess /;
1111
use JSON qw/ decode_json /;
1212

13-
our $VERSION = "0.01";
13+
our $VERSION = "0.02";
1414
our @EXPORT = qw/ j /;
1515

1616
use constant {
@@ -29,17 +29,15 @@ my $data = {};
2929

3030
sub new {
3131
my ($class, $stuff, $before) = @_;
32-
my $self;
32+
my ($self, $existing_document, $nodes);
3333

34+
# instance method, reuse document
3435
if (ref $class) {
3536
$self = $class;
3637
$class = ref $self;
38+
$existing_document = $self->{document};
3739
}
3840

39-
$class = ref $class if ref $class;
40-
41-
my $nodes;
42-
4341
if (blessed $stuff) {
4442

4543
if ($stuff->isa(__PACKAGE__)) {
@@ -65,27 +63,41 @@ sub new {
6563
$nodes = [ _parse_html($stuff) ];
6664
}
6765

66+
# # if called as instance method, import nodes to our document
67+
# if (ref $self && defined $self->{nodes}[0]) {
68+
#
69+
# my $doc = $self->{nodes}[0]->ownerDocument;
70+
# $doc->adoptNode($_)
71+
# for grep { $_->nodeType != XML_DOCUMENT_NODE } @$nodes;
72+
# }
6873
}
6974

70-
# if called as instance method, import nodes to our document
71-
if (ref $self && defined $self->{nodes}[0]) {
75+
# catch bugs :)
76+
confess "undefined node" if grep { !defined } @$nodes;
7277

73-
my $doc = $self->{nodes}[0]->ownerDocument;
74-
$doc->adoptNode($_)
75-
for grep { $_->nodeType != XML_DOCUMENT_NODE } @$nodes;
76-
}
78+
# adopt nodes to existing document
79+
if ($existing_document) {
7780

81+
my $doc_id = $existing_document->unique_key;
82+
$existing_document->adoptNode($_)
83+
for grep { $_->ownerDocument->unique_key != $doc_id }
84+
grep { $_->nodeType != XML_DOCUMENT_NODE }
85+
@$nodes;
86+
}
7887

79-
if (@$nodes) {
88+
my $document = defined $nodes->[0] ? $nodes->[0]->ownerDocument
89+
: XML::LibXML->createDocument;
8090

81-
# increment document data refcount
82-
my $doc_id = $nodes->[0]->ownerDocument->unique_key;
83-
$data->{$doc_id}{refcount}++;
84-
# printf STDERR "[%s] incremented document %d data ref count: %d\n", __PACKAGE__, $doc_id, $data->{$doc_id}{refcount};
85-
}
91+
# increment document data refcount
92+
my $doc_id = $document->unique_key;
93+
$data->{$doc_id}{refcount}++;
94+
# printf STDERR "[%s] incremented document %d data ref count: %d\n", __PACKAGE__, $doc_id, $data->{$doc_id}{refcount};
8695

87-
confess "undefined node" if grep { !defined } @$nodes;
88-
bless({ nodes => $nodes, before => $before }, $class);
96+
bless {
97+
document => $document,
98+
nodes => $nodes,
99+
before => $before
100+
}, $class;
89101
}
90102

91103

@@ -159,7 +171,7 @@ sub end {
159171

160172
sub document {
161173
my $self = shift;
162-
$self->new([ $self->{nodes}[0] ? $self->{nodes}[0]->ownerDocument : () ], $self);
174+
$self->new([ $self->{document} ], $self);
163175
}
164176

165177
sub tagname {
@@ -199,16 +211,20 @@ sub as_html {
199211
# TODO benchmark as_html() using can() vs nodeType to detect document nodes
200212
# best method, but only document nodes can toStringHTML()
201213
if ($_->can('toStringHTML')) {
214+
# printf STDERR "%s: toStringHTML\n", ref $_;
202215
$output .= $_->toStringHTML;
203216
next;
204217
}
205218

219+
206220
# second best is to call toStringC14N(1), which generates valid HTML (eg. no auto closed <div/>),
207221
# but dies on some cases with "Failed to convert doc to string in doc->toStringC14N" error.
208222
# so we fallback to toString()
223+
# the most common case where toStringC14N fails is unbound nodes (getOwner returns a DocumentFragment)
209224
{
210225
local $@; # protect existing $@
211226
my $html = eval { $_->toStringC14N(1) };
227+
# printf STDERR "%s: %s\n", ref $_->getOwner, ($@ ? "toString: $@" : 'toStringC14N');
212228
$output .= $@ ? $_->toString : $html;
213229
}
214230
}
@@ -393,7 +409,7 @@ sub append {
393409

394410
sub append_to {
395411
my $self = shift;
396-
_append_to($self->{nodes}, $self->new(@_)->{nodes});
412+
_append_to($self->{nodes}, (ref $self)->new(@_)->{nodes});
397413
$self;
398414
}
399415

@@ -430,7 +446,7 @@ sub prepend {
430446

431447
sub prepend_to {
432448
my $self = shift;
433-
_prepend_to($self->{nodes}, $self->new(@_)->{nodes});
449+
_prepend_to($self->{nodes}, (ref $self)->new(@_)->{nodes});
434450
$self;
435451
}
436452

@@ -474,7 +490,7 @@ sub before {
474490

475491
sub insert_before {
476492
my $self = shift;
477-
_insert_before($self->{nodes}, $self->new(@_)->{nodes});
493+
_insert_before($self->{nodes}, (ref $self)->new(@_)->{nodes});
478494
$self;
479495
}
480496

@@ -520,7 +536,7 @@ sub after {
520536

521537
sub insert_after {
522538
my $self = shift;
523-
_insert_after($self->{nodes}, $self->new(@_)->{nodes});
539+
_insert_after($self->{nodes}, (ref $self)->new(@_)->{nodes});
524540
$self;
525541
}
526542

@@ -993,6 +1009,24 @@ Unless otherwise noted, all methods behave exactly like the javascript version.
9931009
9941010
=head2 add
9951011
1012+
Implemented signatures:
1013+
1014+
=over
1015+
1016+
=item B<add>(Str B<selector>)
1017+
1018+
=item B<add>(Str B<selector>, XML::LibXML::jQuery B<context>)
1019+
1020+
=item B<add>(Str B<html>)
1021+
1022+
=item B<add>(ArrayRef[L<XML::LibXML::Node>] B<elements>)
1023+
1024+
=item B<add>(XML::LibXML::jQuery B<selection>)
1025+
1026+
=back
1027+
1028+
Documentation and examples at L<http://api.jquery.com/add/>.
1029+
9961030
=head2 add_class
9971031
9981032
=head2 after
@@ -1015,6 +1049,20 @@ Unless otherwise noted, all methods behave exactly like the javascript version.
10151049
10161050
=head2 data
10171051
1052+
Implemented signatures:
1053+
1054+
=over
1055+
1056+
=item B<data>(Str B<key>, Any B<value>)
1057+
1058+
=item B<data>(Str B<key>)
1059+
1060+
=item B<data>(HashRef B<obj>)
1061+
1062+
=back
1063+
1064+
Documentation and examples at L<http://api.jquery.com/data/>.
1065+
10181066
=head2 detach
10191067
10201068
=head2 document

t/add.t

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,28 @@ sub test (&@);
1010

1111
test { shift->add('div > bar') } "add(selector)";
1212

13-
test { my $orig = shift; $orig->add('bar', $orig->document->find('baz')) } "add(selector, context)";
13+
test {
14+
my $orig = shift;
15+
$orig->add('bar', $orig->document->find('baz'));
16+
} "add(selector, context)";
1417

1518
test {
1619
my $orig = shift;
17-
$orig->add($orig->document->find('div > bar')->{nodes})
20+
$orig->add($orig->document->find('div > bar')->{nodes});
1821
} "add(elements)";
1922

20-
test { shift->add('<bar/>') } "add(html)";
23+
test {
24+
my $orig = shift;
25+
my $new = $orig->add('<bar/>')->append_to($orig->parent); # append to render as html
26+
$new;
27+
} "add(html)";
28+
29+
test {
30+
my $orig = shift;
31+
my $new = $orig->add(j('<bar/>'))->append_to($orig->parent); # append to render as html
32+
$new;
33+
} "add(jQuery)";
2134

22-
test { shift->add(j('<bar/>')) } "add(jQuery)";
2335

2436
done_testing;
2537

@@ -46,6 +58,6 @@ sub test (&@) {
4658
isnt $orig_obj, $new_obj, "returns new object";
4759
is $orig_obj->size, 1, 'orig object size';
4860
is $new_obj->size, 2, 'new object size';
49-
is $new_obj->as_html, '<foo/><bar/>', 'output';
61+
is $new_obj->as_html, '<foo></foo><bar></bar>', 'output';
5062
};
5163
}

0 commit comments

Comments
 (0)