Skip to content

Commit

Permalink
(closes #2) Fix bug in selecting <img> tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
fimad committed Mar 31, 2015
1 parent 8d85bbf commit 11dd91e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/Text/HTML/Scalpel/Internal/Select.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,22 @@ checkPreds preds (TagSoup.TagOpen _ attrs:_)
= and [or [p attr | attr <- attrs] | p <- preds]
checkPreds _ _ = False

-- Given a list of tags, return the prefix that of the tags up to the closing
-- tag that corresponds to the initial tag.
-- Given a list of tags, return the prefix of the tags up to the closing tag
-- that corresponds to the initial tag.
extractTagBlock :: TagSoup.StringLike str
=> [TagSoup.Tag str] -> [[TagSoup.Tag str]]
extractTagBlock [] = []
extractTagBlock (openTag : tags)
| not $ TagSoup.isTagOpen openTag = []
| otherwise = map (openTag :)
| otherwise = fakeClosingTag openTag
$ map (openTag :)
$ splitBlock (getTagName openTag) 0 tags
where
-- To handle tags that do not have a closing tag, fake an empty block by
-- adding a closing tag.
fakeClosingTag openTag@(TagSoup.TagOpen name _) []
= [[openTag, TagSoup.TagClose name]]
fakeClosingTag _ blocks = blocks

splitBlock _ _ [] = []
splitBlock name depth (tag : tags)
Expand Down
6 changes: 5 additions & 1 deletion tests/TestMain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ selectTests = "selectTests" ~: TestList [
, selectTest
("a" @: [])
"<a>foo"
[]
["<a></a>"]

, selectTest
("a" @: ["key" @= "value"])
Expand Down Expand Up @@ -162,6 +162,10 @@ scrapeTests = "scrapeTests" ~: TestList [
"<a><b>foo</b></a><a><c>bar</c></a>"
(Just "bar")
((text $ "a" // "d") <|> (text $ "a" // "c"))

, scrapeTest "<img src='foobar'>" (Just "foobar") (attr "src" $ "img")

, scrapeTest "<img src='foobar' />" (Just "foobar") (attr "src" $ "img")
]

scrapeTest :: (Eq a, Show a) => String -> Maybe a -> Scraper String a -> Test
Expand Down

0 comments on commit 11dd91e

Please sign in to comment.