Source

perl-XML-LibXML / t / 06elements.t

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# $Id$

##
# this test checks the DOM element and attribute interface of XML::LibXML

use strict;
use warnings;

# Should be 187.
use Test::More tests => 191;

use XML::LibXML;

my $foo       = "foo";
my $bar       = "bar";
my $nsURI     = "http://foo";
my $prefix    = "x";
my $attname1  = "A";
my $attvalue1 = "a";
my $attname2  = "B";
my $attvalue2 = "b";
my $attname3  = "C";

# TEST:$badnames=4;
my @badnames= ("1A", "<><", "&", "-:");

# 1. bound node
{
    my $doc = XML::LibXML::Document->new();
    my $elem = $doc->createElement( $foo );
    # TEST
    ok($elem, ' TODO : Add test name');
    # TEST
    is($elem->tagName, $foo, ' TODO : Add test name');
    
    {
        foreach my $name ( @badnames ) {
            eval { $elem->setNodeName( $name ); };
            # TEST*$badnames
            ok( $@, "setNodeName throws an exception for $name" );
        }
    }
    
    $elem->setAttribute( $attname1, $attvalue1 );
    # TEST
    ok( $elem->hasAttribute($attname1), ' TODO : Add test name' );
    # TEST
    is( $elem->getAttribute($attname1), $attvalue1, ' TODO : Add test name');

    my $attr = $elem->getAttributeNode($attname1);
    # TEST
    ok($attr, ' TODO : Add test name');
    # TEST
    is($attr->name, $attname1, ' TODO : Add test name');
    # TEST
    is($attr->value, $attvalue1, ' TODO : Add test name');

    $elem->setAttribute( $attname1, $attvalue2 );
    # TEST
    is($elem->getAttribute($attname1), $attvalue2, ' TODO : Add test name');
    # TEST
    is($attr->value, $attvalue2, ' TODO : Add test name');

    my $attr2 = $doc->createAttribute($attname2, $attvalue1);
    # TEST
    ok($attr2, ' TODO : Add test name');

    $elem->setAttributeNode($attr2);
    # TEST
    ok($elem->hasAttribute($attname2), ' TODO : Add test name' );
    # TEST
    is($elem->getAttribute($attname2),$attvalue1, ' TODO : Add test name');

    my $tattr = $elem->getAttributeNode($attname2);
    # TEST
    ok($tattr->isSameNode($attr2), ' TODO : Add test name');

    $elem->setAttribute($attname2, "");    
    # TEST
    ok($elem->hasAttribute($attname2), ' TODO : Add test name' );
    # TEST
    is($elem->getAttribute($attname2), "", ' TODO : Add test name');
    
    $elem->setAttribute($attname3, "");    
    # TEST
    ok($elem->hasAttribute($attname3), ' TODO : Add test name' );
    # TEST
    is($elem->getAttribute($attname3), "", ' TODO : Add test name');

    {
        foreach my $name ( @badnames ) {
            eval {$elem->setAttribute( $name, "X" );};
            # TEST*$badnames
            ok( $@, "setAttribute throws an exxception for '$name'" );
        }

    }


    # 1.1 Namespaced Attributes

    $elem->setAttributeNS( $nsURI, $prefix . ":". $foo, $attvalue2 );
    # TEST
    ok( $elem->hasAttributeNS( $nsURI, $foo ), ' TODO : Add test name' );
    # TEST
    ok( ! $elem->hasAttribute( $foo ), ' TODO : Add test name' );
    # TEST
    ok( $elem->hasAttribute( $prefix.":".$foo ), ' TODO : Add test name' );
    # warn $elem->toString() , "\n";
    $tattr = $elem->getAttributeNodeNS( $nsURI, $foo );
    # TEST
    ok($tattr, ' TODO : Add test name');
    # TEST
    is($tattr->name, $foo, ' TODO : Add test name');
    # TEST
    is($tattr->nodeName, $prefix .":".$foo, ' TODO : Add test name');
    # TEST
    is($tattr->value, $attvalue2, ' TODO : Add test name' );

    $elem->removeAttributeNode( $tattr );
    # TEST
    ok( !$elem->hasAttributeNS($nsURI, $foo), ' TODO : Add test name' );


    # empty NS
    $elem->setAttributeNS( '', $foo, $attvalue2 );
    # TEST
    ok( $elem->hasAttribute( $foo ), ' TODO : Add test name' );
    $tattr = $elem->getAttributeNode( $foo );
    # TEST
    ok($tattr, ' TODO : Add test name');
    # TEST
    is($tattr->name, $foo, ' TODO : Add test name');
    # TEST
    is($tattr->nodeName, $foo, ' TODO : Add test name');
    # TEST
    ok(!defined($tattr->namespaceURI), ' TODO : Add test name');
    # TEST
    is($tattr->value, $attvalue2, ' TODO : Add test name' );

    # TEST

    ok($elem->hasAttribute($foo) == 1, ' TODO : Add test name');
    # TEST
    ok($elem->hasAttributeNS(undef, $foo) == 1, ' TODO : Add test name');
    # TEST
    ok($elem->hasAttributeNS('', $foo) == 1, ' TODO : Add test name');
     
    $elem->removeAttributeNode( $tattr );
    # TEST
    ok( !$elem->hasAttributeNS('', $foo), ' TODO : Add test name' );
    # TEST
    ok( !$elem->hasAttributeNS(undef, $foo), ' TODO : Add test name' );

    # node based functions
    my $e2 = $doc->createElement($foo);
    $doc->setDocumentElement($e2);
    my $nsAttr = $doc->createAttributeNS( $nsURI.".x", $prefix . ":". $foo, $bar);
    # TEST
    ok( $nsAttr, ' TODO : Add test name' );
    $elem->setAttributeNodeNS($nsAttr);
    # TEST
    ok( $elem->hasAttributeNS($nsURI.".x", $foo), ' TODO : Add test name' );    
    $elem->removeAttributeNS( $nsURI.".x", $foo);
    # TEST
    ok( !$elem->hasAttributeNS($nsURI.".x", $foo), ' TODO : Add test name' );

    # warn $elem->toString;
    $elem->setAttributeNS( $nsURI, $prefix . ":". $attname1, $attvalue2 );
    # warn $elem->toString;


    $elem->removeAttributeNS("",$attname1);
    # warn $elem->toString;

    # TEST

    ok( ! $elem->hasAttribute($attname1), ' TODO : Add test name' );
    # TEST
    ok( $elem->hasAttributeNS($nsURI,$attname1), ' TODO : Add test name' );
    # warn $elem->toString;

    {
        foreach my $name ( @badnames ) {
            eval {$elem->setAttributeNS( undef, $name, "X" );};
            # TEST*$badnames
            ok( $@, "setAttributeNS throws an exception for '$name'");
        }
    }
} 

