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

find_by_selector should return more easily navigable / parseable data structure #71

Open
getaaron opened this issue Jun 22, 2016 · 4 comments

Comments

@getaaron
Copy link
Contributor

getaaron commented Jun 22, 2016

This code:

parser = CssParser::Parser.new
parser.load_string! 'a { color: hotpink; font-size: 13px; }'
parser.find_by_selector 'a'

Returns an array of one string:

["color: hotpink; font-size: 13px;"]

Is there a more easily navigable / parseable data structure we could return? For example, maybe something like:

[{:color=>["hotpink"], :font_size=>["13px"]}]
@grosser
Copy link
Contributor

grosser commented Jun 22, 2016

sounds like an useful feature, I think the internal structure should exist ... you could either add a raw: true option or as_hash: true ... but a simpler workaround could be to do

Hash[s.split(';').map {|k| k.split(': ', 2) }]

@getaaron
Copy link
Contributor Author

That doesn't work exactly right on shorthand properties like border:

["color: hotpink; font-size: 13px;border: 5px solid red"]

Also there are some spacing issues (we get the key " font-size" with a leading space), and also the split wouldn't work on variants like color : hotpink or color:hotpink

@grosser
Copy link
Contributor

grosser commented Jun 24, 2016

I think it produces normalized css, so spaces should not be an issue, shorthand won't work though

@tres
Copy link
Contributor

tres commented Jul 19, 2016

#72

this pull request adds a to_h instance method that will allow traversal of CSS as nested Hash objects.

don't know if that's going to get you where you need to go @getaaron , but it solves an issue for me & saw this so I thought I'd drop by & let you know about it.

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

3 participants