Giter Site home page Giter Site logo

Inject HTML into a node about gokogiri HOT 9 CLOSED

moovweb avatar moovweb commented on August 18, 2024
Inject HTML into a node

from gokogiri.

Comments (9)

zhigangc avatar zhigangc commented on August 18, 2024

Does the node erase everything it has, like its current tag name, and all its children nodes?

I'd need more detailed description and examples.

from gokogiri.

HamptonMakes avatar HamptonMakes commented on August 18, 2024
"<div>mom<br/></div>"
divNode.Inject("Hey <hr/>")
"<div>mom<br/>Hey <hr/></div>"

Replaces the inner() contents and returns the Slice of injected nodes. So, the return would have two Nodes in it... one TextNode ("Hey ") and one Element ("hr").

And its current tag name is untouched. The element itself is untouched.

from gokogiri.

zhigangc avatar zhigangc commented on August 18, 2024

Then, in your opening comment, should "node.String()" be really "node.Content". I thought String() would output the node itself and its children, wouldn't it?

So this is the same as SetContent, except we are dealing with HTML here?

from gokogiri.

HamptonMakes avatar HamptonMakes commented on August 18, 2024

Yes, exactly. And you are totally correct. My mistake.

On Nov 16, 2011, at 10:43 AM, Zhigang Chen wrote:

Then, in your opening comment, should "node.String()" be really "node.Content". I thought String() would output the node itself and its children, wouldn't it?

So this is the same as SetContent, except we are dealing with HTML here?


Reply to this email directly or view it on GitHub:
#1 (comment)

from gokogiri.

zhigangc avatar zhigangc commented on August 18, 2024

Add SetHtmlContent for Element. A testcase is also added.

from gokogiri.

HamptonMakes avatar HamptonMakes commented on August 18, 2024

Implementing it this way makes it impossible to get the
newly inserted collection, since all of the other siblings are
destroyed in the process.

See my example above where it injects the new nodes into
the bottom of the document.

On Nov 16, 2011, at 11:18 AM, Zhigang Chen wrote:

Add SetHtmlContent for Element. A testcase is also added.


Reply to this email directly or view it on GitHub:
#1 (comment)

from gokogiri.

zhigangc avatar zhigangc commented on August 18, 2024

Then "Replaces the inner() contents" is at odds with the example.

from gokogiri.

HamptonMakes avatar HamptonMakes commented on August 18, 2024

Blah. What is wrong with me?
I was working on two bugs at once and my brain was getting fried.

This should inject some new nodes (inputted as an HTML string) to the bottom of
the node. So, its like AppendContent(), but its not escaped. And, it returns the
newly added nodes (NodeSet? Slice?).

doc := libxml.XmlParseString("<root><parent><child /></parent></root>")
root := doc.RootElement()
parent := root.First()
Equal(t, parent.Size(), 1)
insertions := parent.Inject(" and <sibling/>")
Equal(t, len(insertions), 2)
Equal(t, parent.Size(), 3)
Equal(t, parent.First().Name(), "child")
Equal(t, parent.First().Next().Content(), " and ") // not a functional line (yet)
Equal(t, parent.Last().Name(), "sibling")
Equal(t, insertions[1], parent.Last())  // go node comparability isnt working, but this should be kind-of-true

from gokogiri.

zhigangc avatar zhigangc commented on August 18, 2024

You can use AppendHtmlContent

check the commit:

https://github.com/moovweb/gokogiri/commit/5417e413ad193d8b376feeb64d2050b34492ae63

from gokogiri.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.