normalize writes the normalized XML content of r to w. It applies the following rules - Rename namespace prefixes according to an internal heuristic. - Remove unnecessary namespace declarations. - Sort attributes in XML start elements in lexical order of their fully qualified name. - Remove XML dir
(w io.Writer, r io.Reader)
| 836 | // instructed to do so. |
| 837 | // - Remove comments, if instructed to do so. |
| 838 | func (n *xmlNormalizer) normalize(w io.Writer, r io.Reader) error { |
| 839 | d := ixml.NewDecoder(r) |
| 840 | e := ixml.NewEncoder(w) |
| 841 | for { |
| 842 | t, err := d.Token() |
| 843 | if err != nil { |
| 844 | if t == nil && err == io.EOF { |
| 845 | break |
| 846 | } |
| 847 | return err |
| 848 | } |
| 849 | switch val := t.(type) { |
| 850 | case ixml.Directive, ixml.ProcInst: |
| 851 | continue |
| 852 | case ixml.Comment: |
| 853 | if n.omitComments { |
| 854 | continue |
| 855 | } |
| 856 | case ixml.CharData: |
| 857 | if n.omitWhitespace && len(bytes.TrimSpace(val)) == 0 { |
| 858 | continue |
| 859 | } |
| 860 | case ixml.StartElement: |
| 861 | start, _ := ixml.CopyToken(val).(ixml.StartElement) |
| 862 | attr := start.Attr[:0] |
| 863 | for _, a := range start.Attr { |
| 864 | if a.Name.Space == "xmlns" || a.Name.Local == "xmlns" { |
| 865 | continue |
| 866 | } |
| 867 | attr = append(attr, a) |
| 868 | } |
| 869 | sort.Sort(byName(attr)) |
| 870 | start.Attr = attr |
| 871 | t = start |
| 872 | } |
| 873 | err = e.EncodeToken(t) |
| 874 | if err != nil { |
| 875 | return err |
| 876 | } |
| 877 | } |
| 878 | return e.Flush() |
| 879 | } |
| 880 | |
| 881 | // equalXML tests for equality of the normalized XML contents of a and b. |
| 882 | func (n *xmlNormalizer) equalXML(a, b io.Reader) (bool, error) { |
no test coverage detected