philss/floki

Possible wrong typespec for `attribute/2`

mtarnovan opened this issue · 3 comments

The typespec of attribute/2 is @spec attribute(binary | html_tree, binary) :: list, but the function can also be called with type html_node as first arg:

iex(10)> Floki.attribute([{"a", [{"href", "https://google.com"}], ["Google"]}], "a", "href")
["https://google.com"]
iex(11)> Floki.attribute({"a", [{"href", "https://google.com"}], ["Google"]}, "a", "href")  
["https://google.com"]

Is the correct typespec actually @spec attribute(binary | html_tree | html_node, binary) :: list or am I missing something? traverse_and_update for example, actually passes in a html_node, not a html_tree to the reducer, so doing this:

    Floki.traverse_and_update(doc, fn
      {"a", _attrs, _children} = el ->
        Floki.attribute(el, "src")

would actually trigger an error from dialyxir.

edit: this may apply to other funs as well

@mtarnovan you are correct! 👍 Although we are going to remove the support for binaries, this function accepts a html_node(). Another thing to mention is that Floki.find/2 also accepts a html_node(), so I think we can fix that too. Do you mind to open a PR? Thanks!

Sure, I can open a PR. Just fix the typespecs or did you have something more in mind?

@mtarnovan Just the typespecs for now. Thank you! 💜