lol_html/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
//! ***LOL HTML*** is a **L**ow **O**utput **L**atency streaming **HTML** rewriter/parser with
//! CSS-selector based API.
//!
//! It is designed to modify HTML on the fly with minimal buffering. It can quickly handle very large
//! documents, and operate in environments with limited memory resources.
//!
//! The crate serves as a back-end for the HTML rewriting functionality of [Cloudflare Workers], but
//! can be used as a standalone library with the convenient API for a wide variety of HTML
//! rewriting/analysis tasks.
//!
//! The crate provides two main API entry points:
//!
//! * [`HtmlRewriter`] - a streaming HTML rewriter;
//! * [`rewrite_str`] - one-off HTML string rewriting function.
//!
//! [Cloudflare Workers]: https://www.cloudflare.com/en-gb/products/cloudflare-workers/
//! [`HtmlRewriter`]: struct.HtmlRewriter.html
//! [`rewrite_str`]: fn.rewrite_str.html
#![cfg_attr(not(any(feature = "integration_test", test)), warn(missing_docs))]
#[macro_use]
mod base;
#[macro_use]
mod html;
#[macro_use]
mod rewriter;
mod memory;
mod parser;
mod rewritable_units;
mod transform_stream;
use cfg_if::cfg_if;
pub use self::rewriter::{
rewrite_str, AsciiCompatibleEncoding, CommentHandler, DoctypeHandler, DocumentContentHandlers,
ElementContentHandlers, ElementHandler, EndHandler, EndTagHandler, HandlerResult, HandlerTypes,
HtmlRewriter, LocalHandlerTypes, MemorySettings, RewriteStrSettings, Settings, TextHandler,
};
pub use self::selectors_vm::Selector;
pub use self::transform_stream::OutputSink;
/// These module contains types to work with [`Send`]able [`HtmlRewriter`](crate::HtmlRewriter)s.
pub mod send {
use crate::rewriter::{
CommentHandlerSend, DoctypeHandlerSend, ElementHandlerSend, EndHandlerSend,
EndTagHandlerSend, SendHandlerTypes, TextHandlerSend,
};
/// An [`HtmlRewriter`](crate::HtmlRewriter) that implements [`Send`].
pub type HtmlRewriter<'h, O> = crate::HtmlRewriter<'h, O, SendHandlerTypes>;
/// [`Settings`](crate::Settings) for [`Send`]able [`HtmlRewriter`](crate::HtmlRewriter)s.
pub type Settings<'h, 's> = crate::Settings<'h, 's, SendHandlerTypes>;
/// [`RewriteStrSettings`](crate::RewriteStrSettings) for [`Send`]able [`HtmlRewriter`](crate::HtmlRewriter)s.
pub type RewriteStrSettings<'h, 's> = crate::RewriteStrSettings<'h, 's, SendHandlerTypes>;
/// [`ElementContentHandlers`](crate::ElementContentHandlers) for [`Send`]able [`HtmlRewriter`](crate::HtmlRewriter)s.
pub type ElementContentHandlers<'h> = crate::ElementContentHandlers<'h, SendHandlerTypes>;
/// [`DocumentContentHandlers`](crate::DocumentContentHandlers) for [`Send`]able [`HtmlRewriter`](crate::HtmlRewriter)s.
pub type DocumentContentHandlers<'h> = crate::DocumentContentHandlers<'h, SendHandlerTypes>;
/// [`CommentHandler`](crate::CommentHandler) for [`Send`]able [`HtmlRewriter`](crate::HtmlRewriter)s.
pub type CommentHandler<'h> = CommentHandlerSend<'h>;
/// [`DoctypeHandler`](crate::DoctypeHandler) for [`Send`]able [`HtmlRewriter`](crate::HtmlRewriter)s.
pub type DoctypeHandler<'h> = DoctypeHandlerSend<'h>;
/// [`ElementHandler`](crate::ElementHandler) for [`Send`]able [`HtmlRewriter`](crate::HtmlRewriter)s.
pub type ElementHandler<'h> = ElementHandlerSend<'h>;
/// [`EndHandler`](crate::EndHandler) for [`Send`]able [`HtmlRewriter`](crate::HtmlRewriter)s.
pub type EndHandler<'h> = EndHandlerSend<'h>;
/// [`EndTagHandler`](crate::EndTagHandler) for [`Send`]able [`HtmlRewriter`](crate::HtmlRewriter)s.
pub type EndTagHandler<'h> = EndTagHandlerSend<'h>;
/// [`TextHandler`](crate::TextHandler) for [`Send`]able [`HtmlRewriter`](crate::HtmlRewriter)s.
pub type TextHandler<'h> = TextHandlerSend<'h>;
/// [`Element`](crate::Element) for [`Send`]able [`HtmlRewriter`](crate::HtmlRewriter)s.
pub type Element<'r, 't> = crate::rewritable_units::Element<'r, 't, SendHandlerTypes>;
}
/// The errors that can be produced by the crate's API.
pub mod errors {
pub use super::memory::MemoryLimitExceededError;
pub use super::parser::ParsingAmbiguityError;
pub use super::rewritable_units::{AttributeNameError, CommentTextError, TagNameError};
pub use super::rewriter::RewritingError;
pub use super::selectors_vm::SelectorError;
}
/// HTML content descriptors that can be produced and modified by a rewriter.
pub mod html_content {
pub use super::rewritable_units::{
Attribute, Comment, ContentType, Doctype, DocumentEnd, Element, EndTag, StartTag,
TextChunk, UserData,
};
pub use super::html::TextType;
}
#[cfg(any(test, feature = "integration_test"))]
pub mod test_utils {
use encoding_rs::*;
pub static ASCII_COMPATIBLE_ENCODINGS: [&Encoding; 36] = [
BIG5,
EUC_JP,
EUC_KR,
GB18030,
GBK,
IBM866,
ISO_8859_2,
ISO_8859_3,
ISO_8859_4,
ISO_8859_5,
ISO_8859_6,
ISO_8859_7,
ISO_8859_8,
ISO_8859_8_I,
ISO_8859_10,
ISO_8859_13,
ISO_8859_14,
ISO_8859_15,
ISO_8859_16,
KOI8_R,
KOI8_U,
MACINTOSH,
SHIFT_JIS,
UTF_8,
WINDOWS_874,
WINDOWS_1250,
WINDOWS_1251,
WINDOWS_1252,
WINDOWS_1253,
WINDOWS_1254,
WINDOWS_1255,
WINDOWS_1256,
WINDOWS_1257,
WINDOWS_1258,
X_MAC_CYRILLIC,
X_USER_DEFINED,
];
pub static NON_ASCII_COMPATIBLE_ENCODINGS: [&encoding_rs::Encoding; 4] =
[UTF_16BE, UTF_16LE, ISO_2022_JP, REPLACEMENT];
pub struct Output {
bytes: Vec<u8>,
encoding: &'static Encoding,
finalizing_chunk_received: bool,
}
impl Output {
pub fn new(encoding: &'static Encoding) -> Self {
Output {
bytes: Vec::default(),
encoding,
finalizing_chunk_received: false,
}
}
pub fn push(&mut self, chunk: &[u8]) {
if chunk.is_empty() {
self.finalizing_chunk_received = true;
} else {
assert!(
!self.finalizing_chunk_received,
"Chunk written to the output after the finalizing chunk."
);
self.bytes.extend_from_slice(chunk);
}
}
}
impl From<Output> for String {
fn from(output: Output) -> String {
assert!(
output.finalizing_chunk_received,
"Finalizing chunk for the output hasn't been received."
);
output
.encoding
.decode_without_bom_handling(&output.bytes)
.0
.into_owned()
}
}
}
cfg_if! {
if #[cfg(feature = "integration_test")] {
pub mod selectors_vm;
pub use self::base::SharedEncoding;
pub use self::transform_stream::{
StartTagHandlingResult, TransformController, TransformStream,
TransformStreamSettings
};
pub use self::rewritable_units::{
EndTag, Serialize, StartTag, Token, TokenCaptureFlags, Mutations
};
pub use self::memory::SharedMemoryLimiter;
pub use self::html::{LocalName, LocalNameHash, Tag, Namespace};
} else {
mod selectors_vm;
}
}