| 1 |
package WebHACC::Language::HTML; |
| 2 |
use strict; |
| 3 |
require WebHACC::Language::DOM; |
| 4 |
push our @ISA, 'WebHACC::Language::DOM'; |
| 5 |
|
| 6 |
sub new ($) { |
| 7 |
return bless {}, shift; |
| 8 |
} # new |
| 9 |
|
| 10 |
sub generate_syntax_error_section ($) { |
| 11 |
my $self = shift; |
| 12 |
|
| 13 |
require Message::DOM::DOMImplementation; |
| 14 |
require Encode; |
| 15 |
require Whatpm::HTML; |
| 16 |
|
| 17 |
$self->result->layer_uncertain ('charset'); |
| 18 |
|
| 19 |
my $out = $self->output; |
| 20 |
$out->start_section (role => 'parse-errors'); |
| 21 |
$out->start_error_list (role => 'parse-errors'); |
| 22 |
$self->result->layer_applicable ('syntax'); |
| 23 |
|
| 24 |
my $input = $self->input; |
| 25 |
my $result = $self->result; |
| 26 |
|
| 27 |
my $onerror = sub { |
| 28 |
$result->add_error (@_, layer => 'syntax'); |
| 29 |
}; |
| 30 |
|
| 31 |
my $dom = Message::DOM::DOMImplementation->new; |
| 32 |
my $doc = $dom->create_document; |
| 33 |
my $el; |
| 34 |
my $inner_html_element = $input->{inner_html_element}; |
| 35 |
if (defined $inner_html_element and length $inner_html_element) { |
| 36 |
$input->{charset} ||= 'windows-1252'; ## TODO: for now. |
| 37 |
my $t = \($input->{s}); |
| 38 |
unless ($input->{is_char_string}) { |
| 39 |
$t = \(Encode::decode ($input->{charset}, $$t)); |
| 40 |
$self->result->layer_uncertain ('encode'); |
| 41 |
} |
| 42 |
|
| 43 |
$el = $doc->create_element_ns |
| 44 |
('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]); |
| 45 |
Whatpm::HTML->set_inner_html ($el, $$t, $onerror); |
| 46 |
|
| 47 |
$self->{structure} = $el; |
| 48 |
} else { |
| 49 |
if ($input->{is_char_string}) { |
| 50 |
Whatpm::HTML->parse_char_string ($input->{s} => $doc, $onerror); |
| 51 |
} else { |
| 52 |
Whatpm::HTML->parse_byte_string |
| 53 |
($input->{charset}, $input->{s} => $doc, $onerror); |
| 54 |
$self->result->layer_uncertain ('encode'); |
| 55 |
} |
| 56 |
|
| 57 |
$self->{structure} = $doc; |
| 58 |
} |
| 59 |
$doc->manakai_charset ($input->{official_charset}) |
| 60 |
if defined $input->{official_charset}; |
| 61 |
|
| 62 |
$doc->document_uri ($input->{uri}); |
| 63 |
$doc->manakai_entity_base_uri ($input->{base_uri}); |
| 64 |
|
| 65 |
$out->end_error_list (role => 'parse-errors'); |
| 66 |
$out->end_section; |
| 67 |
} # generate_syntax_error_section |
| 68 |
|
| 69 |
sub source_charset ($) { |
| 70 |
my $self = shift; |
| 71 |
return $self->input->{charset} || ($self->{structure}->owner_document || $self->{structure})->input_encoding; |
| 72 |
## TODO: Can we always use input_encoding? |
| 73 |
} # source_charset |
| 74 |
|
| 75 |
1; |