public static List<Element> getTags(Document doc, String tagName) { NodeList list = doc.getElementsByTagName(tagName); List<Element> elements = Lists.newArrayListWithExpectedSize(list.getLength()); for (int i = 0; i < list.getLength(); i++) { elements.add((Element) list.item(i)); } // Add equivalent <script> elements String scriptType = SCRIPT_TYPE_TO_OSML_TAG.inverse().get(tagName); if (scriptType != null) { List<Element> scripts = DomUtil.getElementsByTagNameCaseInsensitive(doc, ImmutableSet.of("script")); for (Element script : scripts) { Attr typeAttr = (Attr)script.getAttributes().getNamedItem("type"); if (typeAttr != null && scriptType.equalsIgnoreCase(typeAttr.getValue())) { elements.add(script); } } } return elements; }
public static List<Element> getTags(Document doc, String tagName) { NodeList list = doc.getElementsByTagName(tagName); List<Element> elements = Lists.newArrayListWithExpectedSize(list.getLength()); for (int i = 0; i < list.getLength(); i++) { elements.add((Element) list.item(i)); } // Add equivalent <script> elements String scriptType = SCRIPT_TYPE_TO_OSML_TAG.inverse().get(tagName); if (scriptType != null) { List<Element> scripts = DomUtil.getElementsByTagNameCaseInsensitive(doc, ImmutableSet.of("script")); for (Element script : scripts) { Attr typeAttr = (Attr)script.getAttributes().getNamedItem("type"); if (typeAttr != null && scriptType.equalsIgnoreCase(typeAttr.getValue())) { elements.add(script); } } } return elements; }
public static List<Element> getTags(Document doc, String tagName) { NodeList list = doc.getElementsByTagName(tagName); List<Element> elements = Lists.newArrayListWithExpectedSize(list.getLength()); for (int i = 0; i < list.getLength(); i++) { elements.add((Element) list.item(i)); } // Add equivalent <script> elements String scriptType = SCRIPT_TYPE_TO_OSML_TAG.inverse().get(tagName); if (scriptType != null) { List<Element> scripts = DomUtil.getElementsByTagNameCaseInsensitive(doc, ImmutableSet.of("script")); for (Element script : scripts) { Attr typeAttr = (Attr)script.getAttributes().getNamedItem("type"); if (typeAttr != null && scriptType.equalsIgnoreCase(typeAttr.getValue())) { elements.add(script); } } } return elements; }
@Test public void getElementsByTagNameCaseInsensitive() { Document doc = root.getOwnerDocument(); List<Element> elements = DomUtil.getElementsByTagNameCaseInsensitive(doc, ImmutableSet.of("element")); assertEquals("zero", elements.get(0).getTextContent()); assertEquals("one", elements.get(1).getTextContent()); assertEquals("two", elements.get(2).getTextContent()); } }
@Test public void getElementsByTagNameCaseInsensitive() { Document doc = root.getOwnerDocument(); List<Element> elements = DomUtil.getElementsByTagNameCaseInsensitive(doc, ImmutableSet.of("element")); assertEquals("zero", elements.get(0).getTextContent()); assertEquals("one", elements.get(1).getTextContent()); assertEquals("two", elements.get(2).getTextContent()); } }
@Test public void getElementsByTagNameCaseInsensitive() throws Exception { Element root = XmlUtil.parse(XML); Document doc = root.getOwnerDocument(); List<Element> elements = DomUtil.getElementsByTagNameCaseInsensitive(doc, ImmutableSet.of("element")); assertEquals("zero", elements.get(0).getTextContent()); assertEquals("one", elements.get(1).getTextContent()); assertEquals("two", elements.get(2).getTextContent()); } }
@Test public void getElementsByTagNameCaseInsensitive() throws Exception { Element root = XmlUtil.parse(XML); Document doc = root.getOwnerDocument(); List<Element> elements = DomUtil.getElementsByTagNameCaseInsensitive(doc, ImmutableSet.of("element")); assertEquals("zero", elements.get(0).getTextContent()); assertEquals("one", elements.get(1).getTextContent()); assertEquals("two", elements.get(2).getTextContent()); } }
@Test public void getElementsByTagNameCaseInsensitive() throws Exception { Element root = XmlUtil.parse(XML); Document doc = root.getOwnerDocument(); List<Element> elements = DomUtil.getElementsByTagNameCaseInsensitive(doc, ImmutableSet.of("element")); assertEquals("zero", elements.get(0).getTextContent()); assertEquals("one", elements.get(1).getTextContent()); assertEquals("two", elements.get(2).getTextContent()); } }
@Test public void testConfigBindingFailure() throws Exception { Document document = parser.parseDom( "<script type='text/os-template'>" + "<osx:flash swf='http://www.example.org/test.swf' play='junk'>" + "Click Me" + "</osx:flash></script>"); Element tag = DomUtil.getElementsByTagNameCaseInsensitive(document, ImmutableSet.of("osx:flash")) .get(0); handler.process(result.getDocumentElement().getFirstChild().getNextSibling(), tag, processor); XPathWrapper wrapper = new XPathWrapper(result); assertTrue(wrapper.getValue("/html/body/span").startsWith("Failed to process os:Flash tag")); }
@Test public void testConfigBindingFailure() throws Exception { Document document = parser.parseDom( "<script type='text/os-template'>" + "<osx:flash swf='http://www.example.org/test.swf' play='junk'>" + "Click Me" + "</osx:flash></script>"); Element tag = DomUtil.getElementsByTagNameCaseInsensitive(document, ImmutableSet.of("osx:flash")) .get(0); handler.process(result.getDocumentElement().getFirstChild().getNextSibling(), tag, processor); XPathWrapper wrapper = new XPathWrapper(result); assertTrue(wrapper.getValue("/html/body/span").startsWith("Failed to process os:Flash tag")); }
@Test public void testConfigBindingFailure() throws Exception { Document document = parser.parseDom( "<script type='text/os-template'>" + "<osx:flash swf='http://www.example.org/test.swf' play='junk'>" + "Click Me" + "</osx:flash></script>"); Element tag = DomUtil.getElementsByTagNameCaseInsensitive(document, ImmutableSet.of("osx:flash")) .get(0); handler.process(result.getDocumentElement().getFirstChild().getNextSibling(), tag, processor); XPathWrapper wrapper = new XPathWrapper(result); assertTrue(wrapper.getValue("/html/body/span").startsWith("Failed to process os:Flash tag")); }
@Test public void testSanitizedRenderClickToPlay() throws Exception { Document document = parser.parseDom( "<script type='text/os-template'>" + "<osx:flash swf='http://www.example.org/test.swf' play='onclick'>" + "Click Me" + "</osx:flash></script>"); Element tag = DomUtil.getElementsByTagNameCaseInsensitive(document, ImmutableSet.of("osx:flash")) .get(0); expectSecurityToken(); EasyMock.expect(gadget.sanitizeOutput()).andReturn(true); expectFeatureLookup(); replay(); handler.process(result.getDocumentElement().getFirstChild().getNextSibling(), tag, processor); XPathWrapper wrapper = new XPathWrapper(result); assertEquals("swfobject()", wrapper.getValue("/html/head/script[1]")); assertEquals("os_xFlash_alt_1", wrapper.getValue("/html/body/div/@id")); assertEquals("Click Me", wrapper.getValue("/html/body/div")); assertEquals("os_xFlash_alt_1()", wrapper.getValue("/html/body/div/@onclick")); assertEquals(wrapper.getValue("/html/body/script[1]"), "function os_xFlash_alt_1(){ swfobject.embedSWF(\"http://www.example.org/test.swf\"," + "\"os_xFlash_alt_1\",\"100px\",\"100px\",\"9.0.115\",null,null," + "{\"swliveconnect\":false,\"flashvars\":\"st=12345\",\"allowscriptaccess\":\"never\",\"allownetworking\":\"internal\"},{}); }"); verify(); }
@Test public void testBasicRender() throws Exception { Document document = parser.parseDom( "<script type='text/os-template'>" + "<osx:Flash swf='http://www.example.org/test.swf'>" + "Click Me" + "</osx:Flash></script>"); Element tag = DomUtil.getElementsByTagNameCaseInsensitive(document, ImmutableSet.of("osx:flash")) .get(0); expectSecurityToken(); EasyMock.expect(gadget.sanitizeOutput()).andReturn(false); expectFeatureLookup(); replay(); handler.process(result.getDocumentElement().getFirstChild().getNextSibling(), tag, processor); XPathWrapper wrapper = new XPathWrapper(result); assertEquals("swfobject()", wrapper.getValue("/html/head/script[1]")); assertEquals("os_xFlash_alt_1", wrapper.getValue("/html/body/div/@id")); assertEquals("Click Me", wrapper.getValue("/html/body/div")); assertNull(wrapper.getNode("/html/body/div/@onclick")); assertEquals(wrapper.getValue("/html/body/script[1]"), "swfobject.embedSWF(\"http://www.example.org/test.swf\",\"os_xFlash_alt_1\",\"100px\"," + "\"100px\",\"9.0.115\",null,null,{\"flashvars\":\"st=12345\"},{});"); verify(); }
@Test public void testBasicRender() throws Exception { Document document = parser.parseDom( "<script type='text/os-template'>" + "<osx:Flash swf='http://www.example.org/test.swf'>" + "Click Me" + "</osx:Flash></script>"); Element tag = DomUtil.getElementsByTagNameCaseInsensitive(document, ImmutableSet.of("osx:flash")) .get(0); expectSecurityToken(); EasyMock.expect(gadget.sanitizeOutput()).andReturn(false); expectFeatureLookup(); replay(); handler.process(result.getDocumentElement().getFirstChild().getNextSibling(), tag, processor); XPathWrapper wrapper = new XPathWrapper(result); assertEquals("swfobject()", wrapper.getValue("/html/head/script[1]")); assertEquals("os_xFlash_alt_1", wrapper.getValue("/html/body/div/@id")); assertEquals("Click Me", wrapper.getValue("/html/body/div")); assertNull(wrapper.getNode("/html/body/div/@onclick")); assertEquals(wrapper.getValue("/html/body/script[1]"), "swfobject.embedSWF(\"http://www.example.org/test.swf\",\"os_xFlash_alt_1\",\"100px\"," + "\"100px\",\"9.0.115\",null,null,{\"flashvars\":\"st=12345\"},{});"); verify(); }
@Test public void testBasicRender() throws Exception { Document document = parser.parseDom( "<script type='text/os-template'>" + "<osx:Flash swf='http://www.example.org/test.swf'>" + "Click Me" + "</osx:Flash></script>"); Element tag = DomUtil.getElementsByTagNameCaseInsensitive(document, ImmutableSet.of("osx:flash")) .get(0); expectSecurityToken(); EasyMock.expect(gadget.sanitizeOutput()).andReturn(false); expectFeatureLookup(); replay(); handler.process(result.getDocumentElement().getFirstChild().getNextSibling(), tag, processor); XPathWrapper wrapper = new XPathWrapper(result); assertEquals("swfobject()", wrapper.getValue("/html/head/script[1]")); assertEquals("os_xFlash_alt_1", wrapper.getValue("/html/body/div/@id")); assertEquals("Click Me", wrapper.getValue("/html/body/div")); assertNull(wrapper.getNode("/html/body/div/@onclick")); assertEquals(wrapper.getValue("/html/body/script[1]"), "swfobject.embedSWF(\"http://www.example.org/test.swf\",\"os_xFlash_alt_1\",\"100px\"," + "\"100px\",\"9.0.115\",null,null,{\"flashvars\":\"st=12345\"},{});"); verify(); }
@Test public void testSanitizedRender() throws Exception { Document document = parser.parseDom( "<script type='text/os-template'>" + "<osx:Flash swf='http://www.example.org/test.swf'>" + "Click Me" + "</osx:Flash></script>"); Element tag = DomUtil.getElementsByTagNameCaseInsensitive(document, ImmutableSet.of("osx:flash")) .get(0); expectSecurityToken(); EasyMock.expect(gadget.sanitizeOutput()).andReturn(true); expectFeatureLookup(); replay(); handler.process(result.getDocumentElement().getFirstChild().getNextSibling(), tag, processor); XPathWrapper wrapper = new XPathWrapper(result); assertEquals("swfobject()", wrapper.getValue("/html/head/script[1]")); assertEquals("os_xFlash_alt_1", wrapper.getValue("/html/body/div/@id")); assertEquals("Click Me", wrapper.getValue("/html/body/div")); assertNull(wrapper.getNode("/html/body/div/@onclick")); assertEquals(wrapper.getValue("/html/body/script[1]"), "swfobject.embedSWF(\"http://www.example.org/test.swf\",\"os_xFlash_alt_1\",\"100px\"," + "\"100px\",\"9.0.115\",null,null,{\"swliveconnect\":false," + "\"flashvars\":\"st=12345\",\"allowscriptaccess\":\"never\",\"allownetworking\":\"internal\"},{});"); verify(); }
@Test public void testSanitizedRenderClickToPlay() throws Exception { Document document = parser.parseDom( "<script type='text/os-template'>" + "<osx:flash swf='http://www.example.org/test.swf' play='onclick'>" + "Click Me" + "</osx:flash></script>"); Element tag = DomUtil.getElementsByTagNameCaseInsensitive(document, ImmutableSet.of("osx:flash")) .get(0); expectSecurityToken(); EasyMock.expect(gadget.sanitizeOutput()).andReturn(true); expectFeatureLookup(); replay(); handler.process(result.getDocumentElement().getFirstChild().getNextSibling(), tag, processor); XPathWrapper wrapper = new XPathWrapper(result); assertEquals("swfobject()", wrapper.getValue("/html/head/script[1]")); assertEquals("os_xFlash_alt_1", wrapper.getValue("/html/body/div/@id")); assertEquals("Click Me", wrapper.getValue("/html/body/div")); assertEquals("os_xFlash_alt_1()", wrapper.getValue("/html/body/div/@onclick")); assertEquals(wrapper.getValue("/html/body/script[1]"), "function os_xFlash_alt_1(){ swfobject.embedSWF(\"http://www.example.org/test.swf\"," + "\"os_xFlash_alt_1\",\"100px\",\"100px\",\"9.0.115\",null,null," + "{\"swliveconnect\":false,\"flashvars\":\"st=12345\",\"allowscriptaccess\":\"never\",\"allownetworking\":\"internal\"},{}); }"); verify(); }
@Test public void testSanitizedRender() throws Exception { Document document = parser.parseDom( "<script type='text/os-template'>" + "<osx:Flash swf='http://www.example.org/test.swf'>" + "Click Me" + "</osx:Flash></script>"); Element tag = DomUtil.getElementsByTagNameCaseInsensitive(document, ImmutableSet.of("osx:flash")) .get(0); expectSecurityToken(); EasyMock.expect(gadget.sanitizeOutput()).andReturn(true); expectFeatureLookup(); replay(); handler.process(result.getDocumentElement().getFirstChild().getNextSibling(), tag, processor); XPathWrapper wrapper = new XPathWrapper(result); assertEquals("swfobject()", wrapper.getValue("/html/head/script[1]")); assertEquals("os_xFlash_alt_1", wrapper.getValue("/html/body/div/@id")); assertEquals("Click Me", wrapper.getValue("/html/body/div")); assertNull(wrapper.getNode("/html/body/div/@onclick")); assertEquals(wrapper.getValue("/html/body/script[1]"), "swfobject.embedSWF(\"http://www.example.org/test.swf\",\"os_xFlash_alt_1\",\"100px\"," + "\"100px\",\"9.0.115\",null,null,{\"swliveconnect\":false," + "\"flashvars\":\"st=12345\",\"allowscriptaccess\":\"never\",\"allownetworking\":\"internal\"},{});"); verify(); }
@Test public void testSanitizedRender() throws Exception { Document document = parser.parseDom( "<script type='text/os-template'>" + "<osx:Flash swf='http://www.example.org/test.swf'>" + "Click Me" + "</osx:Flash></script>"); Element tag = DomUtil.getElementsByTagNameCaseInsensitive(document, ImmutableSet.of("osx:flash")) .get(0); expectSecurityToken(); EasyMock.expect(gadget.sanitizeOutput()).andReturn(true); expectFeatureLookup(); replay(); handler.process(result.getDocumentElement().getFirstChild().getNextSibling(), tag, processor); XPathWrapper wrapper = new XPathWrapper(result); assertEquals("swfobject()", wrapper.getValue("/html/head/script[1]")); assertEquals("os_xFlash_alt_1", wrapper.getValue("/html/body/div/@id")); assertEquals("Click Me", wrapper.getValue("/html/body/div")); assertNull(wrapper.getNode("/html/body/div/@onclick")); assertEquals(wrapper.getValue("/html/body/script[1]"), "swfobject.embedSWF(\"http://www.example.org/test.swf\",\"os_xFlash_alt_1\",\"100px\"," + "\"100px\",\"9.0.115\",null,null,{\"swliveconnect\":false," + "\"flashvars\":\"st=12345\",\"allowscriptaccess\":\"never\",\"allownetworking\":\"internal\"},{});"); verify(); }
@Test public void testSanitizedRenderClickToPlay() throws Exception { Document document = parser.parseDom( "<script type='text/os-template'>" + "<osx:flash swf='http://www.example.org/test.swf' play='onclick'>" + "Click Me" + "</osx:flash></script>"); Element tag = DomUtil.getElementsByTagNameCaseInsensitive(document, ImmutableSet.of("osx:flash")) .get(0); expectSecurityToken(); EasyMock.expect(gadget.sanitizeOutput()).andReturn(true); expectFeatureLookup(); replay(); handler.process(result.getDocumentElement().getFirstChild().getNextSibling(), tag, processor); XPathWrapper wrapper = new XPathWrapper(result); assertEquals("swfobject()", wrapper.getValue("/html/head/script[1]")); assertEquals("os_xFlash_alt_1", wrapper.getValue("/html/body/div/@id")); assertEquals("Click Me", wrapper.getValue("/html/body/div")); assertEquals("os_xFlash_alt_1()", wrapper.getValue("/html/body/div/@onclick")); assertEquals(wrapper.getValue("/html/body/script[1]"), "function os_xFlash_alt_1(){ swfobject.embedSWF(\"http://www.example.org/test.swf\"," + "\"os_xFlash_alt_1\",\"100px\",\"100px\",\"9.0.115\",null,null," + "{\"swliveconnect\":false,\"flashvars\":\"st=12345\",\"allowscriptaccess\":\"never\",\"allownetworking\":\"internal\"},{}); }"); verify(); }