Comments (2)
@evanderkoogh I would like an end
method added to ElementHandler
as well. But in the meantime, instead of saving and rewriting the entire head, you can use removeAndKeepContent
to strip the start and close tags and write them back in yourself as needed. This trick only works if you can detect the start of the next element, but this seems to work pretty well for the <head>
. You can also add content to <body>
in the same way, but you have to handle the <html>
element as well, and use a document end handler to rewrite the body closing tag:
let headContent = '';
let bodyContent = '';
const stripEndTag = (el) => {
const attrs = [...el.attributes].map(([k, v]) => `${k}="${v}"`);
const tag = attrs.length > 0
? `<${el.tagName} ${attrs.join(' ')}>`
: `<${el.tagName}>`;
el.prepend(tag, { html: true });
el.removeAndKeepContent();
};
const resp = new HTMLRewriter()
.on('html', {
element: (html) => {
stripEndTag(html);
}
})
.on('head', {
element: (head) => {
stripEndTag(head);
}
})
.on('head > link', {
element: () => {
headContent = '<!-- has some links -->';
}
})
.on('body', {
element: (body) => {
body.before(`${headContent}</head>`, { html: true });
stripEndTag(body);
}
})
.on('body *', {
element: () => {
bodyContent = "<!-- extra content -->";
}
})
.onDocument({
end: (doc) => {
doc.append(`${bodyContent}</body></html>`, { html: true });
}
});
from lol-html.
I ran into similar issue in a slightly different setup. What I'm trying to do is to "ignore" all content outside of CSS selectors. That can in theory be accomplished by communicating between HtmlRewriter
and OutputSink
. The entire setup isn't even that ugly. However the current solution involving on_end_tag
runs my callback before the end tag is written (#108) which means it's not possible to implement this correctly. The flipping of the flag happens before the sink is instructed to write the end tag so I'm generally losing out on it.
I believe there is a need for two end tag handlers: one for where you still have the chance to modify the tag, and one where you already exited it. The latter is needed for properly maintaining a stack of elements of interest if that is needed.
from lol-html.
Related Issues (20)
- Detecting void elements HOT 3
- build fails for cssparser HOT 2
- Make `Element.on_end_tag` handler `'h` instead of `'static`
- Fails to rewrite simple html image tag HOT 4
- Confusion around `el.can_have_content()` HOT 1
- [Feature Request] Support XML parsing HOT 1
- release 1.2.0 not tagged on git
- bring back lockfiles for bindings HOT 1
- [Question/Request] Guarantees around the bytes emitted into the `OutputSink` HOT 2
- Provide way to get document position information for start and end tags HOT 1
- Evaluate using Profile-Guided Optimization (PGO) and Post-Link Optimization (PLO)
- 1st
- rewriter.end() self is non reference HOT 2
- Support for `Element::add_attribute` HOT 2
- Elements within `<noscript>` cannot be rewritten
- Replace strips what comes after the tag HOT 2
- Looser syntax for RSS feeds?
- How to extract inner HTML of an element? HOT 2
- HTMLRewriter’s element.setAttribute() method alters the surrounding single quote character
- replace()-ing or remove()-ing self-closing tags breaks outputted markup HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lol-html.