Skip to content
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

Inject HTML into a node #1

Closed
HamptonMakes opened this issue Nov 16, 2011 · 9 comments
Closed

Inject HTML into a node #1

HamptonMakes opened this issue Nov 16, 2011 · 9 comments
Assignees

Comments

@HamptonMakes
Copy link
Contributor

There should be a way to inject HTML into a node. For instance,

node.String() // ""
node.Inject("<div />")
node.String() // "<div />"

And, furthermore, this new div has to be properly doc'd.

node.FirstElement().Doc() == node.Doc()
// and ensure this happens in C-world too!
@ghost ghost assigned zhigangc Nov 16, 2011
@zhigangc
Copy link
Contributor

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.

@HamptonMakes
Copy link
Contributor Author

"<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.

@zhigangc
Copy link
Contributor

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?

@HamptonMakes
Copy link
Contributor Author

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)

@zhigangc
Copy link
Contributor

Add SetHtmlContent for Element. A testcase is also added.

@HamptonMakes
Copy link
Contributor Author

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)

@zhigangc zhigangc reopened this Nov 16, 2011
@zhigangc
Copy link
Contributor

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

@HamptonMakes
Copy link
Contributor Author

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

zhigangc pushed a commit that referenced this issue Nov 17, 2011
@zhigangc
Copy link
Contributor

You can use AppendHtmlContent

check the commit:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants