(self, tag, attrs, new_namespaces, qname_text=None)
| 1943 | self._start(tag, attrs, new_namespaces) |
| 1944 | |
| 1945 | def _start(self, tag, attrs, new_namespaces, qname_text=None): |
| 1946 | if self._exclude_attrs is not None and attrs: |
| 1947 | attrs = {k: v for k, v in attrs.items() if k not in self._exclude_attrs} |
| 1948 | |
| 1949 | qnames = {tag, *attrs} |
| 1950 | resolved_names = {} |
| 1951 | |
| 1952 | # Resolve prefixes in attribute and tag text. |
| 1953 | if qname_text is not None: |
| 1954 | qname = resolved_names[qname_text] = self._resolve_prefix_name(qname_text) |
| 1955 | qnames.add(qname) |
| 1956 | if self._find_qname_aware_attrs is not None and attrs: |
| 1957 | qattrs = self._find_qname_aware_attrs(attrs) |
| 1958 | if qattrs: |
| 1959 | for attr_name in qattrs: |
| 1960 | value = attrs[attr_name] |
| 1961 | if _looks_like_prefix_name(value): |
| 1962 | qname = resolved_names[value] = self._resolve_prefix_name(value) |
| 1963 | qnames.add(qname) |
| 1964 | else: |
| 1965 | qattrs = None |
| 1966 | else: |
| 1967 | qattrs = None |
| 1968 | |
| 1969 | # Assign prefixes in lexicographical order of used URIs. |
| 1970 | parse_qname = self._qname |
| 1971 | parsed_qnames = {n: parse_qname(n) for n in sorted( |
| 1972 | qnames, key=lambda n: n.split('}', 1))} |
| 1973 | |
| 1974 | # Write namespace declarations in prefix order ... |
| 1975 | if new_namespaces: |
| 1976 | attr_list = [ |
| 1977 | ('xmlns:' + prefix if prefix else 'xmlns', uri) |
| 1978 | for uri, prefix in new_namespaces |
| 1979 | ] |
| 1980 | attr_list.sort() |
| 1981 | else: |
| 1982 | # almost always empty |
| 1983 | attr_list = [] |
| 1984 | |
| 1985 | # ... followed by attributes in URI+name order |
| 1986 | if attrs: |
| 1987 | for k, v in sorted(attrs.items()): |
| 1988 | if qattrs is not None and k in qattrs and v in resolved_names: |
| 1989 | v = parsed_qnames[resolved_names[v]][0] |
| 1990 | attr_qname, attr_name, uri = parsed_qnames[k] |
| 1991 | # No prefix for attributes in default ('') namespace. |
| 1992 | attr_list.append((attr_qname if uri else attr_name, v)) |
| 1993 | |
| 1994 | # Honour xml:space attributes. |
| 1995 | space_behaviour = attrs.get('{http://www.w3.org/XML/1998/namespace}space') |
| 1996 | self._preserve_space.append( |
| 1997 | space_behaviour == 'preserve' if space_behaviour |
| 1998 | else self._preserve_space[-1]) |
| 1999 | |
| 2000 | # Write the tag. |
| 2001 | write = self._write |
| 2002 | write('<' + parsed_qnames[tag][0]) |
no test coverage detected