Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Last active April 27, 2017 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save branflake2267/d16365012e27f514685eb5c365dcb315 to your computer and use it in GitHub Desktop.
Save branflake2267/d16365012e27f514685eb5c365dcb315 to your computer and use it in GitHub Desktop.
GWT reproducing safari 10.1+ getElementsByTagName issue where name tag has colon. Workaround below.
<?xml version="1.0" ?>
<message>
<header>
<xf:label name="Brandon"/>
</header>
<body>the body</body>
</message>
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.TextResource;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.XMLParser;
public class SafariXmlIssue implements EntryPoint {
public interface MyResources extends ClientBundle {
public static final MyResources INSTANCE = GWT.create(MyResources.class);
@Source("message.xml")
public TextResource message();
}
@Override
public void onModuleLoad() {
test();
}
private void test() {
String xml = MyResources.INSTANCE.message().getText();
Document doc = XMLParser.parse(xml);
Node foundNode = doc.getElementsByTagName("label").item(0);
GWT.log("node=" + foundNode);
}
}
/*
// ~~~~~ workaround for safari xml issue
// ~~~~~ Add this to your source folder with the same package name so it clobbers the default implementation.
// ~~~~~ workaround noted with // ~~~~ workaround
*/
/*
* Copyright 2008 Google Inc.
*
* 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 com.google.gwt.xml.client.impl;
import com.google.gwt.core.client.JavaScriptObject;
/**
* This class is Safari implementation of the XMLParser interface.
*/
public class XMLParserImplSafari extends XMLParserImplStandard {
private static boolean safari2LevelWebKit = (getWebKitVersion() <= 420);
public static boolean isSafari2LevelWebKit() {
return safari2LevelWebKit;
}
private static native int getWebKitVersion() /*-{
var result = / AppleWebKit\/([\d]+)/.exec(navigator.userAgent);
return ((result) ? parseInt(result[1]) : 0) || 0;
}-*/;
private static void throwDOMParseException(String message) {
throw new DOMParseException(message);
}
@Override
protected native JavaScriptObject getElementsByTagNameImpl(JavaScriptObject o,
String tagName) /*-{
//return o.getElementsByTagName(tagName);
return o.getElementsByTagNameNS("*",tagName); // ~~~~ workaround
}-*/;
@Override
protected native JavaScriptObject importNodeImpl(JavaScriptObject jsObject,
JavaScriptObject importedNode, boolean deep) /*-{
// Works around a Safari2 issue where importing a node will steal attributes
// from the original.
if (@com.google.gwt.xml.client.impl.XMLParserImplSafari::isSafari2LevelWebKit()()) {
importedNode = importedNode.cloneNode(deep);
}
return jsObject.importNode(importedNode, deep);
}-*/;
/**
* <html><body><parsererror style="white-space: pre; border: 2px solid #c77;
* padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black" >
* <h3>This page contains the following errors:</h3>
* <div style="font-family:monospace;font-size:12px" >error on line 1 at
* column 2: xmlParseStartTag: invalid element name </div>
* <h3>Below is a rendering of the page up to the first error.</h3>
* </parsererror></body></html> is all you get from Safari. Hope that nobody
* wants to send one of those error reports over the wire to be parsed by
* safari...
*
* @param contents contents
* @return parsed JavaScript object
* @see com.google.gwt.xml.client.impl.XMLParserImpl#parseImpl(java.lang.String)
*/
@Override
protected native JavaScriptObject parseImpl(String contents) /*-{
var domParser =
this.@com.google.gwt.xml.client.impl.XMLParserImplStandard::domParser;
var result = domParser.parseFromString(contents,"text/xml");
var parseerrors = result.getElementsByTagName("parsererror");
if (parseerrors.length > 0) {
var err = parseerrors.item(0);
if (err.parentNode.tagName == 'body') {
@com.google.gwt.xml.client.impl.XMLParserImplSafari::throwDOMParseException(Ljava/lang/String;)(err.childNodes[1].innerHTML);
}
}
return result;
}-*/;
}
@niloc132
Copy link

Is that a valid XML document? shouldn't the xf namespace be defined in order to be used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment