Newer Older
798 lines | 18.757kb
copy gitweblite soruce code
root authored on 2012-11-23
1
package Mojo::DOM;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
2
use Mojo::Base -strict;
copy gitweblite soruce code
root authored on 2012-11-23
3
use overload
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
4
  '%{}'    => sub { shift->attr },
5
  bool     => sub {1},
copy gitweblite soruce code
root authored on 2012-11-23
6
  '""'     => sub { shift->to_xml },
7
  fallback => 1;
8

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
9
# "Fry: This snow is beautiful. I'm glad global warming never happened.
10
#  Leela: Actually, it did. But thank God nuclear winter canceled it out."
copy gitweblite soruce code
root authored on 2012-11-23
11
use Carp 'croak';
12
use Mojo::Collection;
13
use Mojo::DOM::CSS;
14
use Mojo::DOM::HTML;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
15
use Mojo::Util 'squish';
16
use Scalar::Util qw(blessed weaken);
copy gitweblite soruce code
root authored on 2012-11-23
17

            
18
sub AUTOLOAD {
19
  my $self = shift;
20

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
21
  my ($package, $method) = our $AUTOLOAD =~ /^([\w:]+)::(\w+)$/;
22
  croak "Undefined subroutine &${package}::$method called"
copy gitweblite soruce code
root authored on 2012-11-23
23
    unless blessed $self && $self->isa(__PACKAGE__);
24

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
25
  # Search children of current element
copy gitweblite soruce code
root authored on 2012-11-23
26
  my $children = $self->children($method);
27
  return @$children > 1 ? $children : $children->[0] if @$children;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
28
  croak qq{Can't locate object method "$method" via package "$package"};
copy gitweblite soruce code
root authored on 2012-11-23
29
}
30

            
31
sub DESTROY { }
32

            
33
sub new {
34
  my $class = shift;
35
  my $self = bless [Mojo::DOM::HTML->new], ref $class || $class;
36
  return @_ ? $self->parse(@_) : $self;
37
}
38

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
39
sub all_text { shift->_content(1, @_) }
40

            
41
sub ancestors { _select($_[0]->_collect(_ancestors($_[0]->tree)), $_[1]) }
copy gitweblite soruce code
root authored on 2012-11-23
42

            
43
sub append { shift->_add(1, @_) }
44

            
45
sub append_content {
46
  my ($self, $new) = @_;
47
  my $tree = $self->tree;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
48
  push @$tree, _link($self->_parse("$new"), $tree);
copy gitweblite soruce code
root authored on 2012-11-23
49
  return $self;
50
}
51

            
52
sub at { shift->find(@_)->[0] }
53

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
54
sub attr {
copy gitweblite soruce code
root authored on 2012-11-23
55
  my $self = shift;
56

            
57
  # Hash
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
58
  my $tree = $self->tree;
59
  my $attrs = $tree->[0] eq 'root' ? {} : $tree->[2];
copy gitweblite soruce code
root authored on 2012-11-23
60
  return $attrs unless @_;
61

            
62
  # Get
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
63
  return defined $attrs->{$_[0]} ? $attrs->{$_[0]} : '' unless @_ > 1 || ref $_[0];
copy gitweblite soruce code
root authored on 2012-11-23
64

            
65
  # Set
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
66
  %$attrs = (%$attrs, %{ref $_[0] ? $_[0] : {@_}});
copy gitweblite soruce code
root authored on 2012-11-23
67

            
68
  return $self;
69
}
70

            
71
sub children {
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
72
  my $self = shift;
73
  return _select(
74
    $self->_collect(grep { $_->[0] eq 'tag' } _nodes($self->tree)), @_);
copy gitweblite soruce code
root authored on 2012-11-23
75
}
76

            
77
sub content_xml {
78
  my $self = shift;
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
79
  my $xml  = $self->xml;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
80
  return join '', map { _render($_, $xml) } _nodes($self->tree);
copy gitweblite soruce code
root authored on 2012-11-23
81
}
82

            
83
sub find {
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
84
  my $self = shift;
85
  my $results = Mojo::DOM::CSS->new(tree => $self->tree)->select(@_);
86
  return $self->_collect(@$results);
87
}
88

            
89
sub match {
90
  my $self = shift;
91
  return undef unless Mojo::DOM::CSS->new(tree => $self->tree)->match(@_);
92
  return $self;
copy gitweblite soruce code
root authored on 2012-11-23
93
}
94

            
95
sub namespace {
96
  my $self = shift;
97

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
98
  return '' if (my $current = $self->tree)->[0] eq 'root';
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
99

            
100
  # Extract namespace prefix and search parents
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
101
  my $ns = $current->[1] =~ /^(.*?):/ ? "xmlns:$1" : undef;
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
102
  while ($current->[0] ne 'root') {
copy gitweblite soruce code
root authored on 2012-11-23
103

            
104
    # Namespace for prefix
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
105
    my $attrs = $current->[2];
106
    if ($ns) { /^\Q$ns\E$/ and return $attrs->{$_} for keys %$attrs }
copy gitweblite soruce code
root authored on 2012-11-23
107

            
108
    # Namespace attribute
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
109
    elsif (defined $attrs->{xmlns}) { return $attrs->{xmlns} }
copy gitweblite soruce code
root authored on 2012-11-23
110

            
111
    $current = $current->[3];
112
  }
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
113

            
114
  return '';
copy gitweblite soruce code
root authored on 2012-11-23
115
}
116

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
117
sub next { shift->_siblings->[1][0] }
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
118

            
copy gitweblite soruce code
root authored on 2012-11-23
119
sub parent {
120
  my $self = shift;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
121
  return undef if (my $tree = $self->tree)->[0] eq 'root';
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
122
  return $self->new->tree($tree->[3])->xml($self->xml);
copy gitweblite soruce code
root authored on 2012-11-23
123
}
124

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
125
sub parse { shift->_delegate(parse => shift) }
copy gitweblite soruce code
root authored on 2012-11-23
126

            
127
sub prepend { shift->_add(0, @_) }
128

            
129
sub prepend_content {
130
  my ($self, $new) = @_;
131
  my $tree = $self->tree;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
132
  splice @$tree, _offset($tree), 0, _link($self->_parse("$new"), $tree);
copy gitweblite soruce code
root authored on 2012-11-23
133
  return $self;
134
}
135

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
136
sub previous { shift->_siblings->[0][-1] }
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
137

            
138
sub remove { shift->replace('') }
139

            
copy gitweblite soruce code
root authored on 2012-11-23
140
sub replace {
141
  my ($self, $new) = @_;
142
  my $tree = $self->tree;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
143
  return $self->xml(undef)->parse($new) if $tree->[0] eq 'root';
144
  return $self->_replace($tree, $self->_parse("$new"));
copy gitweblite soruce code
root authored on 2012-11-23
145
}
146

            
147
sub replace_content {
148
  my ($self, $new) = @_;
149
  my $tree = $self->tree;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
150
  splice @$tree, _offset($tree), $#$tree, _link($self->_parse("$new"), $tree);
copy gitweblite soruce code
root authored on 2012-11-23
151
  return $self;
152
}
153

            
154
sub root {
155
  my $self = shift;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
156
  return $self unless my $tree = _ancestors($self->tree, 1);
157
  return $self->new->tree($tree)->xml($self->xml);
158
}
copy gitweblite soruce code
root authored on 2012-11-23
159

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
160
sub siblings { _select(Mojo::Collection->new(@{_siblings($_[0], 1)}), $_[1]) }
copy gitweblite soruce code
root authored on 2012-11-23
161

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
162
sub strip {
163
  my $self = shift;
164
  my $tree = $self->tree;
165
  return $self if $tree->[0] eq 'root';
166
  return $self->_replace($tree, ['root', _nodes($tree)]);
copy gitweblite soruce code
root authored on 2012-11-23
167
}
168

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
169
sub tap { shift->Mojo::Base::tap(@_) }
170

            
171
sub text { shift->_content(0, @_) }
copy gitweblite soruce code
root authored on 2012-11-23
172

            
173
sub text_after {
174
  my ($self, $trim) = @_;
175

            
176
  return '' if (my $tree = $self->tree)->[0] eq 'root';
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
177

            
178
  my (@nodes, $started);
179
  for my $n (_nodes($tree->[3])) {
180
    ++$started and next if $n eq $tree;
copy gitweblite soruce code
root authored on 2012-11-23
181
    next unless $started;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
182
    last if $n->[0] eq 'tag';
183
    push @nodes, $n;
copy gitweblite soruce code
root authored on 2012-11-23
184
  }
185

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
186
  return _text(\@nodes, 0, _trim($tree->[3], $trim));
copy gitweblite soruce code
root authored on 2012-11-23
187
}
188

            
189
sub text_before {
190
  my ($self, $trim) = @_;
191

            
192
  return '' if (my $tree = $self->tree)->[0] eq 'root';
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
193

            
194
  my @nodes;
195
  for my $n (_nodes($tree->[3])) {
196
    last if $n eq $tree;
197
    push @nodes, $n;
198
    @nodes = () if $n->[0] eq 'tag';
copy gitweblite soruce code
root authored on 2012-11-23
199
  }
200

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
201
  return _text(\@nodes, 0, _trim($tree->[3], $trim));
copy gitweblite soruce code
root authored on 2012-11-23
202
}
203

            
204
sub to_xml { shift->[0]->render }
205

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
206
sub tree { shift->_delegate(tree => @_) }
copy gitweblite soruce code
root authored on 2012-11-23
207

            
208
sub type {
209
  my ($self, $type) = @_;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
210
  return '' if (my $tree = $self->tree)->[0] eq 'root';
copy gitweblite soruce code
root authored on 2012-11-23
211
  return $tree->[1] unless $type;
212
  $tree->[1] = $type;
213
  return $self;
214
}
215

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
216
sub xml { shift->_delegate(xml => @_) }
copy gitweblite soruce code
root authored on 2012-11-23
217

            
218
sub _add {
219
  my ($self, $offset, $new) = @_;
220

            
221
  return $self if (my $tree = $self->tree)->[0] eq 'root';
222

            
223
  my $parent = $tree->[3];
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
224
  splice @$parent, _parent($parent, $tree) + $offset, 0,
225
    _link($self->_parse("$new"), $parent);
copy gitweblite soruce code
root authored on 2012-11-23
226

            
227
  return $self;
228
}
229

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
230
sub _ancestors {
231
  my ($tree, $root) = @_;
232
  my @ancestors;
233
  push @ancestors, $tree while ($tree->[0] eq 'tag') && ($tree = $tree->[3]);
234
  return $root ? $ancestors[-1] : @ancestors[0 .. $#ancestors - 1];
235
}
236

            
237
sub _collect {
238
  my $self = shift;
239
  my $xml  = $self->xml;
240
  return Mojo::Collection->new(@_)
241
    ->map(sub { $self->new->tree($_)->xml($xml) });
242
}
243

            
244
sub _content {
245
  my $tree = shift->tree;
246
  return _text([_nodes($tree)], shift, _trim($tree, @_));
copy gitweblite soruce code
root authored on 2012-11-23
247
}
248

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
249
sub _delegate {
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
250
  my ($self, $method) = (shift, shift);
251
  return $self->[0]->$method unless @_;
252
  $self->[0]->$method(@_);
253
  return $self;
254
}
255

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
256
sub _link {
copy gitweblite soruce code
root authored on 2012-11-23
257
  my ($children, $parent) = @_;
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
258

            
259
  # Link parent to children
copy gitweblite soruce code
root authored on 2012-11-23
260
  my @new;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
261
  for my $n (@$children[1 .. $#$children]) {
262
    push @new, $n;
263
    next unless $n->[0] eq 'tag';
264
    $n->[3] = $parent;
265
    weaken $n->[3];
266
  }
267

            
268
  return @new;
269
}
270

            
271
sub _nodes {
272
  return unless my $n = shift;
273
  return @$n[_offset($n) .. $#$n];
274
}
275

            
276
sub _offset { $_[0][0] eq 'root' ? 1 : 4 }
277

            
278
sub _parent {
279
  my ($parent, $child) = @_;
280

            
281
  # Find parent offset for child
282
  my $i = _offset($parent);
283
  for my $n (@$parent[$i .. $#$parent]) {
284
    last if $n == $child;
285
    $i++;
copy gitweblite soruce code
root authored on 2012-11-23
286
  }
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
287

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
288
  return $i;
copy gitweblite soruce code
root authored on 2012-11-23
289
}
290

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
291
sub _parse { Mojo::DOM::HTML->new(xml => shift->xml)->parse(shift)->tree }
copy gitweblite soruce code
root authored on 2012-11-23
292

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
293
sub _render { Mojo::DOM::HTML->new(tree => shift, xml => shift)->render }
294

            
295
sub _replace {
296
  my ($self, $tree, $new) = @_;
297
  my $parent = $tree->[3];
298
  splice @$parent, _parent($parent, $tree), 1, _link($new, $parent);
299
  return $self->parent;
300
}
301

            
302
sub _select {
303
  my ($self, $selector) = @_;
304
  return defined $selector ? $self->grep(sub { $_->match($selector) }) : $self;
305
}
306

            
307
sub _siblings {
308
  my ($self, $merge) = @_;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
309

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
310
  return $merge ? [] : [[], []] unless my $parent = $self->parent;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
311

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
312
  my $tree = $self->tree;
313
  my (@before, @after, $match);
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
314
  for my $child ($parent->children->each) {
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
315
    ++$match and next if $child->tree eq $tree;
316
    $match ? push @after, $child : push @before, $child;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
317
  }
318

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
319
  return $merge ? [@before, @after] : [\@before, \@after];
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
320
}
321

            
copy gitweblite soruce code
root authored on 2012-11-23
322
sub _text {
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
323
  my ($nodes, $recurse, $trim) = @_;
324

            
325
  # Merge successive text nodes
326
  my $i = 0;
327
  while (my $next = $nodes->[$i + 1]) {
328
    ++$i and next unless $nodes->[$i][0] eq 'text' && $next->[0] eq 'text';
329
    splice @$nodes, $i, 2, ['text', $nodes->[$i][1] . $next->[1]];
330
  }
copy gitweblite soruce code
root authored on 2012-11-23
331

            
332
  my $text = '';
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
333
  for my $n (@$nodes) {
334
    my $type = $n->[0];
copy gitweblite soruce code
root authored on 2012-11-23
335

            
336
    # Nested tag
337
    my $content = '';
338
    if ($type eq 'tag' && $recurse) {
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
339
      $content = _text([_nodes($n)], 1, _trim($n, $trim));
copy gitweblite soruce code
root authored on 2012-11-23
340
    }
341

            
342
    # Text
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
343
    elsif ($type eq 'text') { $content = $trim ? squish($n->[1]) : $n->[1] }
copy gitweblite soruce code
root authored on 2012-11-23
344

            
345
    # CDATA or raw text
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
346
    elsif ($type eq 'cdata' || $type eq 'raw') { $content = $n->[1] }
copy gitweblite soruce code
root authored on 2012-11-23
347

            
348
    # Add leading whitespace if punctuation allows it
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
349
    $content = " $content" if $text =~ /\S\z/ && $content =~ /^[^.!?,;:\s]+/;
copy gitweblite soruce code
root authored on 2012-11-23
350

            
351
    # Trim whitespace blocks
352
    $text .= $content if $content =~ /\S+/ || !$trim;
353
  }
354

            
355
  return $text;
356
}
357

            
358
sub _trim {
359
  my ($e, $trim) = @_;
360

            
361
  # Disabled
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
362
  return 0 unless $e && ($trim = defined $trim ? $trim : 1);
copy gitweblite soruce code
root authored on 2012-11-23
363

            
364
  # Detect "pre" tag
365
  while ($e->[0] eq 'tag') {
366
    return 0 if $e->[1] eq 'pre';
367
    last unless $e = $e->[3];
368
  }
369

            
370
  return 1;
371
}
372

            
373
1;
374

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
375
=encoding utf8
376

            
copy gitweblite soruce code
root authored on 2012-11-23
377
=head1 NAME
378

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
379
Mojo::DOM - Minimalistic HTML/XML DOM parser with CSS selectors
copy gitweblite soruce code
root authored on 2012-11-23
380

            
381
=head1 SYNOPSIS
382

            
383
  use Mojo::DOM;
384

            
385
  # Parse
386
  my $dom = Mojo::DOM->new('<div><p id="a">A</p><p id="b">B</p></div>');
387

            
388
  # Find
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
389
  say $dom->at('#b')->text;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
390
  say $dom->find('p')->text;
391
  say $dom->find('[id]')->attr('id');
copy gitweblite soruce code
root authored on 2012-11-23
392

            
393
  # Walk
394
  say $dom->div->p->[0]->text;
395
  say $dom->div->children('p')->first->{id};
396

            
397
  # Iterate
398
  $dom->find('p[id]')->each(sub { say shift->{id} });
399

            
400
  # Loop
401
  for my $e ($dom->find('p[id]')->each) {
402
    say $e->text;
403
  }
404

            
405
  # Modify
406
  $dom->div->p->[1]->append('<p id="c">C</p>');
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
407
  $dom->find(':not(p)')->strip;
copy gitweblite soruce code
root authored on 2012-11-23
408

            
409
  # Render
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
410
  say "$dom";
copy gitweblite soruce code
root authored on 2012-11-23
411

            
412
=head1 DESCRIPTION
413

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
414
L<Mojo::DOM> is a minimalistic and relaxed HTML/XML DOM parser with CSS
copy gitweblite soruce code
root authored on 2012-11-23
415
selector support. It will even try to interpret broken XML, so you should not
416
use it for validation.
417

            
418
=head1 CASE SENSITIVITY
419

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
420
L<Mojo::DOM> defaults to HTML semantics, that means all tags and attributes
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
421
are lowercased and selectors need to be lowercase as well.
copy gitweblite soruce code
root authored on 2012-11-23
422

            
423
  my $dom = Mojo::DOM->new('<P ID="greeting">Hi!</P>');
424
  say $dom->at('p')->text;
425
  say $dom->p->{id};
426

            
427
If XML processing instructions are found, the parser will automatically switch
428
into XML mode and everything becomes case sensitive.
429

            
430
  my $dom = Mojo::DOM->new('<?xml version="1.0"?><P ID="greeting">Hi!</P>');
431
  say $dom->at('P')->text;
432
  say $dom->P->{ID};
433

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
434
XML detection can also be disabled with the L</"xml"> method.
copy gitweblite soruce code
root authored on 2012-11-23
435

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
436
  # Force XML semantics
copy gitweblite soruce code
root authored on 2012-11-23
437
  $dom->xml(1);
438

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
439
  # Force HTML semantics
copy gitweblite soruce code
root authored on 2012-11-23
440
  $dom->xml(0);
441

            
442
=head1 METHODS
443

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
444
L<Mojo::DOM> implements the following methods.
copy gitweblite soruce code
root authored on 2012-11-23
445

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
446
=head2 new
copy gitweblite soruce code
root authored on 2012-11-23
447

            
448
  my $dom = Mojo::DOM->new;
449
  my $dom = Mojo::DOM->new('<foo bar="baz">test</foo>');
450

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
451
Construct a new array-based L<Mojo::DOM> object and L</"parse"> HTML/XML
452
fragment if necessary.
copy gitweblite soruce code
root authored on 2012-11-23
453

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
454
=head2 all_text
copy gitweblite soruce code
root authored on 2012-11-23
455

            
456
  my $trimmed   = $dom->all_text;
457
  my $untrimmed = $dom->all_text(0);
458

            
459
Extract all text content from DOM structure, smart whitespace trimming is
460
enabled by default.
461

            
462
  # "foo bar baz"
463
  $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->all_text;
464

            
465
  # "foo\nbarbaz\n"
466
  $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->all_text(0);
467

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
468
=head2 ancestors
469

            
470
  my $collection = $dom->ancestors;
471
  my $collection = $dom->ancestors('div');
472

            
473
Find all ancestors of this element matching the CSS selector and return a
474
L<Mojo::Collection> object containing these elements as L<Mojo::DOM> objects.
475
All selectors from L<Mojo::DOM::CSS> are supported.
476

            
477
  # List types of ancestor elements
478
  say $dom->ancestors->type;
479

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
480
=head2 append
copy gitweblite soruce code
root authored on 2012-11-23
481

            
482
  $dom = $dom->append('<p>Hi!</p>');
483

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
484
Append HTML/XML fragment to element.
copy gitweblite soruce code
root authored on 2012-11-23
485

            
486
  # "<div><h1>A</h1><h2>B</h2></div>"
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
487
  $dom->parse('<div><h1>A</h1></div>')->at('h1')->append('<h2>B</h2>')->root;
copy gitweblite soruce code
root authored on 2012-11-23
488

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
489
=head2 append_content
copy gitweblite soruce code
root authored on 2012-11-23
490

            
491
  $dom = $dom->append_content('<p>Hi!</p>');
492

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
493
Append HTML/XML fragment to element content.
copy gitweblite soruce code
root authored on 2012-11-23
494

            
495
  # "<div><h1>AB</h1></div>"
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
496
  $dom->parse('<div><h1>A</h1></div>')->at('h1')->append_content('B')->root;
copy gitweblite soruce code
root authored on 2012-11-23
497

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
498
=head2 at
copy gitweblite soruce code
root authored on 2012-11-23
499

            
500
  my $result = $dom->at('html title');
501

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
502
Find first element matching the CSS selector and return it as a L<Mojo::DOM>
503
object or return C<undef> if none could be found. All selectors from
504
L<Mojo::DOM::CSS> are supported.
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
505

            
506
  # Find first element with "svg" namespace definition
507
  my $namespace = $dom->at('[xmlns\:svg]')->{'xmlns:svg'};
copy gitweblite soruce code
root authored on 2012-11-23
508

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
509
=head2 attr
copy gitweblite soruce code
root authored on 2012-11-23
510

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
511
  my $attrs = $dom->attr;
512
  my $foo   = $dom->attr('foo');
513
  $dom      = $dom->attr({foo => 'bar'});
514
  $dom      = $dom->attr(foo => 'bar');
copy gitweblite soruce code
root authored on 2012-11-23
515

            
516
Element attributes.
517

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
518
  # List id attributes
519
  say $dom->find('*')->attr('id')->compact;
520

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
521
=head2 children
copy gitweblite soruce code
root authored on 2012-11-23
522

            
523
  my $collection = $dom->children;
524
  my $collection = $dom->children('div');
525

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
526
Find all children of this element matching the CSS selector and return a
527
L<Mojo::Collection> object containing these elements as L<Mojo::DOM> objects.
528
All selectors from L<Mojo::DOM::CSS> are supported.
copy gitweblite soruce code
root authored on 2012-11-23
529

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
530
  # Show type of random child element
531
  say $dom->children->shuffle->first->type;
532

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
533
=head2 content_xml
copy gitweblite soruce code
root authored on 2012-11-23
534

            
535
  my $xml = $dom->content_xml;
536

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
537
Render content of this element to XML.
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
538

            
539
  # "<b>test</b>"
540
  $dom->parse('<div><b>test</b></div>')->div->content_xml;
copy gitweblite soruce code
root authored on 2012-11-23
541

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
542
=head2 find
copy gitweblite soruce code
root authored on 2012-11-23
543

            
544
  my $collection = $dom->find('html title');
545

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
546
Find all elements matching the CSS selector and return a L<Mojo::Collection>
547
object containing these elements as L<Mojo::DOM> objects. All selectors from
548
L<Mojo::DOM::CSS> are supported.
copy gitweblite soruce code
root authored on 2012-11-23
549

            
550
  # Find a specific element and extract information
551
  my $id = $dom->find('div')->[23]{id};
552

            
553
  # Extract information from multiple elements
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
554
  my @headers = $dom->find('h1, h2, h3')->text->each;
555
  my @links   = $dom->find('a[href]')->attr('href')->each;
556

            
557
=head2 match
558

            
559
  my $result = $dom->match('html title');
560

            
561
Match the CSS selector against this element and return it as a L<Mojo::DOM>
562
object or return C<undef> if it didn't match. All selectors from
563
L<Mojo::DOM::CSS> are supported.
copy gitweblite soruce code
root authored on 2012-11-23
564

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
565
=head2 namespace
copy gitweblite soruce code
root authored on 2012-11-23
566

            
567
  my $namespace = $dom->namespace;
568

            
569
Find element namespace.
570

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
571
  # Find namespace for an element with namespace prefix
572
  my $namespace = $dom->at('svg > svg\:circle')->namespace;
573

            
574
  # Find namespace for an element that may or may not have a namespace prefix
575
  my $namespace = $dom->at('svg > circle')->namespace;
576

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
577
=head2 next
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
578

            
579
  my $sibling = $dom->next;
580

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
581
Return L<Mojo::DOM> object for next sibling of element or C<undef> if there
582
are no more siblings.
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
583

            
584
  # "<h2>B</h2>"
585
  $dom->parse('<div><h1>A</h1><h2>B</h2></div>')->at('h1')->next;
586

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
587
=head2 parent
copy gitweblite soruce code
root authored on 2012-11-23
588

            
589
  my $parent = $dom->parent;
590

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
591
Return L<Mojo::DOM> object for parent of element or C<undef> if this element
592
has no parent.
copy gitweblite soruce code
root authored on 2012-11-23
593

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
594
=head2 parse
copy gitweblite soruce code
root authored on 2012-11-23
595

            
596
  $dom = $dom->parse('<foo bar="baz">test</foo>');
597

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
598
Parse HTML/XML fragment with L<Mojo::DOM::HTML>.
copy gitweblite soruce code
root authored on 2012-11-23
599

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
600
  # Parse XML
601
  my $dom = Mojo::DOM->new->xml(1)->parse($xml);
copy gitweblite soruce code
root authored on 2012-11-23
602

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
603
=head2 prepend
copy gitweblite soruce code
root authored on 2012-11-23
604

            
605
  $dom = $dom->prepend('<p>Hi!</p>');
606

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
607
Prepend HTML/XML fragment to element.
copy gitweblite soruce code
root authored on 2012-11-23
608

            
609
  # "<div><h1>A</h1><h2>B</h2></div>"
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
610
  $dom->parse('<div><h2>B</h2></div>')->at('h2')->prepend('<h1>A</h1>')->root;
copy gitweblite soruce code
root authored on 2012-11-23
611

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
612
=head2 prepend_content
copy gitweblite soruce code
root authored on 2012-11-23
613

            
614
  $dom = $dom->prepend_content('<p>Hi!</p>');
615

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
616
Prepend HTML/XML fragment to element content.
copy gitweblite soruce code
root authored on 2012-11-23
617

            
618
  # "<div><h2>AB</h2></div>"
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
619
  $dom->parse('<div><h2>B</h2></div>')->at('h2')->prepend_content('A')->root;
620

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
621
=head2 previous
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
622

            
623
  my $sibling = $dom->previous;
624

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
625
Return L<Mojo::DOM> object for previous sibling of element or C<undef> if
626
there are no more siblings.
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
627

            
628
  # "<h1>A</h1>"
629
  $dom->parse('<div><h1>A</h1><h2>B</h2></div>')->at('h2')->previous;
630

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
631
=head2 remove
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
632

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
633
  my $parent = $dom->remove;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
634

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
635
Remove element and return L<Mojo::DOM> object for parent of element.
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
636

            
637
  # "<div></div>"
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
638
  $dom->parse('<div><h1>A</h1></div>')->at('h1')->remove;
copy gitweblite soruce code
root authored on 2012-11-23
639

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
640
=head2 replace
copy gitweblite soruce code
root authored on 2012-11-23
641

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
642
  my $parent = $dom->replace('<div>test</div>');
copy gitweblite soruce code
root authored on 2012-11-23
643

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
644
Replace element with HTML/XML fragment and return L<Mojo::DOM> object for
645
parent of element.
copy gitweblite soruce code
root authored on 2012-11-23
646

            
647
  # "<div><h2>B</h2></div>"
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
648
  $dom->parse('<div><h1>A</h1></div>')->at('h1')->replace('<h2>B</h2>');
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
649

            
650
  # "<div></div>"
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
651
  $dom->parse('<div><h1>A</h1></div>')->at('h1')->replace('');
copy gitweblite soruce code
root authored on 2012-11-23
652

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
653
=head2 replace_content
copy gitweblite soruce code
root authored on 2012-11-23
654

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
655
  $dom = $dom->replace_content('<p>test</p>');
copy gitweblite soruce code
root authored on 2012-11-23
656

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
657
Replace element content with HTML/XML fragment.
copy gitweblite soruce code
root authored on 2012-11-23
658

            
659
  # "<div><h1>B</h1></div>"
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
660
  $dom->parse('<div><h1>A</h1></div>')->at('h1')->replace_content('B')->root;
661

            
662
  # "<div><h1></h1></div>"
663
  $dom->parse('<div><h1>A</h1></div>')->at('h1')->replace_content('')->root;
copy gitweblite soruce code
root authored on 2012-11-23
664

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
665
=head2 root
copy gitweblite soruce code
root authored on 2012-11-23
666

            
667
  my $root = $dom->root;
668

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
669
Return L<Mojo::DOM> object for root node.
copy gitweblite soruce code
root authored on 2012-11-23
670

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
671
=head2 siblings
672

            
673
  my $collection = $dom->siblings;
674
  my $collection = $dom->siblings('div');
675

            
676
Find all siblings of this element matching the CSS selector and return a
677
L<Mojo::Collection> object containing these elements as L<Mojo::DOM> objects.
678
All selectors from L<Mojo::DOM::CSS> are supported.
679

            
680
  # List types of sibling elements
681
  say $dom->siblings->type;
682

            
683
=head2 strip
684

            
685
  my $parent = $dom->strip;
686

            
687
Remove element while preserving its content and return L<Mojo::DOM> object for
688
parent of element.
689

            
690
  # "<div>A</div>"
691
  $dom->parse('<div><h1>A</h1></div>')->at('h1')->strip;
692

            
693
=head2 tap
694

            
695
  $dom = $dom->tap(sub {...});
696

            
697
Alias for L<Mojo::Base/"tap">.
698

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
699
=head2 text
copy gitweblite soruce code
root authored on 2012-11-23
700

            
701
  my $trimmed   = $dom->text;
702
  my $untrimmed = $dom->text(0);
703

            
704
Extract text content from element only (not including child elements), smart
705
whitespace trimming is enabled by default.
706

            
707
  # "foo baz"
708
  $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->text;
709

            
710
  # "foo\nbaz\n"
711
  $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->text(0);
712

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
713
=head2 text_after
copy gitweblite soruce code
root authored on 2012-11-23
714

            
715
  my $trimmed   = $dom->text_after;
716
  my $untrimmed = $dom->text_after(0);
717

            
718
Extract text content immediately following element, smart whitespace trimming
719
is enabled by default.
720

            
721
  # "baz"
722
  $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->p->text_after;
723

            
724
  # "baz\n"
725
  $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->p->text_after(0);
726

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
727
=head2 text_before
copy gitweblite soruce code
root authored on 2012-11-23
728

            
729
  my $trimmed   = $dom->text_before;
730
  my $untrimmed = $dom->text_before(0);
731

            
732
Extract text content immediately preceding element, smart whitespace trimming
733
is enabled by default.
734

            
735
  # "foo"
736
  $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->p->text_before;
737

            
738
  # "foo\n"
739
  $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->p->text_before(0);
740

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
741
=head2 to_xml
copy gitweblite soruce code
root authored on 2012-11-23
742

            
743
  my $xml = $dom->to_xml;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
744
  my $xml = "$dom";
745

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
746
Render this element and its content to XML.
copy gitweblite soruce code
root authored on 2012-11-23
747

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
748
  # "<b>test</b>"
749
  $dom->parse('<div><b>test</b></div>')->div->b->to_xml;
copy gitweblite soruce code
root authored on 2012-11-23
750

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
751
=head2 tree
copy gitweblite soruce code
root authored on 2012-11-23
752

            
753
  my $tree = $dom->tree;
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
754
  $dom     = $dom->tree(['root', ['text', 'foo']]);
copy gitweblite soruce code
root authored on 2012-11-23
755

            
update Mojolicious 4.07
Yuki Kimoto authored on 2013-06-03
756
Document Object Model. Note that this structure should only be used very
757
carefully since it is very dynamic.
copy gitweblite soruce code
root authored on 2012-11-23
758

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
759
=head2 type
copy gitweblite soruce code
root authored on 2012-11-23
760

            
761
  my $type = $dom->type;
762
  $dom     = $dom->type('div');
763

            
764
Element type.
765

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
766
  # List types of child elements
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
767
  say $dom->children->type;
copy gitweblite soruce code
root authored on 2012-11-23
768

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
769
=head2 xml
copy gitweblite soruce code
root authored on 2012-11-23
770

            
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
771
  my $bool = $dom->xml;
772
  $dom     = $dom->xml($bool);
copy gitweblite soruce code
root authored on 2012-11-23
773

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
774
Disable HTML semantics in parser and activate case sensitivity, defaults to
775
auto detection based on processing instructions.
copy gitweblite soruce code
root authored on 2012-11-23
776

            
777
=head1 CHILD ELEMENTS
778

            
779
In addition to the methods above, many child elements are also automatically
780
available as object methods, which return a L<Mojo::DOM> or
781
L<Mojo::Collection> object, depending on number of children.
782

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
783
  say $dom->p->text;
copy gitweblite soruce code
root authored on 2012-11-23
784
  say $dom->div->[23]->text;
update Mojolicious to 4.57
Yuki Kimoto authored on 2013-12-02
785
  say $dom->div->text;
copy gitweblite soruce code
root authored on 2012-11-23
786

            
787
=head1 ELEMENT ATTRIBUTES
788

            
789
Direct hash reference access to element attributes is also possible.
790

            
791
  say $dom->{foo};
792
  say $dom->div->{id};
793

            
794
=head1 SEE ALSO
795

            
796
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
797

            
798
=cut