(self, position, newChild)
| 187 | return lastChild |
| 188 | |
| 189 | def insert(self, position, newChild): |
| 190 | if isinstance(newChild, basestring) \ |
| 191 | and not isinstance(newChild, NavigableString): |
| 192 | newChild = NavigableString(newChild) |
| 193 | |
| 194 | position = min(position, len(self.contents)) |
| 195 | if hasattr(newChild, 'parent') and newChild.parent is not None: |
| 196 | # We're 'inserting' an element that's already one |
| 197 | # of this object's children. |
| 198 | if newChild.parent is self: |
| 199 | index = self.index(newChild) |
| 200 | if index > position: |
| 201 | # Furthermore we're moving it further down the |
| 202 | # list of this object's children. That means that |
| 203 | # when we extract this element, our target index |
| 204 | # will jump down one. |
| 205 | position = position - 1 |
| 206 | newChild.extract() |
| 207 | |
| 208 | newChild.parent = self |
| 209 | previousChild = None |
| 210 | if position == 0: |
| 211 | newChild.previousSibling = None |
| 212 | newChild.previous = self |
| 213 | else: |
| 214 | previousChild = self.contents[position-1] |
| 215 | newChild.previousSibling = previousChild |
| 216 | newChild.previousSibling.nextSibling = newChild |
| 217 | newChild.previous = previousChild._lastRecursiveChild() |
| 218 | if newChild.previous: |
| 219 | newChild.previous.next = newChild |
| 220 | |
| 221 | newChildsLastElement = newChild._lastRecursiveChild() |
| 222 | |
| 223 | if position >= len(self.contents): |
| 224 | newChild.nextSibling = None |
| 225 | |
| 226 | parent = self |
| 227 | parentsNextSibling = None |
| 228 | while not parentsNextSibling: |
| 229 | parentsNextSibling = parent.nextSibling |
| 230 | parent = parent.parent |
| 231 | if not parent: # This is the last element in the document. |
| 232 | break |
| 233 | if parentsNextSibling: |
| 234 | newChildsLastElement.next = parentsNextSibling |
| 235 | else: |
| 236 | newChildsLastElement.next = None |
| 237 | else: |
| 238 | nextChild = self.contents[position] |
| 239 | newChild.nextSibling = nextChild |
| 240 | if newChild.nextSibling: |
| 241 | newChild.nextSibling.previousSibling = newChild |
| 242 | newChildsLastElement.next = nextChild |
| 243 | |
| 244 | if newChildsLastElement.next: |
| 245 | newChildsLastElement.next.previous = newChildsLastElement |
| 246 | self.contents.insert(position, newChild) |
no test coverage detected