Template-Lace

 view release on metacpan or  search on metacpan

t/dom.t  view on Meta::CPAN

        +{ value=>'tx', content=>'Texas' },
      ]
    });

  is $dom->find('option')->size, 2;
}

# optgroup with ids

{
  # nested ol
  ok my $dom = Template::Lace::DOM->new(q[
    <section>
      <ul id='outer'>
        <li>
          <ul class='inner'>
            <li>
              Hello 
              <span class='name'>NAME</span>
              , you are 
              <span data-lace-id='age'>AGE</span>
            </li>
          </ul>
      </ul>
    </section>
  ]);

  $dom->for('#outer', [
      'bbbb',
      +{
        'inner' => [
          sub { shift->content('stuff') },
          +{ name=>'john', age=>42 },
        ],
      },
      sub {
        my $dom = shift;
        $dom->content('aaa');
      },
    ]
  );
  
  is $dom->find('#outer li')->[0]->content, 'bbbb';
  is $dom->find('.inner li')->[0]->content, 'stuff';
  is $dom->find('.inner li')->[1]->at('.name')->[0]->content, 'john';
  is $dom->find('.inner li')->[1]->find('span')->[1]->content, 42;
  is $dom->find('#outer li')->[4]->content, 'aaa';
}

# General Helpers
{
  # clone
  {
    ok my $dom = Template::Lace::DOM->new('<h1>Hello</h1>');
    ok my $clone = $dom->clone;
    is "$dom", '<h1>Hello</h1>';
    is "$clone", '<h1>Hello</h1>';
    isnt refaddr($dom), refaddr($clone);
  }
  
  #overlay
  {
    my $dom = Template::Lace::DOM->new(qq[
      <h1 id="title">HW</h1>
      <section id="body">Hello World</section>
      </html>
    ]);

    $dom->overlay(sub {
      my ($dom, $now) = @_; # $dom is also localized to $_
      my $new_dom = Template::Lace::DOM->new(qq[
        <html>
          <head>
            <title>PAGE_TITLE</title>
          </head>
          <body>
            STUFF
          </body>
        </html>
      ]);

      $new_dom->title($dom->at('#title')->content)
        ->body($dom->at('#body')->content)
        ->at('head')
        ->append_content("<meta startup='$now'>");

      return $new_dom
    }, scalar(localtime));

    ok $dom->at('html');
    ok $dom->at('meta');
    is $dom->at('title')->content, 'HW';
    is $dom->at('body')->content, 'Hello World';
  }

  #repeat
  {
    my $dom = Template::Lace::DOM->new("<ul><li>ITEMS</li></ul>");
    my @items = (qw/aaa bbb ccc/);

    $dom->at('li')
      ->repeat(sub {
          my ($li, $item, $index) = @_;
          $li->content($item);
          return $li;
      }, @items);

    is $dom->find('li')->[0]->content, 'aaa';
    is $dom->find('li')->[1]->content, 'bbb';
    is $dom->find('li')->[2]->content, 'ccc';
    is @{$dom->find('li')}, 3;     
  }

  #fill
  {
    my $dom = Template::Lace::DOM->new(q[
      <section>
        <ul id='stuff'>
          <li></li>
        </ul>
        <ul id='stuff2'>
          <li>
            <a class='link'>Links</a> and Info: 
            <span class='info'></span>
          </li>
        </ul>

        <ol id='ordered'>
          <li></li>



( run in 0.469 second using v1.01-cache-2.11-cpan-ceb78f64989 )