@@ -3,18 +3,18 @@
33-----------------------------------------------------------------------------
44-- |
55-- Module : Text.XML.Lens
6−-- Copyright : (C) 2013 Fumiaki Kinoshita
6+-- Copyright : (C) 2015 Fumiaki Kinoshita
77-- License : BSD-style (see the file LICENSE)
88--
99-- Maintainer : Fumiaki Kinoshita <fumiexcel@gmail.com>
10−-- Stability : experimental
10+-- Stability : provisional
1111-- Portability : non-portable
12−-- Useful traversals inspired by XPath
12+--
1313----------------------------------------------------------------------------
1414module Text.XML.Lens (
1515 -- * Lenses, traversals for 'Element'
1616 Element(..)
17− , (./)
17+ , (...)
1818 -- ** Names
1919 , name
2020 , localName
@@ -24,6 +24,8 @@ module Text.XML.Lens (
2424 -- ** Attributes
2525 , attributeIs
2626 , attributeSatisfies
27+ , attributeSatisfies'
28+ , withoutAttribute
2729 , attr
2830 , attribute
2931 , attrs
@@ -31,7 +33,6 @@ module Text.XML.Lens (
3133 , text
3234 , comment
3335 -- ** Children
34− , entire
3536 , nodes
3637 -- * Prisms for 'Node'
3738 , Node(..)
@@ -54,48 +55,53 @@ module Text.XML.Lens (
5455 , Instruction(..)
5556 , _instructionTarget
5657 , _instructionData
57− -- * Reexport
58− , module Control.Lens
5958 ) where
6059import Text.XML
6160import Control.Lens
6261import Data.Text (Text)
6362import Data.Map (Map)
64−import Control.Applicative
65−
66−infixr 9 ./
63+import qualified Data.CaseInsensitive as CI
64+import Data.Maybe (isNothing)
6765
6866prologue :: Lens' Document Prologue
6967prologue f doc = fmap (\p -> doc { documentPrologue = p} ) $ f $ documentPrologue doc
68+{-# INLINE prologue #-}
7069
7170-- | The root element of the document.
7271root :: Lens' Document Element
7372root f doc = fmap (\p -> doc { documentRoot = p} ) $ f $ documentRoot doc
73+{-# INLINE root #-}
7474
7575epilogue :: Lens' Document [Miscellaneous]
7676epilogue f doc = fmap (\p -> doc { documentEpilogue = p} ) $ f $ documentEpilogue doc
77+{-# INLINE epilogue #-}
7778
7879doctype :: Lens' Prologue (Maybe Doctype)
7980doctype f doc = fmap (\p -> doc { prologueDoctype = p} ) $ f $ prologueDoctype doc
81+{-# INLINE doctype #-}
8082
8183class AsInstruction t where
8284 _Instruction :: Prism' t Instruction
8385
8486_instructionTarget :: Lens' Instruction Text
8587_instructionTarget f (Instruction t d) = f t <&> \t' -> Instruction t' d
88+{-# INLINE _instructionTarget #-}
8689
8790_instructionData :: Lens' Instruction Text
8891_instructionData f (Instruction t d) = f d <&> \d' -> Instruction t d'
92+{-# INLINE _instructionData #-}
8993
9094instance AsInstruction Node where
9195 _Instruction = prism' NodeInstruction $ \s -> case s of
9296 NodeInstruction e -> Just e
9397 _ -> Nothing
98+ {-# INLINE _Instruction #-}
9499
95100instance AsInstruction Miscellaneous where
96101 _Instruction = prism' MiscInstruction $ \s -> case s of
97102 MiscInstruction e -> Just e
98103 _ -> Nothing
104+ {-# INLINE _Instruction #-}
99105
100106class AsComment t where
101107 _Comment :: Prism' t Text
@@ -104,33 +110,41 @@ instance AsComment Node where
104110 _Comment = prism' NodeComment $ \s -> case s of
105111 NodeComment e -> Just e
106112 _ -> Nothing
113+ {-# INLINE _Comment #-}
107114
108115instance AsComment Miscellaneous where
109116 _Comment = prism' MiscComment $ \s -> case s of
110117 MiscComment e -> Just e
111118 _ -> Nothing
119+ {-# INLINE _Comment #-}
112120
113121_nameLocalName :: Lens' Name Text
114122_nameLocalName f n = f (nameLocalName n) <&> \x -> n { nameLocalName = x }
123+{-# INLINE _nameLocalName #-}
115124
116125_nameNamespace :: Lens' Name (Maybe Text)
117126_nameNamespace f n = f (nameNamespace n) <&> \x -> n { nameNamespace = x }
127+{-# INLINE _nameNamespace #-}
118128
119129_namePrefix :: Lens' Name (Maybe Text)
120130_namePrefix f n = f (namePrefix n) <&> \x -> n { namePrefix = x }
131+{-# INLINE _namePrefix #-}
121132
122133_Element :: Prism' Node Element
123134_Element = prism' NodeElement $ \s -> case s of
124135 NodeElement e -> Just e
125136 _ -> Nothing
137+{-# INLINE _Element #-}
126138
127139_Content :: Prism' Node Text
128140_Content = prism' NodeContent $ \s -> case s of
129141 NodeContent e -> Just e
130142 _ -> Nothing
143+{-# INLINE _Content #-}
131144
132145name :: Lens' Element Name
133146name f e = f (elementName e) <&> \x -> e { elementName = x }
147+{-# INLINE name #-}
134148
135149localName :: Lens' Element Text
136150localName = name . _nameLocalName
@@ -138,58 +152,65 @@ localName = name . _nameLocalName
138152
139153attrs :: Lens' Element (Map Name Text)
140154attrs f e = fmap (\x -> e { elementAttributes = x }) $ f $ elementAttributes e
155+{-# INLINE attrs #-}
141156
142157nodes :: Lens' Element [Node]
143158nodes f e = fmap (\x -> e { elementNodes = x }) $ f $ elementNodes e
159+{-# INLINE nodes #-}
144160
145161attr :: Name -> Traversal' Element Text
146162attr n = attrs . ix n
163+{-# INLINE attr #-}
147164
148165attribute :: Name -> Lens' Element (Maybe Text)
149166attribute n = attrs . at n
167+{-# INLINE attribute #-}
150168
151−-- | Traverse itself with its all children. Rewriting subnodes of each children will break a traversal law.
152−entire :: Traversal' Element Element
153−entire f e@(Element _ _ ns) = com <$> f e <*> traverse (_Element (entire f)) ns where
154− com (Element n a _) = Element n a
155−
156−-- | Traverse elements which has the specified *local* name.
157−named :: Text -> Traversal' Element Element
169+-- | Traverse elements which has the specified *local* name (case-insensitive).
170+named :: CI.CI Text -> Traversal' Element Element
158171named n f s
159− | nameLocalName (elementName s) == n = f s
172+ | CI.mk (nameLocalName (elementName s)) == n = f s
160173 | otherwise = pure s
174+{-# INLINE named #-}
161175
176+-- | Old name for 'named'
162177ell :: Text -> Traversal' Element Element
163−ell = named
178+ell n = named $ CI.mk n
164179
165180-- | Traverse elements which has the specified name.
166181el :: Name -> Traversal' Element Element
167182el n f s
168183 | elementName s == n = f s
169184 | otherwise = pure s
185+{-# DEPRECATED el "Use named instead" #-}
170186
171187attributeSatisfies :: Name -> (Text -> Bool) -> Traversal' Element Element
172−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 #-}
173198
174199attributeIs :: Name -> Text -> Traversal' Element Element
175200attributeIs n v = attributeSatisfies n (==v)
201+{-# INLINE attributeIs #-}
176202
177203-- | Traverse all contents of the element.
178204text :: Traversal' Element Text
179205text = nodes . traverse . _Content
206+{-# INLINE text #-}
180207
181208-- | Traverse all comments of the element.
182209comment :: Traversal' Element Text
183210comment = nodes . traverse . _Comment
211+{-# INLINE comment #-}
184212
213+-- | 'plate' traverses over its sub-elements.
185214instance Plated Element where
186215 plate = nodes . traverse . _Element
187−
188−-- | Combine two 'Traversal's just like XPath's slash.
189−--
190−-- @
191−-- l ./ m ≡ l . 'plate' . m
192−-- @
193−(./) :: (Applicative f, Plated c) => LensLike f s t c c -> Over p f c c a b -> Over p f s t a b
194−(./) = (...)
195−{-# INLINE (./) #-}
216+ {-# INLINE plate #-}