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 ('encode'); |
18 |
$self->result->layer_uncertain ('charset'); |
19 |
|
20 |
my $out = $self->output; |
21 |
$out->start_section (role => 'parse-errors'); |
22 |
$out->start_error_list (role => 'parse-errors'); |
23 |
$self->result->layer_applicable ('syntax'); |
24 |
|
25 |
my $input = $self->input; |
26 |
my $result = $self->result; |
27 |
|
28 |
my $onerror = sub { |
29 |
$result->add_error (@_, layer => 'syntax'); |
30 |
}; |
31 |
|
32 |
my $dom = Message::DOM::DOMImplementation->new; |
33 |
my $doc = $dom->create_document; |
34 |
my $el; |
35 |
my $inner_html_element = $input->{inner_html_element}; |
36 |
if (defined $inner_html_element and length $inner_html_element) { |
37 |
$input->{charset} ||= 'windows-1252'; ## TODO: for now. |
38 |
my $t = \($input->{s}); |
39 |
unless ($input->{is_char_string}) { |
40 |
$t = \(Encode::decode ($input->{charset}, $$t)); |
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 |
} |
55 |
|
56 |
$self->{structure} = $doc; |
57 |
} |
58 |
$doc->manakai_charset ($input->{official_charset}) |
59 |
if defined $input->{official_charset}; |
60 |
|
61 |
$doc->document_uri ($input->{uri}); |
62 |
$doc->manakai_entity_base_uri ($input->{base_uri}); |
63 |
|
64 |
$out->end_error_list (role => 'parse-errors'); |
65 |
$out->end_section; |
66 |
} # generate_syntax_error_section |
67 |
|
68 |
sub source_charset ($) { |
69 |
my $self = shift; |
70 |
return $self->input->{charset} || ($self->{structure}->owner_document || $self->{structure})->input_encoding; |
71 |
## TODO: Can we always use input_encoding? |
72 |
} # source_charset |
73 |
|
74 |
1; |