(self, position, newChild)
| 222 | return lastChild |
| 223 | |
| 224 | def insert(self, position, newChild): |
| 225 | if isinstance(newChild, basestring) \ |
| 226 | and not isinstance(newChild, NavigableString): |
| 227 | newChild = NavigableString(newChild) |
| 228 | |
| 229 | position = min(position, len(self.contents)) |
| 230 | if hasattr(newChild, 'parent') and newChild.parent is not None: |
| 231 | # We're 'inserting' an element that's already one |
| 232 | # of this object's children. |
| 233 | if newChild.parent is self: |
| 234 | index = self.index(newChild) |
| 235 | if index > position: |
| 236 | # Furthermore we're moving it further down the |
| 237 | # list of this object's children. That means that |
| 238 | # when we extract this element, our target index |
| 239 | # will jump down one. |
| 240 | position = position - 1 |
| 241 | newChild.extract() |
| 242 | |
| 243 | newChild.parent = self |
| 244 | previousChild = None |
| 245 | if position == 0: |
| 246 | newChild.previousSibling = None |
| 247 | newChild.previous = self |
| 248 | else: |
| 249 | previousChild = self.contents[position-1] |
| 250 | newChild.previousSibling = previousChild |
| 251 | newChild.previousSibling.nextSibling = newChild |
| 252 | newChild.previous = previousChild._lastRecursiveChild() |
| 253 | if newChild.previous: |
| 254 | newChild.previous.next = newChild |
| 255 | |
| 256 | newChildsLastElement = newChild._lastRecursiveChild() |
| 257 | |
| 258 | if position >= len(self.contents): |
| 259 | newChild.nextSibling = None |
| 260 | |
| 261 | parent = self |
| 262 | parentsNextSibling = None |
| 263 | while not parentsNextSibling: |
| 264 | parentsNextSibling = parent.nextSibling |
| 265 | parent = parent.parent |
| 266 | if not parent: # This is the last element in the document. |
| 267 | break |
| 268 | if parentsNextSibling: |
| 269 | newChildsLastElement.next = parentsNextSibling |
| 270 | else: |
| 271 | newChildsLastElement.next = None |
| 272 | else: |
| 273 | nextChild = self.contents[position] |
| 274 | newChild.nextSibling = nextChild |
| 275 | if newChild.nextSibling: |
| 276 | newChild.nextSibling.previousSibling = newChild |
| 277 | newChildsLastElement.next = nextChild |
| 278 | |
| 279 | if newChildsLastElement.next: |
| 280 | newChildsLastElement.next.previous = newChildsLastElement |
| 281 | self.contents.insert(position, newChild) |
no test coverage detected