MCPcopy
hub / github.com/csev/py4e / appendChild

Method appendChild

code3/bs4/builder/_html5lib.py:147–197  ·  view source on GitHub ↗
(self, node)

Source from the content-addressed store, hash-verified

145 self.namespace = namespace
146
147 def appendChild(self, node):
148 string_child = child = None
149 if isinstance(node, str):
150 # Some other piece of code decided to pass in a string
151 # instead of creating a TextElement object to contain the
152 # string.
153 string_child = child = node
154 elif isinstance(node, Tag):
155 # Some other piece of code decided to pass in a Tag
156 # instead of creating an Element object to contain the
157 # Tag.
158 child = node
159 elif node.element.__class__ == NavigableString:
160 string_child = child = node.element
161 else:
162 child = node.element
163
164 if not isinstance(child, str) and child.parent is not None:
165 node.element.extract()
166
167 if (string_child and self.element.contents
168 and self.element.contents[-1].__class__ == NavigableString):
169 # We are appending a string onto another string.
170 # TODO This has O(n^2) performance, for input like
171 # "a</a>a</a>a</a>..."
172 old_element = self.element.contents[-1]
173 new_element = self.soup.new_string(old_element + string_child)
174 old_element.replace_with(new_element)
175 self.soup._most_recent_element = new_element
176 else:
177 if isinstance(node, str):
178 # Create a brand new NavigableString from this string.
179 child = self.soup.new_string(node)
180
181 # Tell Beautiful Soup to act as if it parsed this element
182 # immediately after the parent's last descendant. (Or
183 # immediately after the parent, if it has no children.)
184 if self.element.contents:
185 most_recent_element = self.element._last_descendant(False)
186 elif self.element.next_element is not None:
187 # Something from further ahead in the parse tree is
188 # being inserted into this earlier element. This is
189 # very annoying because it means an expensive search
190 # for the last element in the tree.
191 most_recent_element = self.soup._last_descendant()
192 else:
193 most_recent_element = self.element
194
195 self.soup.object_was_parsed(
196 child, parent=self.element,
197 most_recent_element=most_recent_element)
198
199 def getAttributes(self):
200 return AttrList(self.element)

Callers 15

insertTextMethod · 0.95
CodeMirrorFunction · 0.45
codemirrorepl.jsFile · 0.45
DisplayFunction · 0.45
updateGuttersFunction · 0.45
patchDisplayFunction · 0.45
ensureLineWrappedFunction · 0.45
updateLineGutterFunction · 0.45
insertLineWidgetsForFunction · 0.45
drawSelectionCursorFunction · 0.45
addFunction · 0.45

Calls 5

extractMethod · 0.45
new_stringMethod · 0.45
replace_withMethod · 0.45
_last_descendantMethod · 0.45
object_was_parsedMethod · 0.45

Tested by

no test coverage detected