@@ -1,24 +1,31 @@
11{-# LANGUAGE Rank2Types, FlexibleContexts #-}
2+{-# OPTIONS_GHC -fno-warn-orphans #-}
23-----------------------------------------------------------------------------
34-- |
45-- Module : Text.XML.Lens
5−-- Copyright : (C) 2013 Fumiaki Kinoshita
6+-- Copyright : (C) 2015 Fumiaki Kinoshita
67-- License : BSD-style (see the file LICENSE)
78--
89-- Maintainer : Fumiaki Kinoshita <fumiexcel@gmail.com>
9−-- Stability : experimental
10+-- Stability : provisional
1011-- Portability : non-portable
11−-- Useful traversals inspired by XPath
12+--
1213----------------------------------------------------------------------------
1314module Text.XML.Lens (
1415 -- * Lenses, traversals for 'Element'
15− (./)
16+ Element(..)
17+ , (...)
1618 -- ** Names
1719 , name
20+ , localName
1821 , el
22+ , ell
23+ , named
1924 -- ** Attributes
2025 , attributeIs
2126 , attributeSatisfies
27+ , attributeSatisfies'
28+ , withoutAttribute
2229 , attr
2330 , attribute
2431 , attrs
@@ -26,23 +33,26 @@ module Text.XML.Lens (
2633 , text
2734 , comment
2835 -- ** Children
29− , entire
3036 , nodes
3137 -- * Prisms for 'Node'
38+ , Node(..)
3239 , _Element
3340 , _Content
3441 , AsInstruction(..)
3542 , AsComment(..)
3643 -- * Lenses for 'Document'
44+ , Document(..)
3745 , root
3846 , prologue
3947 , epilogue
4048 , doctype
4149 -- * Lenses for 'Name'
50+ , Name(..)
4251 , _nameLocalName
4352 , _nameNamespace
4453 , _namePrefix
4554 -- * Lenses for 'Instruction'
55+ , Instruction(..)
4656 , _instructionTarget
4757 , _instructionData
4858 ) where
@@ -50,41 +60,48 @@ import Text.XML
5060import Control.Lens
5161import Data.Text (Text)
5262import Data.Map (Map)
53−import Control.Applicative
54−
55−infixr 9 ./
63+import qualified Data.CaseInsensitive as CI
64+import Data.Maybe (isNothing)
5665
5766prologue :: Lens' Document Prologue
5867prologue f doc = fmap (\p -> doc { documentPrologue = p} ) $ f $ documentPrologue doc
68+{-# INLINE prologue #-}
5969
6070-- | The root element of the document.
6171root :: Lens' Document Element
6272root f doc = fmap (\p -> doc { documentRoot = p} ) $ f $ documentRoot doc
73+{-# INLINE root #-}
6374
6475epilogue :: Lens' Document [Miscellaneous]
6576epilogue f doc = fmap (\p -> doc { documentEpilogue = p} ) $ f $ documentEpilogue doc
77+{-# INLINE epilogue #-}
6678
6779doctype :: Lens' Prologue (Maybe Doctype)
6880doctype f doc = fmap (\p -> doc { prologueDoctype = p} ) $ f $ prologueDoctype doc
81+{-# INLINE doctype #-}
6982
7083class AsInstruction t where
7184 _Instruction :: Prism' t Instruction
7285
7386_instructionTarget :: Lens' Instruction Text
7487_instructionTarget f (Instruction t d) = f t <&> \t' -> Instruction t' d
88+{-# INLINE _instructionTarget #-}
7589
7690_instructionData :: Lens' Instruction Text
7791_instructionData f (Instruction t d) = f d <&> \d' -> Instruction t d'
92+{-# INLINE _instructionData #-}
7893
7994instance AsInstruction Node where
8095 _Instruction = prism' NodeInstruction $ \s -> case s of
8196 NodeInstruction e -> Just e
8297 _ -> Nothing
98+ {-# INLINE _Instruction #-}
8399
84100instance AsInstruction Miscellaneous where
85101 _Instruction = prism' MiscInstruction $ \s -> case s of
86102 MiscInstruction e -> Just e
87103 _ -> Nothing
104+ {-# INLINE _Instruction #-}
88105
89106class AsComment t where
90107 _Comment :: Prism' t Text
@@ -93,78 +110,107 @@ instance AsComment Node where
93110 _Comment = prism' NodeComment $ \s -> case s of
94111 NodeComment e -> Just e
95112 _ -> Nothing
113+ {-# INLINE _Comment #-}
96114
97115instance AsComment Miscellaneous where
98116 _Comment = prism' MiscComment $ \s -> case s of
99117 MiscComment e -> Just e
100118 _ -> Nothing
119+ {-# INLINE _Comment #-}
101120
102121_nameLocalName :: Lens' Name Text
103−_nameLocalName f n = fmap (\x -> n { nameLocalName = x }) $ f $ nameLocalName n
122+_nameLocalName f n = f (nameLocalName n) <&> \x -> n { nameLocalName = x }
123+{-# INLINE _nameLocalName #-}
104124
105125_nameNamespace :: Lens' Name (Maybe Text)
106−_nameNamespace f n = fmap (\x -> n { nameNamespace = x }) $ f $ nameNamespace n
126+_nameNamespace f n = f (nameNamespace n) <&> \x -> n { nameNamespace = x }
127+{-# INLINE _nameNamespace #-}
107128
108129_namePrefix :: Lens' Name (Maybe Text)
109−_namePrefix f n = fmap (\x -> n { namePrefix = x }) $ f $ namePrefix n
130+_namePrefix f n = f (namePrefix n) <&> \x -> n { namePrefix = x }
131+{-# INLINE _namePrefix #-}
110132
111133_Element :: Prism' Node Element
112134_Element = prism' NodeElement $ \s -> case s of
113135 NodeElement e -> Just e
114136 _ -> Nothing
137+{-# INLINE _Element #-}
115138
116139_Content :: Prism' Node Text
117140_Content = prism' NodeContent $ \s -> case s of
118141 NodeContent e -> Just e
119142 _ -> Nothing
143+{-# INLINE _Content #-}
120144
121145name :: Lens' Element Name
122−name f e = fmap (\x -> e { elementName = x }) $ f $ elementName e
146+name f e = f (elementName e) <&> \x -> e { elementName = x }
147+{-# INLINE name #-}
148+
149+localName :: Lens' Element Text
150+localName = name . _nameLocalName
151+{-# INLINE localName #-}
123152
124153attrs :: Lens' Element (Map Name Text)
125154attrs f e = fmap (\x -> e { elementAttributes = x }) $ f $ elementAttributes e
155+{-# INLINE attrs #-}
126156
127157nodes :: Lens' Element [Node]
128158nodes f e = fmap (\x -> e { elementNodes = x }) $ f $ elementNodes e
159+{-# INLINE nodes #-}
129160
130−attr :: Name -> IndexedTraversal' Name Element Text
161+attr :: Name -> Traversal' Element Text
131162attr n = attrs . ix n
163+{-# INLINE attr #-}
132164
133−attribute :: Name -> IndexedLens' Name Element (Maybe Text)
165+attribute :: Name -> Lens' Element (Maybe Text)
134166attribute n = attrs . at n
167+{-# INLINE attribute #-}
168+
169+-- | Traverse elements which has the specified *local* name (case-insensitive).
170+named :: CI.CI Text -> Traversal' Element Element
171+named n f s
172+ | CI.mk (nameLocalName (elementName s)) == n = f s
173+ | otherwise = pure s
174+{-# INLINE named #-}
135175
136−-- | Traverse itself with its all children.
137−entire :: Traversal' Element Element
138−entire f e@(Element _ _ ns) = com <$> f e <*> traverse (_Element (entire f)) ns where
139− com (Element n a _) ns = Element n a ns
176+-- | Old name for 'named'
177+ell :: Text -> Traversal' Element Element
178+ell n = named $ CI.mk n
140179
141180-- | Traverse elements which has the specified name.
142181el :: Name -> Traversal' Element Element
143182el n f s
144183 | elementName s == n = f s
145184 | otherwise = pure s
185+{-# DEPRECATED el "Use named instead" #-}
146186
147187attributeSatisfies :: Name -> (Text -> Bool) -> Traversal' Element Element
148−attributeSatisfies n p = filtered (maybe False p . preview (attrs . ix n))
188+attributeSatisfies n p = attributeSatisfies' n (maybe False p)
189+{-# INLINE attributeSatisfies #-}
190+
191+attributeSatisfies' :: Name -> (Maybe Text -> Bool) -> Traversal' Element Element
192+attributeSatisfies' n p = filtered (p . preview (attrs . ix n))
193+{-# INLINE attributeSatisfies' #-}
194+
195+withoutAttribute :: Name -> Traversal' Element Element
196+withoutAttribute n = attributeSatisfies' n isNothing
197+{-# INLINE withoutAttribute #-}
149198
150199attributeIs :: Name -> Text -> Traversal' Element Element
151200attributeIs n v = attributeSatisfies n (==v)
201+{-# INLINE attributeIs #-}
152202
153203-- | Traverse all contents of the element.
154204text :: Traversal' Element Text
155205text = nodes . traverse . _Content
206+{-# INLINE text #-}
156207
157208-- | Traverse all comments of the element.
158209comment :: Traversal' Element Text
159210comment = nodes . traverse . _Comment
211+{-# INLINE comment #-}
160212
213+-- | 'plate' traverses over its sub-elements.
161214instance Plated Element where
162215 plate = nodes . traverse . _Element
163−
164−-- | Combine two 'Traversal's just like XPath's slash.
165−--
166−-- @
167−-- l ./ m ≡ l . 'plate' . m
168−-- @
169−(./) :: Plated a => Traversal s t a a -> Traversal a a u v -> Traversal s t u v
170−l ./ m = l . plate . m
216+ {-# INLINE plate #-}