# 2. unbound node
{
    my $elem = XML::LibXML::Element->new($foo);
    # TEST
    ok($elem, ' TODO : Add test name');
    # TEST
    is($elem->tagName, $foo, ' TODO : Add test name');

    $elem->setAttribute( $attname1, $attvalue1 );
    # TEST
    ok( $elem->hasAttribute($attname1), ' TODO : Add test name' );
    # TEST
    is( $elem->getAttribute($attname1), $attvalue1, ' TODO : Add test name');

    my $attr = $elem->getAttributeNode($attname1);
    # TEST
    ok($attr, ' TODO : Add test name');
    # TEST
    is($attr->name, $attname1, ' TODO : Add test name');
    # TEST
    is($attr->value, $attvalue1, ' TODO : Add test name');

    $elem->setAttributeNS( $nsURI, $prefix . ":". $foo, $attvalue2 );
    # TEST
    ok( $elem->hasAttributeNS( $nsURI, $foo ), ' TODO : Add test name' );
    # warn $elem->toString() , "\n";
    my $tattr = $elem->getAttributeNodeNS( $nsURI, $foo );
    # TEST
    ok($tattr, ' TODO : Add test name');
    # TEST
    is($tattr->name, $foo, ' TODO : Add test name');
    # TEST
    is($tattr->nodeName, $prefix .":".$foo, ' TODO : Add test name');
    # TEST
    is($tattr->value, $attvalue2, ' TODO : Add test name' );

    $elem->removeAttributeNode( $tattr );
    # TEST
    ok( !$elem->hasAttributeNS($nsURI, $foo), ' TODO : Add test name' );
    # warn $elem->toString() , "\n";
}

