Skip to content

Commit

Permalink
Be more careful translating ReLaxNG, with * standing for any NON-name…
Browse files Browse the repository at this point in the history
…spaced name and *:* standing for any namespaced name
  • Loading branch information
brucemiller committed Jul 23, 2023
1 parent 00f0d32 commit 032a7db
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 95 deletions.
19 changes: 12 additions & 7 deletions lib/LaTeXML/Common/Model.pm
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ sub setSchemaClass {
# NOTE: These are public, but perhaps should be passed
# to submodel, in case it can evolve to more precision?
# However, it would need more context to do that.
# NOTE: That * matches any NON-namespaced name; *:* matches any namespaced name

# Can an element with (qualified name) $tag contain a $childtag element?
sub canContain {
Expand Down Expand Up @@ -396,13 +397,15 @@ sub canContain {
: ($$model{"!$childtag"} ? 0
: ($$model{"$chns:*"} ? 1
: ($$model{"!$chns:*"} ? 0
: ($$model{'*:*'} ? 1
: 0))))); }
: ($$model{'!*:*'} ? 0
: ($$model{'*:*'} ? 1
: 0)))))); }
else {
return ($$model{$childtag} ? 1
: ($$model{"!$childtag"} ? 0
: ($$model{'*:*'} ? 1
: 0))); } }
: ($$model{'!*'} ? 0
: ($$model{'*'} ? 1
: 0)))); } }

# NOTE: Currently the Document class already allows ANY namespaced attributes!
# (which is very unmodular, although it does have to arrange for namespace declarations)
Expand Down Expand Up @@ -432,13 +435,15 @@ sub canHaveAttribute {
: ($$attr{"!$attrib"} ? 0
: ($$attr{"$attrns:*"} ? 1
: ($$attr{"!$attrns:*"} ? 0
: ($$attr{'*:*'} ? 1
: ($$attr{'!*:*'} ? 0
: ($$attr{'!*:*'} ? 0
: ($$attr{'*:*'} ? 1
: 0)))))); }
else {
return ($$attr{$attrib} ? 1
: ($$attr{"!$attrib"} ? 0
: 0)); } }
: ($$attr{'!*'} ? 0
: ($$attr{'*'} ? 1
: 0)))); } }

sub isInSchemaClass {
my ($self, $classname, $tag) = @_;
Expand Down
21 changes: 16 additions & 5 deletions lib/LaTeXML/Common/Model/RelaxNG.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ sub loadSchema {
next; }
my @body = @{ $$self{elements}{$tag} };
my ($content, $attributes) = $self->extractContent($tag, @body);
$$self{model}->addTagContent($tag, sort keys %$content);
$$self{model}->addTagAttribute($tag, sort keys %$attributes); }
$$self{model}->addTagContent($tag, filterNames($content));
$$self{model}->addTagAttribute($tag, filterNames($attributes)); }
# Extract definitions of symbols that define Schema Classes, too
foreach my $symbol (sort keys %{ $$self{defs} }) {
if ($symbol =~ /^grammar\d+:(.+?)\.class$/) {
Expand All @@ -96,6 +96,17 @@ sub loadSchema {
ProgressSpindown("Loading RelaxNG $$self{name}");
return; }

# collapse redundancies like having both *:* and !*:*
sub filterNames {
my ($hash) = @_;
my %filtered = ();
foreach my $name (keys %$hash) {
if (($name ne '!*') # !* can be omitted; only cancels *
&& ((($name =~ /^!(.*)$/) && !(defined $$hash{$1})) # Negated name, but name not present?
|| (!defined $$hash{ '!' . $name }))) { # Or negation of name not present
$filtered{$name} = 1; } }
return sort keys %filtered; }

# Return two hashrefs for content & attributes
sub extractContent {
my ($self, $tag, @body) = @_;
Expand Down Expand Up @@ -343,12 +354,12 @@ sub scanNameClass {
my @exceptions = (); # Check for exceptions!
if (my @children = getElements($node)) {
@exceptions = map { $self->scanNameClass($_, $forattr, $ns) } @children; }
return ('*:*', @exceptions); }
return ('*', '*:*', @exceptions); } # anyName can be namespaced or not
elsif ($relaxop eq 'rng:nsName') {
my @exceptions = (); # Check for exceptions!
my @exceptions = (); # Check for exceptions!
if (my @children = getElements($node)) {
@exceptions = map { $self->scanNameClass($_, $forattr, $ns) } @children; }
return ($$self{model}->encodeQName($node->getAttribute('ns') || $ns, '*'), @exceptions); }
return ($$self{model}->encodeQName($node->getAttribute('ns') // $ns, '*'), @exceptions); }
elsif ($relaxop eq 'rng:choice') {
my %names = ();
foreach my $choice ($node->childNodes) {
Expand Down
Loading

0 comments on commit 032a7db

Please sign in to comment.