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

proposal: x/net/html: add PrependChild and InsertAfter to Node #46960

Closed
olavfosse opened this issue Jun 29, 2021 · 7 comments
Closed

proposal: x/net/html: add PrependChild and InsertAfter to Node #46960

olavfosse opened this issue Jun 29, 2021 · 7 comments

Comments

@olavfosse
Copy link

I have already made a PR, but @neild said I should create a proposal.

The following is the commit message of the commit in my PR:

html: add PrependChild and InsertAfter

These methods complement the already existing AppendChild and
InsertBefore methods in allowing for easy and safe manipulation of
Node trees.

For example code like

oldChild.Parent.InsertBefore(newChild, oldChild.NextSibling)

can now be written like

oldChild.Parent.InsertAfter(newChild, oldChild)

and code like

node.InsertBefore(newNode, node.FirstChild)

can now be written like

node.PrependChild(newNode)
@gopherbot gopherbot added this to the Proposal milestone Jun 29, 2021
@seankhliao seankhliao changed the title Proposal: add PrependChild and InsertAfter methods to golang.org/x/net/html.Node proposal: x/net/html: add PrependChild and InsertAfter to Node Jun 29, 2021
@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Jun 29, 2021
@ianlancetaylor
Copy link
Contributor

Can you give a short example showing when a program would want to use these new methods? Thanks.

@olavfosse
Copy link
Author

Sure. I am writing a static site generator which renders pages by combining a layout HTML file with HTML files containing the content of each page. The repetitive parts of the pages reside in the layout file and the unique content are in the content files. The content files start with a front-matter containing metadata about the page, such as its title and description.

For example if we have the content page

<!--{
"title": "The title of the page",
"description": "Page description"
}-->

<p>here is a paragraph</p>

<h2>And a subheading</h2>
<p>And another paragraph</p>

the program would first insert all elements after the front-matter into the layout. It would then insert a h1 and p based on the metadata:

// Insert the page title heading
pageTitle := &html.Node{
Type: html.ElementNode,
Data: "h1",
DataAtom: atom.H1,
}
pageTitle.AppendChild(&html.Node{
Type: html.TextNode,
Data: titleFieldInFrontMatter,
})
main.PrependChild(pageTitle)

// Insert the page description paragraph
pageDescription := &html.Node{
Type: html.ElementNode,
Data: "p",
DataAtom: atom.P,
}
pageDescription.AppendChild(&html.Node{
Type: html.TextNode,
Data: descriptionFieldInFrontMatter,
})
pageTitle.Parent.InsertAfter(pageDescription, pageTitle)

Sorry, this did not turn out short after all :D

@rsc
Copy link
Contributor

rsc commented Oct 27, 2021

/cc @nigeltao

@rsc rsc moved this from Incoming to Active in Proposals (old) Oct 27, 2021
@rsc
Copy link
Contributor

rsc commented Oct 27, 2021

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented Nov 3, 2021

The methods that are already there - AppendChild and InsertBefore - are a complete set in the sense that they provide all possible functionality. Do we really need to add a second complete set?

Also, if your program finds that it needs these frequently, it is easy to write them as local helper functions, either using AppendChild and InsertBefore or using direct manipulation of the Nodes.

@rsc
Copy link
Contributor

rsc commented Nov 10, 2021

Based on the discussion above, this proposal seems like a likely decline.
— rsc for the proposal review group

@rsc rsc moved this from Active to Likely Decline in Proposals (old) Nov 10, 2021
@rsc rsc moved this from Likely Decline to Declined in Proposals (old) Dec 1, 2021
@rsc
Copy link
Contributor

rsc commented Dec 1, 2021

No change in consensus, so declined.
— rsc for the proposal review group

@rsc rsc closed this as completed Dec 1, 2021
@golang golang locked and limited conversation to collaborators Dec 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests

4 participants