Mercurial > prosody-hg
comparison util/stanza.lua @ 12636:e8934ce6ea0f
util.stanza: Add method for extracting a single attribute value
Sometimes you only care about a single attribute, but the child tag
itself may be optional, leading to needing `tag and tag.attr.foo` or
`stanza:find("tag@foo")`.
The `:find()` method is fairly complex, so avoiding it for this kind of
simpler use case is a win.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Wed, 17 Aug 2022 19:04:30 +0200 |
| parents | b6b01724e04f |
| children | 5b69ecaf3427 |
comparison
equal
deleted
inserted
replaced
| 12635:f928cb5c5d04 | 12636:e8934ce6ea0f |
|---|---|
| 174 return tag:get_text(); | 174 return tag:get_text(); |
| 175 end | 175 end |
| 176 return nil; | 176 return nil; |
| 177 end | 177 end |
| 178 | 178 |
| 179 function stanza_mt:get_child_attr(name, xmlns, attr) | |
| 180 local tag = self:get_child(name, xmlns); | |
| 181 if tag then | |
| 182 return tag.attr[attr]; | |
| 183 end | |
| 184 return nil; | |
| 185 end | |
| 186 | |
| 179 function stanza_mt:child_with_name(name) | 187 function stanza_mt:child_with_name(name) |
| 180 for _, child in ipairs(self.tags) do | 188 for _, child in ipairs(self.tags) do |
| 181 if child.name == name then return child; end | 189 if child.name == name then return child; end |
| 182 end | 190 end |
| 183 end | 191 end |
