1 |
wakaba |
1.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 |
wakaba |
1.4 |
require Message::DOM::DOMImplementation; |
14 |
wakaba |
1.1 |
require Encode; |
15 |
|
|
require Whatpm::HTML; |
16 |
|
|
|
17 |
wakaba |
1.5 |
$self->result->layer_uncertain ('charset'); |
18 |
|
|
|
19 |
wakaba |
1.1 |
my $out = $self->output; |
20 |
wakaba |
1.3 |
$out->start_section (role => 'parse-errors'); |
21 |
|
|
$out->start_error_list (role => 'parse-errors'); |
22 |
wakaba |
1.5 |
$self->result->layer_applicable ('syntax'); |
23 |
wakaba |
1.1 |
|
24 |
|
|
my $input = $self->input; |
25 |
|
|
my $result = $self->result; |
26 |
|
|
|
27 |
|
|
my $onerror = sub { |
28 |
wakaba |
1.2 |
$result->add_error (@_, layer => 'syntax'); |
29 |
wakaba |
1.1 |
}; |
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 |
wakaba |
1.6 |
$self->result->layer_uncertain ('encode'); |
41 |
wakaba |
1.1 |
} |
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 |
wakaba |
1.6 |
$self->result->layer_uncertain ('encode'); |
55 |
wakaba |
1.1 |
} |
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 |
wakaba |
1.3 |
$out->end_error_list (role => 'parse-errors'); |
66 |
wakaba |
1.1 |
$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; |