diff --git a/src/Text/HTML/Scalpel/Internal/Select.hs b/src/Text/HTML/Scalpel/Internal/Select.hs index cb4900d..0524716 100644 --- a/src/Text/HTML/Scalpel/Internal/Select.hs +++ b/src/Text/HTML/Scalpel/Internal/Select.hs @@ -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) diff --git a/tests/TestMain.hs b/tests/TestMain.hs index 6872a32..72018fe 100644 --- a/tests/TestMain.hs +++ b/tests/TestMain.hs @@ -49,7 +49,7 @@ selectTests = "selectTests" ~: TestList [ , selectTest ("a" @: []) "foo" - [] + [""] , selectTest ("a" @: ["key" @= "value"]) @@ -162,6 +162,10 @@ scrapeTests = "scrapeTests" ~: TestList [ "foobar" (Just "bar") ((text $ "a" // "d") <|> (text $ "a" // "c")) + + , scrapeTest "" (Just "foobar") (attr "src" $ "img") + + , scrapeTest "" (Just "foobar") (attr "src" $ "img") ] scrapeTest :: (Eq a, Show a) => String -> Maybe a -> Scraper String a -> Test