-
Notifications
You must be signed in to change notification settings - Fork 12
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
Node create, append, before, and after #12
Conversation
Thanks. I think need to rename methods to append_child, insert_before, insert_after (this is clearer names). also move this methods to about create, feels strange to create tree each time when node created. need to think more about it. is it really make sense to create independent trees? may be we can do something like: tree = Myhtml::Tree.new
div = tree.create_node(:div)
tree.insert_in_document(div)
a = tree.create(:a)
div.insert_after(a) i think i refactor and split Parser and Tree classes. |
i split parser and tree in master: 55ed2ba |
I went back and forth on how to name these. It's weird because OO doesn't mix well with English. English uses Subject verb object. OO uses Object.verb(subject). So "Alice comes before Bob" is how it's said, but in code that's `bob.before(alice)`. I also considered `bob.follow(alice)` but that seemed too clever. Nokogiri uses before and after also.
Thanks for the comments and the quick refactor! I'll rename the insert methods and move |
It works the same but you need to create a tree first, then a node in that tree. The node can then be inserted into a document.
c09eee4
to
29d1776
Compare
Methods renamed/moved! I didn't add |
spec/node_spec.cr
Outdated
it "adds a node just prior to this node" do | ||
document = Myhtml::Parser.new("<html><body><main></main></body></html>") | ||
main = document.css("main").first | ||
tree = Myhtml::Tree.new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use document.tree
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Fixed.
seems all ok, i add some changes after, anything you want to add here? docs? |
Yes, I'll add docs and submit those before you merge. Thanks for all your help! |
Docs added. |
released v1.2.0 with this changes, example usage: https://github.com/kostya/myhtml/blob/master/examples/create_html.cr |
Fantastic! 🎉 Thank you for such a quick turn around and a great library. |
This adds for new methods to Node:
create
,append
,before
, andafter
. They enable documents to be modified to in addition to being parsed and searched.I feel good about the
append
,before
, andafter
methods, but I feel likecreate
could be better. I'd love some feedback on that 🙂 One thing I tried was exposing the@raw_html
instance variable from Parser, but that seemed like a strange thing to make public just so a different class could use it. As it stands, it feels like I'm too much in one method for a single class. Maybe the answer is just to abstract all the setup of creating and initializing a structure and tree into a different method so it doesn't look as cluttered. Ideally there's some other tree that's created already that I can use.Also, I didn't create any documentation. When you think this looks okay, I'll happily add documentation to the methods before it's merged.
Thank you!