Skip to content

Commit

Permalink
Merge pull request #4 from flavorjones/3-head-includes-body
Browse files Browse the repository at this point in the history
Modify pluck behavior when seeing a `head` tag to include the `body`
  • Loading branch information
flavorjones committed Apr 26, 2024
2 parents d345d9d + 04ba3eb commit 60ff491
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/nokogiri/html5/inference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ module PluckRegexp # :nodoc:
TBODY = /\A\s*<(#{PluckTags::TBODY.join("|")})\b/i
TBODY_TR = /\A\s*<(#{PluckTags::TBODY_TR.join("|")})\b/i
COLGROUP = /\A\s*<(#{PluckTags::COLGROUP.join("|")})\b/i
HEAD_OUTER = /\A\s*<(head)\b/i
HTML_INNER = /\A\s*<(head)\b/i
BODY_OUTER = /\A\s*<(body)\b/i
end

Expand Down Expand Up @@ -184,7 +184,7 @@ def pluck_path(input) # :nodoc:
when PluckRegexp::TBODY then "tbody/*"
when PluckRegexp::TBODY_TR then "tbody/tr/*"
when PluckRegexp::COLGROUP then "colgroup/*"
when PluckRegexp::HEAD_OUTER then "head"
when PluckRegexp::HTML_INNER then "./*"
when PluckRegexp::BODY_OUTER then "body"
end
end
Expand Down
21 changes: 20 additions & 1 deletion test/nokogiri/html5/test_inference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
],
"html" => [
"<body><div>hello</div></body>",
"<head><meta charset=\"UTF-8\"><title>hello</title></head>"
"<head><meta charset=\"UTF-8\"><title>hello</title></head><body><div>hello</div></body>"
]
}

Expand Down Expand Up @@ -99,6 +99,25 @@
end
end

describe "passed a Fragment containing head and body" do
it "returns a Fragment containing both head and body" do
fragment = "<head></head><body></body>"
actual = Nokogiri::HTML5::Inference.parse(fragment).to_html

assert_equal(fragment, actual)
end
end

describe "passed a Fragment containing head and p" do
it "returns a Fragment containing both head and body" do
fragment = "<head><p>"
expected = "<head></head><body><p></p></body>"
actual = Nokogiri::HTML5::Inference.parse(fragment).to_html

assert_equal(expected, actual)
end
end

describe "multiple children that need plucking" do
it "parses correctly" do
fragment = "<tr><td>hello</td></tr><tr><td>world</td></tr>"
Expand Down

0 comments on commit 60ff491

Please sign in to comment.