1#!/usr/bin/perl
2
3use Test;
4use XML::LibXML;
5
6BEGIN {
7    if (XML::LibXML::LIBXML_VERSION() >= 20623) {
8        plan tests => 42;
9    }
10    else {
11        plan tests => 0;
12        print "# Skipping ID tests on libxml2 <= 2.6.23\n";
13	exit;
14    }
15}
16
17my $parser = XML::LibXML->new;
18
19my $xml1 = <<'EOF';
20<!DOCTYPE root [
21<!ELEMENT root (root?)>
22<!ATTLIST root id ID #REQUIRED
23               notid CDATA #IMPLIED
24>
25]>
26<root id="foo" notid="x"/>
27EOF
28
29my $xml2 = <<'EOF';
30<root2 xml:id="foo"/>
31EOF
32
33sub _debug {
34  my ($msg,$n)=@_;
35  print "$msg\t$$n\n'",(ref $n ? $n->toString : "NULL"),"'\n";
36}
37
38for my $do_validate (0..1) {
39  my ($n,$doc,$root,$at);
40  ok( $doc = $parser->parse_string($xml1) );
41  $root = $doc->getDocumentElement;
42  $n = $doc->getElementById('foo');
43  ok( $root->isSameNode( $n ) );
44
45  # old name
46  $n = $doc->getElementsById('foo');
47  ok( $root->isSameNode( $n ) );
48
49  $at = $n->getAttributeNode('id');
50  ok( $at );
51  ok( $at->isId );
52
53  $at = $root->getAttributeNode('notid');
54  ok( $at->isId == 0 );
55
56  # _debug("1: foo: ",$n);
57  $doc->getDocumentElement->setAttribute('id','bar');
58  ok( $doc->validate ) if $do_validate;
59  $n = $doc->getElementById('bar');
60  ok( $root->isSameNode( $n ) );
61
62  # _debug("1: bar: ",$n);
63  $n = $doc->getElementById('foo');
64  ok( !defined($n) );
65  # _debug("1: !foo: ",$n);
66
67  my $test = $doc->createElement('root');
68  $root->appendChild($test);
69  $test->setAttribute('id','new');
70  ok( $doc->validate ) if $do_validate;
71  $n = $doc->getElementById('new');
72  ok( $test->isSameNode( $n ) );
73
74  $at = $n->getAttributeNode('id');
75  ok( $at );
76  ok( $at->isId );
77  # _debug("1: new: ",$n);
78}
79
80{
81  my ($n,$doc,$root,$at);
82  ok( $doc = $parser->parse_string($xml2) );
83  $root = $doc->getDocumentElement;
84
85  $n = $doc->getElementById('foo');
86  ok( $root->isSameNode( $n ) );
87  # _debug("1: foo: ",$n);
88
89  $doc->getDocumentElement->setAttribute('xml:id','bar');
90  $n = $doc->getElementById('foo');
91  ok( !defined($n) );
92  # _debug("1: !foo: ",$n);
93
94  $n = $doc->getElementById('bar');
95  ok( $root->isSameNode( $n ) );
96
97  $at = $n->getAttributeNode('xml:id');
98  ok( $at );
99  ok( $at->isId );
100
101  $n->setAttribute('id','FOO');
102  ok( $at->isSameNode($n->getAttributeNode('xml:id')) );
103
104  $at = $n->getAttributeNode('id');
105  ok( $at );
106  ok( ! $at->isId );
107
108  $at = $n->getAttributeNodeNS('http://www.w3.org/XML/1998/namespace','id');
109  ok( $at );
110  ok( $at->isId );
111  # _debug("1: bar: ",$n);
112
113  $doc->getDocumentElement->setAttributeNS('http://www.w3.org/XML/1998/namespace','id','baz');
114  $n = $doc->getElementById('bar');
115  ok( !defined($n) );
116  # _debug("1: !bar: ",$n);
117
118  $n = $doc->getElementById('baz');
119  ok( $root->isSameNode( $n ) );
120  # _debug("1: baz: ",$n);
121  $at = $n->getAttributeNodeNS('http://www.w3.org/XML/1998/namespace','id');
122  ok( $at );
123  ok( $at->isId );
124
125  $doc->getDocumentElement->setAttributeNS('http://www.w3.org/XML/1998/namespace','xml:id','bag');
126  $n = $doc->getElementById('baz');
127  ok( !defined($n) );
128  # _debug("1: !baz: ",$n);
129
130  $n = $doc->getElementById('bag');
131  ok( $root->isSameNode( $n ) );
132  # _debug("1: bag: ",$n);
133
134  $n->removeAttribute('id');
135  ok( $root->toString, '<root2 xml:id="bag"/>' );
136}
137
1381;
139