/*
 * Copyright © 2020 The GWT Project Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gwtproject.xml.client.impl;

import elemental2.dom.DomGlobal;
import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;
import org.gwtproject.xml.client.Document;
import org.gwtproject.xml.client.impl.DocumentImpl.NativeDocumentImpl;
import org.gwtproject.xml.client.impl.ElementImpl.NativeElementImpl;
import org.gwtproject.xml.client.impl.NodeImpl.NativeNodeImpl;
import org.gwtproject.xml.client.impl.NodeListImpl.NativeNodeListImpl;

/** Native implementation associated with {@link org.gwtproject.xml.client.XMLParser}. */
public abstract class XMLParserImpl {

  /**
   * This class implements the methods for standard browsers that use the DOMParser model of XML
   * parsing.
   */
  private static class XMLParserImplStandard extends XMLParserImpl {

    @JsType(isNative = true, name = "DOMParser", namespace = JsPackage.GLOBAL)
    static class DOMParser {
      native NativeDocumentImpl parseFromString(String contents, String mimeType);
    }

    protected final DOMParser domParser = new DOMParser();

    @Override
    protected NativeDocumentImpl createDocumentImpl() {
      return JsHelper.createDocumentImpl();
    }

    @Override
    protected NativeElementImpl getElementByIdImpl(NativeDocumentImpl document, String id) {
      return document.getElementById(id);
    }

    @Override
    protected NativeNodeListImpl getElementsByTagNameImpl(NativeNodeImpl o, String tagName) {
      return o.getElementsByTagNameNS("*", tagName);
    }

    @Override
    protected String getPrefixImpl(NativeNodeImpl node) {
      String fullName = node.nodeName;
      if (fullName != null && fullName.indexOf(":") != -1) {
        return fullName.split(":", 2)[0];
      }
      return null;
    }

    @Override
    protected NativeNodeImpl importNodeImpl(
        NativeDocumentImpl document, NativeNodeImpl importedNode, boolean deep) {
      return document.importNode(importedNode, deep);
    }

    @Override
    protected NativeDocumentImpl parseImpl(String contents) {
      NativeDocumentImpl result = domParser.parseFromString(contents, "text/xml");
      String userAgent = DomGlobal.navigator.userAgent;
      if (!userAgent.toLowerCase().contains("safari")) {
        NativeElementImpl rootTag = result.documentElement;
        if ("parsererror".equals(rootTag.tagName)
            && "http://www.mozilla.org/newlayout/xml/parsererror.xml"
                .equals(rootTag.namespaceURI)) {
          throw new RuntimeException(rootTag.firstChild.data);
        }
      } else {
        NativeNodeListImpl parseErrors = result.getElementsByTagName("parsererror");
        if (parseErrors.length > 0) {
          NativeNodeImpl error = parseErrors.item(0);
          if ("body".equals(error.parentNode.tagName)) {
            throw new RuntimeException(error.childNodes.item(1).innerHTML);
          }
        }
      }
      return result;
    }

    @Override
    protected String toStringImpl(ProcessingInstructionImpl node) {
      return toStringImpl((NodeImpl) node);
    }

    @Override
    protected String toStringImpl(NodeImpl node) {
      return new XMLSerializer().serializeToString(node.node);
    }

    @JsType(isNative = true, name = "XMLSerializer", namespace = JsPackage.GLOBAL)
    private static class XMLSerializer {
      native String serializeToString(NativeNodeImpl node);
    }
  }

  private static XMLParserImpl impl;

  public static XMLParserImpl getInstance() {
    if (impl == null) {
      impl = createImpl();
    }

    return impl;
  }

  private static XMLParserImpl createImpl() {
    return new XMLParserImplStandard();
  }

  static NativeElementImpl getElementById(NativeDocumentImpl document, String id) {
    return impl.getElementByIdImpl(document, id);
  }

  static NativeNodeListImpl getElementsByTagName(NativeNodeImpl o, String tagName) {
    return impl.getElementsByTagNameImpl(o, tagName);
  }

  static String getPrefix(NativeNodeImpl node) {
    return impl.getPrefixImpl(node);
  }

  static NativeNodeImpl importNode(
      NativeDocumentImpl document, NativeNodeImpl importedNode, boolean deep) {
    return impl.importNodeImpl(document, importedNode, deep);
  }

  /** Not globally instantable. */
  XMLParserImpl() {}

  public final Document createDocument() {
    return (Document) NodeImpl.build(createDocumentImpl());
  }

  public final Document parse(String contents) {
    try {
      return (Document) NodeImpl.build(parseImpl(contents));
    } catch (Exception e) {
      throw new DOMParseException(contents, e);
    }
  }

  protected abstract NativeDocumentImpl createDocumentImpl();

  protected abstract NativeElementImpl getElementByIdImpl(NativeDocumentImpl document, String id);

  protected abstract NativeNodeListImpl getElementsByTagNameImpl(
      NativeNodeImpl node, String tagName);

  protected abstract String getPrefixImpl(NativeNodeImpl node);

  protected abstract NativeNodeImpl importNodeImpl(
      NativeDocumentImpl document, NativeNodeImpl importedNode, boolean deep);

  protected abstract NativeDocumentImpl parseImpl(String contents);

  abstract String toStringImpl(ProcessingInstructionImpl node);

  abstract String toStringImpl(NodeImpl node);
}

@@ -647,41 +647,8 @@
 l;%0A%0A
-import elemental2.dom.DomGlobal;%0A
 impo
@@ -2676,123 +2676,8 @@
 %22);%0A
-      String userAgent = DomGlobal.navigator.userAgent;%0A      if (!userAgent.toLowerCase().contains(%22safari%22)) %7B%0A  
     
@@ -2730,18 +2730,16 @@
 lement;%0A
-  
       if
@@ -2788,18 +2788,16 @@
         
-  
 && %22http
@@ -2849,25 +2849,8 @@
 xml%22
-%0A                
 .equ
@@ -2874,26 +2874,24 @@
 paceURI)) %7B%0A
-  
         thro
@@ -2949,29 +2949,10 @@
     
-  
 %7D%0A
-      %7D else %7B%0A  
     
@@ -3032,26 +3032,24 @@
 or%22);%0A      
-  
 if (parseErr
@@ -3062,26 +3062,24 @@
 ngth %3E 0) %7B%0A
-  
         Nati
@@ -3122,26 +3122,24 @@
 0);%0A        
-  
 if (%22body%22.e
@@ -3169,26 +3169,24 @@
 tagName)) %7B%0A
-  
           th
@@ -3251,20 +3251,8 @@
 L);%0A
-          %7D%0A
     

remove useragent dependent code