Mojo-DOM-Role-Restrict

 view release on metacpan or  search on metacpan

lib/Mojo/DOM/Role/Restrict.pm  view on Meta::CPAN

	# Processing instruction
	return '<?' . $tree->[1] . '?>' if $type eq 'pi';

	# Everything else
	return '';
}

sub _valid_tag {
	my ($spec, $tag, $attrs) = @_;
	my $valid = $spec->{$tag} // $spec->{'*'};
	return ref $valid && $valid->{validate_tag} 
		? $valid->{validate_tag}($tag, $attrs)
		: $valid
			? ($tag, $attrs)
			: 0;
}

sub _valid_attribute {
	my ($spec, $tag, $attr, $value) = @_;
	my $valid = $spec->{$tag}->{$attr} // $spec->{$tag}->{'*'} // $spec->{'*'}->{$attr} // $spec->{'*'}->{'*'};
	return ref $valid 
		? $valid->($attr, $value) 

lib/Mojo/DOM/Role/Restrict.pm  view on Meta::CPAN

			'*' => 1, # allow all attributes
			onclick => sub { 0 }, # disable onclick attributes
			id => sub { return @_ }, # enable id attributes
			class => sub { # allow only 1 class 'okay'
				my ($attr, $val) = @_;
				my $match = $val =~ m/^okay$/;
				return $match ? ($attr, $val) : 0;
			}
		},
		span => {
			validate_tag => sub { # replace span tags with b tags
				return ('b', $_[1]);
			}
		},
		p => {
			validate_tag => sub {
				$_[1]->{id} = "prefixed-" . $_[1]->{id}; # prefix all p tag IDs
				$_[1]->{'data-unknown'} = 'abc';  # extend all p tags with a data-unknown attribute
				return @_;
			}
		},
	};
	
	$dom->parse($html, $spec);
	
	# <html><head></head><body><p class="okay" data-unknown="abc" id="prefixed-allow">Restrict <b>HTML</b></p></body></html>

lib/Mojo/DOM/Role/Restrict.pm  view on Meta::CPAN

			'*' => 1, # allow all attributes
			onclick => sub { 0 }, # disable onclick attributes
			id => sub { return @_ }, # enable id attributes
			class => sub { # allow only 1 class 'okay'
				my ($attr, $val) = @_;
				my $match = $val =~ m/^okay$/;
				return $match ? ($attr, $val) : 0;
			}
		},
		span => {
			validate_tag => sub { # replace span tags with b tags
				return ('b', $_[1]);
			}
		},
		p => {
			validate_tag => sub {
				$_[1]->{id} = "prefixed-" . $_[1]->{id}; # prefix all p tag IDs
				$_[1]->{'data-unknown'} = 'abc';  # extend all p tags with a data-unknown attribute
				return @_;
			}
		},
	};
	
	$dom->parse($html, $spec);

	# render without spec validation

t/03-callback.t  view on Meta::CPAN

use Test::More;
use Mojo::DOM;

my $dom = Mojo::DOM->with_roles('+Restrict')->new;

basic_test(
	html => q|<html><head><script>...</script></head><body><p class="okay" id="allow" onclick="not-allow">Restrict <span class="not-okay">HTML</span></p></body></html>|,
	spec => {
		script => 0,
		span => {
			validate_tag => sub {
				delete $_[1]->{class};
				return @_;
			}
		},
		'*' => {
			class => sub {
				my ($attr, $val) = @_;
				my $match = $val =~ m/^okay$/;
				return $match ? ($attr, $val) : 0;
			},

t/03-callback.t  view on Meta::CPAN

		},
	},
	expected => q|<html><head></head><body><p class="okay" id="allow">Restrict <span>HTML</span></p></body></html>|
);

basic_test(
	html => q|<html><head><script>...</script></head><body><p class="okay" id="allow" onclick="not-allow">Restrict <span class="not-okay">HTML</span></p></body></html>|,
	spec => {
		script => 0,
		span => {
			validate_tag => sub {
				delete $_[1]->{class};
				return @_;
			}
		},
		'*' => {
			class => 1,
			id => sub {
				return @_;
			}
		},

t/03-callback.t  view on Meta::CPAN

		},
	},
	expected => q|<html><head></head><body><p class="okay" id="allow">Restrict <span>HTML</span></p></body></html>|
);

