CSS-DOM

 view release on metacpan or  search on metacpan

lib/CSS/DOM/Style.pm  view on Meta::CPAN

package CSS::DOM::Style;

$VERSION = '0.17';

use warnings; no warnings qw' utf8';
use strict;

use CSS::DOM::Exception 'SYNTAX_ERR';
use CSS::DOM::Util qw 'escape_ident unescape';
use Scalar::Util 'weaken';

# ~~~ use overload fallback => 1, '@{}' => 

# Internal object structure
#
# Each style object is a hash ref:
# {
#    owner       => $owner_rule,
#    parser      => $property_parser,
#    mod_handler => sub { ... },  # undef initially
#    names       => [...],
#    props       => {...},
#    pri         => {...},  # property priorities
# }
#
# The value of an element in the props hash can be one of three things
#  1) a CSSValue object
#  2) an array ref that is a blueprint for a CSSValue object:
#     [ $css_code, $class, @constructor_args]
#  3) a string of css code
# Item (3) is only used when there is no property parser.

sub parse {
	require CSS::DOM::Parser;
	goto &CSS::DOM::Parser::parse_style_declaration;
}

sub new {
	my($class) = shift;

	my $self = bless {}, $class;
	if(@_ == 1) {
		$self->{owner} = shift;
	}
	else {
		my %args = @_;
		$self->{owner} = delete $args{owner};
		$self->{parser}
		 = delete $args{property_parser};
	}
	{
		$self->{parser} ||= (
		    ($self->{owner} || next)->parentStyleSheet || next
		   )->property_parser;
	}
	weaken $self->{owner};
	return $self
}

sub cssText {
	my $self = shift;
	my $out;
	if (defined wantarray) {
		$out = join "; ", map {
			my $pri = $self->getPropertyPriority($_);
			"$_: ".$self->getPropertyValue($_)." !"x!!$pri
				. escape_ident($pri)
		} @{$$self{names}};
	}
	if(@_) {
		my $css = shift;
		!defined $css || !length $css and
			@$self{'props','names'} = (), return $out;

		require CSS::DOM::Parser;
		my $new =CSS::DOM::Parser::parse_style_declaration(
		 $css, property_parser => $$self{parser}
		);

		@$self{'props','names'} = @$new{'props','names'};
		_m($self);
	}
	return $out;
}

sub getPropertyValue { # ~~~ Later I plan to make this return lists of
                       #     scalars in list context (for list properties).
	my $self = shift;
	my $props = $self->{props} || return '';
	my $name = lc$_[0];

	if(my $spec = $self->{parser}) { serialise: {
		if(my $p = $spec->get_property($name)) {
		 if(exists $p->{serialise} and my $s = $p->{serialise}) {
		   my @p = $spec->subproperty_names($name);
		   my %p;
		   for(@p) {
		    my $v = $self->getPropertyValue($_) ;
		    length $v or last serialise;
		    $p{$_}
		     = $spec->get_property($_)->{default} eq $v ?'':$v;
		   }
		   return $s->(\%p);
		 }
		}
	}}

	exists $props->{$name}
		or return return '';
	my $val = $props->{$name};
	return ref $val eq 'ARRAY' ? $$val[0]
	     : !ref $val           ? $val
	     :                       $val->cssText;
}

sub getPropertyCSSValue {



( run in 0.476 second using v1.01-cache-2.11-cpan-e1769b4cff6 )