@@ -14,7 +14,6 @@
14module Text.XML.Lens (
14module Text.XML.Lens (
15 -- * Lenses, traversals for 'Element'
15 -- * Lenses, traversals for 'Element'
16 Element(..)
16 Element(..)
19 -- ** Names
18 -- ** Names
@@ -34,7 +33,6 @@ module Text.XML.Lens (
36 -- ** Children
35 -- ** Children
39 -- * Prisms for 'Node'
37 -- * Prisms for 'Node'
40 , Node(..)
38 , Node(..)
@@ -62,43 +60,48 @@ import Text.XML
62import Control.Lens
60import Control.Lens
63import Data.Text (Text)
61import Data.Text (Text)
64import Data.Map (Map)
62import Data.Map (Map)
65−import Control.Applicative
66import qualified Data.CaseInsensitive as CI
63import qualified Data.CaseInsensitive as CI
67import Data.Maybe (isNothing)
64import Data.Maybe (isNothing)
71prologue :: Lens' Document Prologue
66prologue :: Lens' Document Prologue
72prologue f doc = fmap (\p -> doc { documentPrologue = p} ) $ f $ documentPrologue doc
67prologue f doc = fmap (\p -> doc { documentPrologue = p} ) $ f $ documentPrologue doc
68+{-# INLINE prologue #-}
74-- | The root element of the document.
70-- | The root element of the document.
75root :: Lens' Document Element
71root :: Lens' Document Element
76root f doc = fmap (\p -> doc { documentRoot = p} ) $ f $ documentRoot doc
72root f doc = fmap (\p -> doc { documentRoot = p} ) $ f $ documentRoot doc
78epilogue :: Lens' Document [Miscellaneous]
75epilogue :: Lens' Document [Miscellaneous]
79epilogue f doc = fmap (\p -> doc { documentEpilogue = p} ) $ f $ documentEpilogue doc
76epilogue f doc = fmap (\p -> doc { documentEpilogue = p} ) $ f $ documentEpilogue doc
77+{-# INLINE epilogue #-}
81doctype :: Lens' Prologue (Maybe Doctype)
79doctype :: Lens' Prologue (Maybe Doctype)
82doctype f doc = fmap (\p -> doc { prologueDoctype = p} ) $ f $ prologueDoctype doc
80doctype f doc = fmap (\p -> doc { prologueDoctype = p} ) $ f $ prologueDoctype doc
81+{-# INLINE doctype #-}
84class AsInstruction t where
83class AsInstruction t where
85 _Instruction :: Prism' t Instruction
84 _Instruction :: Prism' t Instruction
87_instructionTarget :: Lens' Instruction Text
86_instructionTarget :: Lens' Instruction Text
88_instructionTarget f (Instruction t d) = f t <&> \t' -> Instruction t' d
87_instructionTarget f (Instruction t d) = f t <&> \t' -> Instruction t' d
88+{-# INLINE _instructionTarget #-}
90_instructionData :: Lens' Instruction Text
90_instructionData :: Lens' Instruction Text
91_instructionData f (Instruction t d) = f d <&> \d' -> Instruction t d'
91_instructionData f (Instruction t d) = f d <&> \d' -> Instruction t d'
92+{-# INLINE _instructionData #-}
93instance AsInstruction Node where
94instance AsInstruction Node where
94 _Instruction = prism' NodeInstruction $ \s -> case s of
95 _Instruction = prism' NodeInstruction $ \s -> case s of
95 NodeInstruction e -> Just e
96 NodeInstruction e -> Just e
96 _ -> Nothing
97 _ -> Nothing
98+ {-# INLINE _Instruction #-}
98instance AsInstruction Miscellaneous where
100instance AsInstruction Miscellaneous where
99 _Instruction = prism' MiscInstruction $ \s -> case s of
101 _Instruction = prism' MiscInstruction $ \s -> case s of
100 MiscInstruction e -> Just e
102 MiscInstruction e -> Just e
101 _ -> Nothing
103 _ -> Nothing
104+ {-# INLINE _Instruction #-}
103class AsComment t where
106class AsComment t where
104 _Comment :: Prism' t Text
107 _Comment :: Prism' t Text
@@ -107,33 +110,41 @@ instance AsComment Node where
107 _Comment = prism' NodeComment $ \s -> case s of
110 _Comment = prism' NodeComment $ \s -> case s of
108 NodeComment e -> Just e
111 NodeComment e -> Just e
109 _ -> Nothing
112 _ -> Nothing
113+ {-# INLINE _Comment #-}
111instance AsComment Miscellaneous where
115instance AsComment Miscellaneous where
112 _Comment = prism' MiscComment $ \s -> case s of
116 _Comment = prism' MiscComment $ \s -> case s of
113 MiscComment e -> Just e
117 MiscComment e -> Just e
114 _ -> Nothing
118 _ -> Nothing
119+ {-# INLINE _Comment #-}
116_nameLocalName :: Lens' Name Text
121_nameLocalName :: Lens' Name Text
117_nameLocalName f n = f (nameLocalName n) <&> \x -> n { nameLocalName = x }
122_nameLocalName f n = f (nameLocalName n) <&> \x -> n { nameLocalName = x }
123+{-# INLINE _nameLocalName #-}
119_nameNamespace :: Lens' Name (Maybe Text)
125_nameNamespace :: Lens' Name (Maybe Text)
120_nameNamespace f n = f (nameNamespace n) <&> \x -> n { nameNamespace = x }
126_nameNamespace f n = f (nameNamespace n) <&> \x -> n { nameNamespace = x }
127+{-# INLINE _nameNamespace #-}
122_namePrefix :: Lens' Name (Maybe Text)
129_namePrefix :: Lens' Name (Maybe Text)
123_namePrefix f n = f (namePrefix n) <&> \x -> n { namePrefix = x }
130_namePrefix f n = f (namePrefix n) <&> \x -> n { namePrefix = x }
131+{-# INLINE _namePrefix #-}
125_Element :: Prism' Node Element
133_Element :: Prism' Node Element
126_Element = prism' NodeElement $ \s -> case s of
134_Element = prism' NodeElement $ \s -> case s of
127 NodeElement e -> Just e
135 NodeElement e -> Just e
128 _ -> Nothing
136 _ -> Nothing
137+{-# INLINE _Element #-}
130_Content :: Prism' Node Text
139_Content :: Prism' Node Text
131_Content = prism' NodeContent $ \s -> case s of
140_Content = prism' NodeContent $ \s -> case s of
132 NodeContent e -> Just e
141 NodeContent e -> Just e
133 _ -> Nothing
142 _ -> Nothing
143+{-# INLINE _Content #-}
135name :: Lens' Element Name
145name :: Lens' Element Name
136name f e = f (elementName e) <&> \x -> e { elementName = x }
146name f e = f (elementName e) <&> \x -> e { elementName = x }
138localName :: Lens' Element Text
149localName :: Lens' Element Text
139localName = name . _nameLocalName
150localName = name . _nameLocalName
@@ -141,69 +152,65 @@ localName = name . _nameLocalName
142attrs :: Lens' Element (Map Name Text)
153attrs :: Lens' Element (Map Name Text)
143attrs f e = fmap (\x -> e { elementAttributes = x }) $ f $ elementAttributes e
154attrs f e = fmap (\x -> e { elementAttributes = x }) $ f $ elementAttributes e
145nodes :: Lens' Element [Node]
157nodes :: Lens' Element [Node]
146nodes f e = fmap (\x -> e { elementNodes = x }) $ f $ elementNodes e
158nodes f e = fmap (\x -> e { elementNodes = x }) $ f $ elementNodes e
148attr :: Name -> Traversal' Element Text
161attr :: Name -> Traversal' Element Text
149attr n = attrs . ix n
162attr n = attrs . ix n
151attribute :: Name -> Lens' Element (Maybe Text)
165attribute :: Name -> Lens' Element (Maybe Text)
152attribute n = attrs . at n
166attribute n = attrs . at n
153−
167+{-# INLINE attribute #-}
154−-- | Traverse itself with its all children. Rewriting subnodes of each children will break a traversal law.
155−entire :: Traversal' Element Element
156−entire f e@(Element _ _ ns) = com <$> f e <*> traverse (_Element (entire f)) ns where
157− com (Element n a _) = Element n a
158−{-# DEPRECATED entire "Use cosmos or deep instead" #-}
160-- | Traverse elements which has the specified *local* name (case-insensitive).
169-- | Traverse elements which has the specified *local* name (case-insensitive).
161named :: CI.CI Text -> Traversal' Element Element
170named :: CI.CI Text -> Traversal' Element Element
162named n f s
171named n f s
163 | CI.mk (nameLocalName (elementName s)) == n = f s
172 | CI.mk (nameLocalName (elementName s)) == n = f s
164 | otherwise = pure s
173 | otherwise = pure s
166-- | Old name for 'named'
176-- | Old name for 'named'
167ell :: Text -> Traversal' Element Element
177ell :: Text -> Traversal' Element Element
168−ell = named . CI.mk
178+ell n = named $ CI.mk n
170-- | Traverse elements which has the specified name.
180-- | Traverse elements which has the specified name.
171el :: Name -> Traversal' Element Element
181el :: Name -> Traversal' Element Element
173 | elementName s == n = f s
183 | elementName s == n = f s
174 | otherwise = pure s
184 | otherwise = pure s
185+{-# DEPRECATED el "Use named instead" #-}
176attributeSatisfies :: Name -> (Text -> Bool) -> Traversal' Element Element
187attributeSatisfies :: Name -> (Text -> Bool) -> Traversal' Element Element
177attributeSatisfies n p = attributeSatisfies' n (maybe False p)
188attributeSatisfies n p = attributeSatisfies' n (maybe False p)
189+{-# INLINE attributeSatisfies #-}
179attributeSatisfies' :: Name -> (Maybe Text -> Bool) -> Traversal' Element Element
191attributeSatisfies' :: Name -> (Maybe Text -> Bool) -> Traversal' Element Element
180attributeSatisfies' n p = filtered (p . preview (attrs . ix n))
192attributeSatisfies' n p = filtered (p . preview (attrs . ix n))
193+{-# INLINE attributeSatisfies' #-}
182withoutAttribute :: Name -> Traversal' Element Element
195withoutAttribute :: Name -> Traversal' Element Element
183withoutAttribute n = attributeSatisfies' n isNothing
196withoutAttribute n = attributeSatisfies' n isNothing
197+{-# INLINE withoutAttribute #-}
185attributeIs :: Name -> Text -> Traversal' Element Element
199attributeIs :: Name -> Text -> Traversal' Element Element
186attributeIs n v = attributeSatisfies n (==v)
200attributeIs n v = attributeSatisfies n (==v)
201+{-# INLINE attributeIs #-}
188-- | Traverse all contents of the element.
203-- | Traverse all contents of the element.
189text :: Traversal' Element Text
204text :: Traversal' Element Text
190text = nodes . traverse . _Content
205text = nodes . traverse . _Content
192-- | Traverse all comments of the element.
208-- | Traverse all comments of the element.
193comment :: Traversal' Element Text
209comment :: Traversal' Element Text
194comment = nodes . traverse . _Comment
210comment = nodes . traverse . _Comment
211+{-# INLINE comment #-}
196-- | 'plate' traverses over its sub-elements.
213-- | 'plate' traverses over its sub-elements.
197instance Plated Element where
214instance Plated Element where
198 plate = nodes . traverse . _Element
215 plate = nodes . traverse . _Element
200−-- | Combine two 'Traversal's just like XPath's slash. Identical to ('...').
203−-- l ./ m ≡ l . 'plate' . m
216+ {-# INLINE plate #-}
205−(./) :: (Applicative f, Plated c) => LensLike f s t c c -> Over p f c c a b -> Over p f s t a b
209−{-# DEPRECATED (./) "Use (...) instead" #-}