basic_test(
	html => q|<html><head><script>...</script></head><body><p class="okay" id="allow" onclick="not-allow">Restrict <span class="not-okay">HTML</span></p><p class="okay" id="allow" onclick="not-allow">Restrict <span class="not-okay">HTML</span></p><p cla...
	spec => {
		script => 0,
		span => {
			validate_tag => sub {
				return ('b', $_[1]);
			}
		},
		p => {
			validate_tag => sub {
				$_[1]->{id} = "prefixed-" . $_[1]->{id};
				$_[1]->{'data-unknown'} = 'abc'; 
				return ('div', $_[1]);
			}
		},
		'*' => {
			'*' => 1,
			onclick => sub { 0 },
			class => sub {
				my ($attr, $val) = @_;

t/07-restrict-callback.t  view on Meta::CPAN

use Test::More;
use Mojo::DOM;

my $dom = Mojo::DOM->with_roles('+Restrict')->new;

basic_test(
	html => q|<html><head><script>...</script></head><body><p class="okay" id="allow" onclick="not-allow">Restrict <span class="not-okay">HTML</span></p></body></html>|,
	spec => {
		script => 0,
		span => {
			validate_tag => sub {
				delete $_[1]->{class};
				return @_;
			}
		},
		'*' => {
			class => sub {
				my ($attr, $val) = @_;
				my $match = $val =~ m/^okay$/;
				return $match ? ($attr, $val) : 0;
			},

t/07-restrict-callback.t  view on Meta::CPAN

		},
	},
	expected => q|<html><head></head><body><p class="okay" id="allow">Restrict <span>HTML</span></p></body></html>|
);

basic_test(
	html => q|<html><head><script>...</script></head><body><p class="okay" id="allow" onclick="not-allow">Restrict <span class="not-okay">HTML</span></p></body></html>|,
	spec => {
		script => 0,
		span => {
			validate_tag => sub {
				delete $_[1]->{class};
				return @_;
			}
		},
		'*' => {
			class => 1,
			id => sub {
				return @_;
			}
		},

t/07-restrict-callback.t  view on Meta::CPAN

		},
	},
	expected => q|<html><head></head><body><p class="okay" id="allow">Restrict <span>HTML</span></p></body></html>|
);

basic_test(
	html => q|<html><head><script>...</script></head><body><p class="okay" id="allow" onclick="not-allow">Restrict <span class="not-okay">HTML</span></p><p class="okay" id="allow" onclick="not-allow">Restrict <span class="not-okay">HTML</span></p><p cla...
	spec => {
		script => 0,
		span => {
			validate_tag => sub {
				return ('b', $_[1]);
			}
		},
		p => {
			validate_tag => sub {
				$_[1]->{id} = "prefixed-" . $_[1]->{id};
				$_[1]->{'data-unknown'} = 'abc'; 
				return ('div', $_[1]);
			}
		},
		'*' => {
			'*' => 1,
			onclick => sub { 0 },
			class => sub {
				my ($attr, $val) = @_;

t/synopsis.t  view on Meta::CPAN

		'*' => 1,
		onclick => sub { 0 },
		id => sub { return @_ },
		class => sub {
			my ($attr, $val) = @_;
			my $match = $val =~ m/^okay$/;
			return $match ? ($attr, $val) : 0;
		}
	},
	span => {
		validate_tag => sub {
			return ('b', $_[1]);
		}
	},
	p => {
		validate_tag => sub {
			$_[1]->{id} = "prefixed-" . $_[1]->{id};
			$_[1]->{'data-unknown'} = 'abc'; 
			return @_;
		}
	},
};

$dom->parse($html, $spec);
	
is($dom->to_string, q|<html><head></head><body><p class="okay" data-unknown="abc" id="prefixed-allow">Restrict <b>HTML</b></p></body></html>|);



( run in 2.661 seconds using v1.01-cache-2.11-cpan-d06a3f9ecfd )