-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting text contents of a node? #8
Comments
This isn't possible, because of the nature of gumbo iteself, all of the node data you're having exposed to you is entirely managed internally by gumbo. If you mess with it at all, you're going to bug out or even crash your program, because you're tampering with the managed memory of another object. This is the very clear contract that gumbo provides to you, that if you want to own things, you need to copy them. |
I'd assume then a suitable option is to perform replacements on the original string given to CDocument and re-parse it? (I suppose that's not so bad) |
I made heavy modifications to gumbo-query just to be able to perform the simplest modifications of nodes at a good speed. These modifications included providing a Get() method to expose the underlying gumbo_node of CDocument/CNode. I then wrote several helper functions, the most important one is generating a unique node ID string. Like so:
Using this unique node ID, I could then keep a map of nodes I wanted to manipulate by storing them in a simple So yeah, not too bad, but there is a lot involved to doing these modifications. For me, this approach was necessary because I'm doing modifications to HTML in real-time as users browse, so speed was of the utmost importance. |
In my case speed isn't an issue. I'm making some POST requests, analysing the response and then making subsequent POST requests. I haven't tried it yet, but I'm assuming my simple idea of using the Replace function on my System::Strings should work (that particular replace function is quite fast), as I probably need to replace a couple of tags each containing a few thousand or so characters after each POST. It's not optimal but it might be okay. Thanks for clarifying that for me though.
|
Look at the code behind the text() methods and such in gumbo-query. They are just convenience functions that copy data from the parsed html, which resides exclusively in and owned by GumboOutput. So if you change the text that you get back from node.text(), this will have absolutely no effect on the actual document that you parsed. gumo-parser and gumbo-query only provide to you a read-only access to traverse parsed html. Maybe I'm not understand your use case, maybe the only text you need you're getting copied to you when you call text() on your node. But I want to make it clear that if you're expecting to get a HTML response, replace the text() of one element and end up with the whole response including your modifications, this simply isn't possible out of the box. |
I'm thinking more along the lines of having a global variable string. Everytime I make a new request that responds with pieces of HTML, I merge them into the global string by replacing the current CNode.text() with the HTML piece, and pass this global string to CDocument to be analysed again before making further requests. |
I think this feature can be implemented by CNode:startPos and CNode:endPos |
I'm assuming this wasn't intended, but would it be possible to create a way to set the text contents of a CNode? I'm in a situation where I need to update parts of a DOM on the fly, and I need such a feature.
If it were to be implemented, I'd imagine overloading .text() for a CNode to accept an std::string would work well and be similar to the JQuery function .html().
The text was updated successfully, but these errors were encountered: