FunctionalPerl

 view release on metacpan or  search on metacpan

lib/PXML/Element.pm  view on Meta::CPAN

    @_ == 1 or fp_croak_arity 1;
    my ($fn) = @_;
    bless [$$s[NAME], $$s[ATTRIBUTES], &$fn($$s[BODY])], ref $s
}

# mapping

sub body_map {
    my $s = shift;
    @_ == 1 or fp_croak_arity 1;
    my ($fn) = @_;
    $s->body_update(sub { stream_map $fn, stream_mixed_flatten $_[0] })
}

# "body text", a string, dropping tags; not having knowledge about
# which XML tags have 'relevant body text', this returns all of it.

# XX ugly: this is replicating part of the serializer. But don't want
# to touch the code there... so, here goes. Really, better languages
# have been created to write code in.

sub _text {
    my ($v) = @_;
    if (defined $v) {
        if (length ref $v) {
            if (defined blessed $v) {
                if ($v->isa("PXML::Element")) {
                    $v->text
                } elsif ($v->isa("FP::Abstract::Sequence")) {
                    join("", map { _text($_) } $v->values)
                } elsif (is_promise $v) {
                    _text(force $v);
                } else {
                    die "don't know how to get text of: $v";
                }
            } else {
                if (UNIVERSAL::isa($v, "ARRAY")) {
                    join("", map { _text($_) } @$v);
                } elsif (UNIVERSAL::isa($v, "CODE")) {

                    # correct? XX why does A(string_to_stream("You're
                    # great."))->text trigger this case?
                    _text(&$v());
                } else {
                    die "don't know how to get text of: $v";
                }
            }
        } else {
            $v
        }
    } else {
        ""
    }
}

sub text {
    my $s = shift;
    _text($s->body)
}

# only for debugging? Doesn't emit XML/XHTML prologues!  Also, ugly
# monkey-access to PXML::Serialize. Circular dependency, too.

sub string {
    my $s = shift;
    require PXML::Serialize;
    capture_stdout {
        PXML::Serialize::pxml_print_fragment_fast($s, *STDOUT);
    }
}

# XML does not distinguish between void elements and non-void ones in
# its syntactical representation; whether an element is printed in
# self-closing representation is orthogonal and can rely simply on
# whether the content of the particular element ('at runtime') is
# empty.

sub require_printing_nonvoid_elements_nonselfreferential {
    0
}

#sub void_element_h {
#    undef
#}

_END_



( run in 2.035 seconds using v1.01-cache-2.11-cpan-2398b32b56e )