File size: 3,901 Bytes
bee6636 |
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 |
use oxc::{allocator::Allocator, span::Span};
use transform::{
TransformResult, Transformer,
transform::{Transform, TransformLL},
transforms,
};
use crate::RewriterError;
#[derive(PartialEq, Eq)]
enum HtmlRewriteType<'alloc: 'data, 'data> {
AddScramAttr { key: &'data str, val: &'data str },
ReplaceAttr { new: &'alloc str },
ReplaceText { new: &'alloc str },
InsertText { text: &'alloc str },
RemoveText,
}
#[derive(PartialEq, Eq)]
pub struct HtmlRewrite<'alloc: 'data, 'data> {
span: Span,
ty: HtmlRewriteType<'alloc, 'data>,
}
impl<'alloc: 'data, 'data> HtmlRewrite<'alloc, 'data> {
pub fn add_scram_attr(after: Span, key: &'data str, val: &'data str) -> Self {
Self {
span: Span::new(after.end, after.end),
ty: HtmlRewriteType::AddScramAttr { key, val },
}
}
pub fn replace_attr(val: Span, new: &'alloc str) -> Self {
Self {
span: val,
ty: HtmlRewriteType::ReplaceAttr { new },
}
}
pub fn remove_attr(data: &'data str, key: Span, value: Span) -> Self {
let end = if Self::attr_is_quoted(data, value) {
value.end + 1
} else {
value.end
};
Self {
span: Span::new(key.start, end),
ty: HtmlRewriteType::RemoveText,
}
}
pub fn replace(node: Span, text: &'alloc str) -> Self {
Self {
span: node,
ty: HtmlRewriteType::ReplaceText { new: text },
}
}
pub fn insert_text(after: Span, text: &'alloc str) -> Self {
Self {
span: Span::new(after.end, after.end),
ty: HtmlRewriteType::InsertText { text },
}
}
pub fn remove_node(node: Span) -> Self {
Self {
span: node,
ty: HtmlRewriteType::RemoveText,
}
}
fn attr_is_quoted(data: &str, attr: Span) -> bool {
data.get((attr.start - 1) as usize..attr.start as usize)
.is_some_and(|x| x == "\'" || x == "\"")
}
}
impl<'alloc: 'data, 'data> Transform<'data> for HtmlRewrite<'alloc, 'data> {
type ToLowLevelData = &'data str;
fn span(&self) -> Span {
self.span
}
fn into_low_level(self, data: &Self::ToLowLevelData, _offset: i32) -> TransformLL<'data> {
let data = *data;
match self.ty {
HtmlRewriteType::AddScramAttr { key, val } => {
TransformLL::insert(transforms![" scramjet-attr-", key, "=\"", val, "\""])
}
HtmlRewriteType::ReplaceAttr { new } => {
TransformLL::replace(if Self::attr_is_quoted(data, self.span) {
transforms![new]
} else {
transforms!["\"", new, "\""]
})
}
HtmlRewriteType::ReplaceText { new } => TransformLL::replace(transforms![new]),
HtmlRewriteType::InsertText { text } => TransformLL::insert(transforms![text]),
HtmlRewriteType::RemoveText => TransformLL::replace(transforms![]),
}
}
}
impl PartialOrd for HtmlRewrite<'_, '_> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for HtmlRewrite<'_, '_> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.span.start.cmp(&other.span.start)
}
}
pub(crate) struct HtmlChanges<'alloc: 'data, 'data> {
inner: Transformer<'alloc, 'data, HtmlRewrite<'alloc, 'data>>,
}
impl<'alloc: 'data, 'data> HtmlChanges<'alloc, 'data> {
#[inline]
pub fn new() -> Self {
Self {
inner: Transformer::new(),
}
}
#[inline]
pub fn add(&mut self, rewrite: HtmlRewrite<'alloc, 'data>) {
self.inner.add(std::iter::once(rewrite));
}
#[inline]
pub fn set_alloc(&mut self, alloc: &'alloc Allocator) -> Result<(), RewriterError> {
Ok(self.inner.set_alloc(alloc)?)
}
#[inline]
pub fn take_alloc(&mut self) -> Result<(), RewriterError> {
Ok(self.inner.take_alloc()?)
}
#[inline]
pub fn empty(&self) -> bool {
self.inner.empty()
}
#[inline]
pub fn perform(&mut self, html: &'data str) -> Result<TransformResult<'alloc>, RewriterError> {
Ok(self.inner.perform(html, &html, false)?)
}
}
|