swc_html_parser/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![deny(unused_must_use)]
3#![deny(clippy::all)]
4#![allow(clippy::needless_return)]
5#![allow(clippy::nonminimal_bool)]
6#![allow(clippy::wrong_self_convention)]
7#![allow(clippy::match_like_matches_macro)]
8
9use swc_common::{input::StringInput, SourceFile};
10use swc_html_ast::{Document, DocumentFragment, DocumentMode, Element};
11
12use crate::{
13    error::Error,
14    lexer::Lexer,
15    parser::{PResult, Parser, ParserConfig},
16};
17
18#[macro_use]
19mod macros;
20pub mod error;
21pub mod lexer;
22pub mod parser;
23
24/// Parse a given file as `Document`.
25///
26/// If there are syntax errors but if it was recoverable, it will be appended to
27/// `errors`.
28pub fn parse_file_as_document(
29    fm: &SourceFile,
30    config: ParserConfig,
31    errors: &mut Vec<Error>,
32) -> PResult<Document> {
33    let lexer = Lexer::new(StringInput::from(fm));
34    let mut parser = Parser::new(lexer, config);
35    let result = parser.parse_document();
36
37    errors.extend(parser.take_errors());
38
39    result
40}
41
42/// Parse a given file as `DocumentFragment`.
43///
44/// If there are syntax errors but if it was recoverable, it will be appended to
45/// `errors`.
46pub fn parse_file_as_document_fragment(
47    fm: &SourceFile,
48    context_element: &Element,
49    mode: DocumentMode,
50    form_element: Option<&Element>,
51    config: ParserConfig,
52    errors: &mut Vec<Error>,
53) -> PResult<DocumentFragment> {
54    let lexer = Lexer::new(StringInput::from(fm));
55    let mut parser = Parser::new(lexer, config);
56    let result =
57        parser.parse_document_fragment(context_element.clone(), mode, form_element.cloned());
58
59    errors.extend(parser.take_errors());
60
61    result
62}