# 3. Namespace handling
# 3.1 Namespace switching
{
    my $elem = XML::LibXML::Element->new($foo);
    # TEST
    ok($elem, ' TODO : Add test name');

    my $doc = XML::LibXML::Document->new();
    my $e2 = $doc->createElement($foo);
    $doc->setDocumentElement($e2);
    my $nsAttr = $doc->createAttributeNS( $nsURI, $prefix . ":". $foo, $bar);
    # TEST
    ok( $nsAttr, ' TODO : Add test name' );

    $elem->setAttributeNodeNS($nsAttr);
    # TEST
    ok( $elem->hasAttributeNS($nsURI, $foo), ' TODO : Add test name' );    

    # TEST
    ok( ! defined $nsAttr->ownerDocument, ' TODO : Add test name');
    # warn $elem->toString() , "\n";
} 

# 3.2 default Namespace and Attributes
{
    my $doc  = XML::LibXML::Document->new();
    my $elem = $doc->createElementNS( "foo", "root" );
    $doc->setDocumentElement( $elem );

    $elem->setNamespace( "foo", "bar" );

    $elem->setAttributeNS( "foo", "x:attr",  "test" );
    $elem->setAttributeNS( undef, "attr2",  "test" );

    # TEST

    is( $elem->getAttributeNS( "foo", "attr" ), "test", ' TODO : Add test name' );
    # TEST
    is( $elem->getAttributeNS( "", "attr2" ), "test", ' TODO : Add test name' );

    # warn $doc->toString;
    # actually this doesn't work correctly with libxml2 <= 2.4.23
    $elem->setAttributeNS( "foo", "attr2",  "bar" );
    # TEST
    is( $elem->getAttributeNS( "foo", "attr2" ), "bar", ' TODO : Add test name' );
    # warn $doc->toString;
}

# 4. Text Append and Normalization
# 4.1 Normalization on an Element node
{
    my $doc = XML::LibXML::Document->new();
    my $t1 = $doc->createTextNode( "bar1" );
    my $t2 = $doc->createTextNode( "bar2" );
    my $t3 = $doc->createTextNode( "bar3" );
    my $e  = $doc->createElement("foo");
    my $e2 = $doc->createElement("bar");
    $e->appendChild( $e2 );
    $e->appendChild( $t1 );
    $e->appendChild( $t2 );
    $e->appendChild( $t3 );

    my @cn = $e->childNodes;

    # this is the correct behaviour for DOM. the nodes are still
    # refered
    # TEST
    is( scalar( @cn ), 4, ' TODO : Add test name' );
    
    $e->normalize;

    @cn = $e->childNodes;
    # TEST
    is( scalar( @cn ), 2, ' TODO : Add test name' );

    # TEST

    ok(! defined $t2->parentNode, ' TODO : Add test name');
    # TEST
    ok(! defined $t3->parentNode, ' TODO : Add test name');
}

# 4.2 Normalization on a Document node
{
    my $doc = XML::LibXML::Document->new();
    my $t1 = $doc->createTextNode( "bar1" );
    my $t2 = $doc->createTextNode( "bar2" );
    my $t3 = $doc->createTextNode( "bar3" );
    my $e  = $doc->createElement("foo");
    my $e2 = $doc->createElement("bar");
    $doc->setDocumentElement($e);
    $e->appendChild( $e2 );
    $e->appendChild( $t1 );
    $e->appendChild( $t2 );
    $e->appendChild( $t3 );

    my @cn = $e->childNodes;

    # this is the correct behaviour for DOM. the nodes are still
    # refered
    # TEST
    is( scalar( @cn ), 4, ' TODO : Add test name' );

    $doc->normalize;

    @cn = $e->childNodes;
    # TEST
    is( scalar( @cn ), 2, ' TODO : Add test name' );

    # TEST

    ok(! defined $t2->parentNode, ' TODO : Add test name');
    # TEST
    ok(! defined $t3->parentNode, ' TODO : Add test name');
}


# 5. XML::LibXML extensions
{
    my $plainstring = "foo";
    my $stdentstring= "$foo & this";
    
    my $doc = XML::LibXML::Document->new();
    my $elem = $doc->createElement( $foo );
    $doc->setDocumentElement( $elem );
    
    $elem->appendText( $plainstring );
    # TEST
    is( $elem->string_value , $plainstring, ' TODO : Add test name' );

    $elem->appendText( $stdentstring );
    # TEST
    is( $elem->string_value , $plainstring.$stdentstring, ' TODO : Add test name' );

    $elem->appendTextChild( "foo");
    $elem->appendTextChild( "foo" => "foo&bar" );

    my @cn = $elem->childNodes;
    # TEST
    ok( scalar(@cn), ' TODO : Add test name' );
    # TEST
    is( scalar(@cn), 3, ' TODO : Add test name' );
    # TEST
    ok( !$cn[1]->hasChildNodes, ' TODO : Add test name');
    # TEST
    ok( $cn[2]->hasChildNodes, ' TODO : Add test name');
}

# 6. XML::LibXML::Attr nodes
{
    my $dtd = <<'EOF';
<!DOCTYPE root [
<!ELEMENT root EMPTY>
<!ATTLIST root fixed CDATA  #FIXED "foo">
<!ATTLIST root a:ns_fixed CDATA  #FIXED "ns_foo">
<!ATTLIST root name NMTOKEN #IMPLIED>
<!ENTITY ent "ENT">
]>
EOF
    my $ns = q(urn:xx);
    my $xml_nons = qq(<root foo="&quot;bar&ent;&quot;" xmlns:a="$ns"/>);
    my $xml_ns = qq(<root xmlns="$ns" xmlns:a="$ns" foo="&quot;bar&ent;&quot;"/>);

    # TEST:$xml=2;
    for my $xml ($xml_nons, $xml_ns) {
        my $parser = new XML::LibXML;
        $parser->complete_attributes(0);
        $parser->expand_entities(0);
        my $doc = $parser->parse_string($dtd.$xml);

        # TEST*$xml

        ok ($doc, ' TODO : Add test name');
        my $root = $doc->getDocumentElement;
        {
            my $attr = $root->getAttributeNode('foo');
            # TEST*$xml
            ok ($attr, ' TODO : Add test name');
            # TEST*$xml
            is (ref($attr), 'XML::LibXML::Attr', ' TODO : Add test name');
            # TEST*$xml
            ok ($root->isSameNode($attr->ownerElement), ' TODO : Add test name');
            # TEST*$xml
            is ($attr->value, '"barENT"', ' TODO : Add test name');
            # TEST*$xml
            is ($attr->serializeContent, '&quot;bar&ent;&quot;', ' TODO : Add test name');
            # TEST*$xml
            is ($attr->toString, ' foo="&quot;bar&ent;&quot;"', ' TODO : Add test name');
        }
        {
            my $attr = $root->getAttributeNodeNS(undef,'foo');
            # TEST*$xml
            ok ($attr, ' TODO : Add test name');
            # TEST*$xml
            is (ref($attr), 'XML::LibXML::Attr', ' TODO : Add test name');
            # TEST*$xml
            ok ($root->isSameNode($attr->ownerElement), ' TODO : Add test name');
            # TEST*$xml
            is ($attr->value, '"barENT"', ' TODO : Add test name');
        }
        # fixed values are defined
        # TEST*$xml
        is ($root->getAttribute('fixed'),'foo', ' TODO : Add test name');

        SKIP:
        {
            if (XML::LibXML::LIBXML_VERSION() < 20627)
            {
                skip('skipping for libxml2 < 2.6.27', 1);
            }
            # TEST*$xml
            is($root->getAttributeNS($ns,'ns_fixed'),'ns_foo', 'ns_fixed is ns_foo')
        }

        # TEST*$xml
        is ($root->getAttribute('a:ns_fixed'),'ns_foo', ' TODO : Add test name');

        # TEST*$xml

        is ($root->hasAttribute('fixed'),0, ' TODO : Add test name');
        # TEST*$xml
        is ($root->hasAttributeNS($ns,'ns_fixed'),0, ' TODO : Add test name');
        # TEST*$xml
        is ($root->hasAttribute('a:ns_fixed'),0, ' TODO : Add test name');


        # but no attribute nodes correspond to them
        # TEST*$xml
        ok (!defined $root->getAttributeNode('a:ns_fixed'), ' TODO : Add test name');
        # TEST*$xml
        ok (!defined $root->getAttributeNode('fixed'), ' TODO : Add test name');
        # TEST*$xml
        ok (!defined $root->getAttributeNode('name'), ' TODO : Add test name');
        # TEST*$xml
        ok (!defined $root->getAttributeNode('baz'), ' TODO : Add test name');
        # TEST*$xml
        ok (!defined $root->getAttributeNodeNS($ns,'foo'), ' TODO : Add test name');
        # TEST*$xml
        ok (!defined $root->getAttributeNodeNS($ns,'fixed'), ' TODO : Add test name');
        # TEST*$xml
        ok (!defined $root->getAttributeNodeNS($ns,'ns_fixed'), ' TODO : Add test name');
        # TEST*$xml
        ok (!defined $root->getAttributeNodeNS(undef,'fixed'), ' TODO : Add test name');
        # TEST*$xml
        ok (!defined $root->getAttributeNodeNS(undef,'name'), ' TODO : Add test name');
        # TEST*$xml
        ok (!defined $root->getAttributeNodeNS(undef,'baz'), ' TODO : Add test name');
    }

    # TEST:$xml=2;
    {
    my @names = ("nons", "ns");
    for my $xml ($xml_nons, $xml_ns) {
        my $n = shift(@names);
        my $parser = new XML::LibXML;
        $parser->complete_attributes(1);
        $parser->expand_entities(1);
        my $doc = $parser->parse_string($dtd.$xml);
        # TEST*$xml
        ok ($doc, "Could parse document $n");
        my $root = $doc->getDocumentElement;
        {
            my $attr = $root->getAttributeNode('foo');
            # TEST*$xml
            ok ($attr, "Attribute foo exists for $n");
            # TEST*$xml
            isa_ok ($attr, 'XML::LibXML::Attr', 
                "Attribute is of type XML::LibXML::Attr - $n");
            # TEST*$xml
            ok ($root->isSameNode($attr->ownerElement), 
                "attr owner element is root - $n");
            # TEST*$xml
            is ($attr->value, q{"barENT"}, 
                "attr value is OK - $n");
            # TEST*$xml
            is ($attr->serializeContent, 
                '&quot;barENT&quot;',
                "serializeContent - $n");
            # TEST*$xml
            is ($attr->toString, ' foo="&quot;barENT&quot;"', 
                "toString - $n");
        }
        # fixed values are defined
        # TEST*$xml
        is ($root->getAttribute('fixed'),'foo', ' TODO : Add test name');
        # TEST*$xml
        is ($root->getAttributeNS($ns,'ns_fixed'),'ns_foo', ' TODO : Add test name');
        # TEST*$xml
        is ($root->getAttribute('a:ns_fixed'),'ns_foo', ' TODO : Add test name');

        # and attribute nodes are created
        {
            my $attr = $root->getAttributeNode('fixed');
            # TEST*$xml
            is (ref($attr), 'XML::LibXML::Attr', ' TODO : Add test name');
            # TEST*$xml
            is ($attr->value,'foo', ' TODO : Add test name');
            # TEST*$xml
            is ($attr->toString, ' fixed="foo"', ' TODO : Add test name');
        }
        {
            my $attr = $root->getAttributeNode('a:ns_fixed');
            # TEST*$xml
            is (ref($attr), 'XML::LibXML::Attr', ' TODO : Add test name');
            # TEST*$xml
            is ($attr->value,'ns_foo', ' TODO : Add test name');
        }
        {
            my $attr = $root->getAttributeNodeNS($ns,'ns_fixed');
            # TEST*$xml
            is (ref($attr), 'XML::LibXML::Attr', ' TODO : Add test name');
            # TEST*$xml
            is ($attr->value,'ns_foo', ' TODO : Add test name');
            # TEST*$xml
            is ($attr->toString, ' a:ns_fixed="ns_foo"', ' TODO : Add test name');
        }

        # TEST*$xml

        ok (!defined $root->getAttributeNode('ns_fixed'), ' TODO : Add test name');
        # TEST*$xml
        ok (!defined $root->getAttributeNode('name'), ' TODO : Add test name');
        # TEST*$xml
        ok (!defined $root->getAttributeNode('baz'), ' TODO : Add test name');
        # TEST*$xml
        ok (!defined $root->getAttributeNodeNS($ns,'foo'), ' TODO : Add test name');
        # TEST*$xml
        ok (!defined $root->getAttributeNodeNS($ns,'fixed'), ' TODO : Add test name');
        # TEST*$xml
        ok (!defined $root->getAttributeNodeNS(undef,'name'), ' TODO : Add test name');
        # TEST*$xml
        ok (!defined $root->getAttributeNodeNS(undef,'baz'), ' TODO : Add test name');
    }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.