struct-dom-01-b-isvalid.html (7049B)
1 <!DOCTYPE html> 2 <html lang='en'> 3 <head> 4 <title>struct-dom-01-b-manual.svg</title> 5 <meta charset='utf-8'> 6 </head> 7 <body> 8 <h1>Source SVG: struct-dom-01-b-manual.svg</h1> 9 <svg version="1.1" baseProfile="basic" onload="domTest(evt)" id="svg-root" 10 width="100%" height="100%" viewBox="0 0 480 360" 11 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> 12 <!--======================================================================--> 13 <!--= SVG 1.1 2nd Edition Test Case =--> 14 <!--======================================================================--> 15 <!--= Copyright 2009 World Wide Web Consortium, (Massachusetts =--> 16 <!--= Institute of Technology, European Research Consortium for =--> 17 <!--= Informatics and Mathematics (ERCIM), Keio University). =--> 18 <!--= All Rights Reserved. =--> 19 <!--= See http://www.w3.org/Consortium/Legal/. =--> 20 <!--======================================================================--> 21 22 <title id="test-title">$RCSfile: struct-dom-01-b.svg,v $</title> 23 <defs> 24 <font-face font-family="SVGFreeSansASCII" unicode-range="U+0-7F"> 25 <font-face-src> 26 <font-face-uri xlink:href="../resources/SVGFreeSans.svg#ascii"/> 27 </font-face-src> 28 </font-face> 29 </defs> 30 <g id="test-body-content" font-family="SVGFreeSansASCII,sans-serif" font-size="18"> 31 <script type="text/ecmascript" xlink:href="../resources/testharness.js"></script> 32 <script type="text/ecmascript"><![CDATA[ 33 function domTest(evt) { 34 35 var svg_ns = "http://www.w3.org/2000/svg"; 36 37 // Get Document 38 var target = evt.target; 39 var doc = target.ownerDocument; 40 41 // 42 // Test that our rectangle is an SVGElement instance 43 // 44 var rect = doc.getElementById("rectId"); 45 var rootSVG = rect.ownerSVGElement; 46 var rootId = rootSVG.getAttribute( "id" ); 47 48 // Insert a new text element to the DOM tree using the id 49 var newText = doc.createElementNS(svg_ns, 'text'); 50 newText.setAttribute('x', '50'); 51 newText.setAttribute('y', '100'); 52 var message = "This document's root identifier is: " + rootId; 53 var textContent = doc.createTextNode(message); 54 newText.appendChild(textContent); 55 rect.parentNode.appendChild(newText); 56 57 // 58 // Now, check that our rectangle is an instance of SVGRect by accessing 59 // specific methods in order to get its x, y, width and height attributes. 60 // 61 var x = rect.x.baseVal.value; // SVGRect -> SVGAnimatedLenght -> SVGLength -> long 62 var y = rect.y.baseVal.value; 63 var width = rect.width.baseVal.value; 64 var height = rect.height.baseVal.value; 65 66 // 67 // Now, build a new SVGRect through the SVGSVGElement interface. 68 // 69 var newRect = doc.createElementNS(svg_ns, 'rect'); 70 71 // 72 // Set the x, y, width and height of this element 73 // 74 newRect.x.baseVal.value = x + 10; 75 newRect.y.baseVal.value = y + 10; 76 newRect.setAttribute("width", width); 77 newRect.setAttribute("height", height); 78 79 // 80 // Insert new element in DOM tree 81 // 82 rect.parentNode.insertBefore(newRect, rect); 83 84 // 85 // Check the pass criteria using the JS framework 86 // 87 test(function() {assert_equals(rootId, "svg-root")}, "Assert that the document's root identifier is 'svg-root'."); 88 test(function() { 89 assert_true(newRect instanceof SVGRectElement); 90 assert_equals(newRect.getAttribute('x'), String(x+10)); 91 assert_equals(newRect.getAttribute('y'), String(y+10)); 92 assert_equals(newRect.getAttribute('width'), String(width)); 93 assert_equals(newRect.getAttribute('height'), String(height)); 94 }, "Assert that 'newRect' is replica (ignoring fill) of 'rect' with different x and y."); 95 } 96 97 ]]></script> 98 <!--======================================================================--> 99 <!-- Since this test is examining the SVG DOM, it could use any language --> 100 <!-- binding. Here is the equivalent code for the Java binding --> 101 <!-- 102 103 // 104 // Test that our rectangle is an SVGElement instance 105 // 106 SVGRectElement rect = (SVGRectElement) doc.getElementById("rectId"); 107 SVGElement rootSVG = rect.getOwnerSVGElement(); 108 String rootId = rootSVG.getId(); 109 110 // Insert a new text element to the DOM tree using the id 111 Element newText = doc.createElement("text"); 112 newText.setAttribute("x", "50"); 113 newText.setAttribute("y", "100"); 114 String message = "This document's root identifier is=" " + rootId" 115 Text textContent = doc.createTextNode(message); 116 newText.appendChild(textContent); 117 rect.getParentNode().appendChild(newText); 118 119 // 120 // Now, check that our rectangle is an instance of SVGRect by accessing 121 // specific methods in order to get its x, y, width and height attributes. 122 // 123 float x = rect.getX().getBaseVal().getValue(); 124 float y = rect.getY().getBaseVal().getValue(); 125 float width = rect.getWidth().getBaseVal().getValue(); 126 float height = rect.getHeight().getBaseVal().getValue(); 127 128 // 129 // Now, build a new SVGRect through the SVGSVGElement interface. 130 // 131 SVGRectElement newRect = (SVGRectElement) doc.createElement("rect"); 132 133 // 134 // Set the x, y, width and height of this element 135 // 136 newRect.getX().getBaseVal().setValue(x + 10); 137 newRect.getY().getBaseVal().setValue(y + 10); 138 newRect.getWidth().getBaseVal().setValue(width); 139 newRect.getHeight().getBaseVal().setValue(height); 140 141 // 142 // Insert new element in DOM tree 143 // 144 alert(newRect) 145 rect.getParentNode().insertBefore(newRect, rect); 146 147 --> 148 <!-- ===================================================================== --> 149 <!-- The following rectangle's is accessed in the 'domTest' ECMA Script --> 150 <!-- handler. --> 151 <!-- ===================================================================== --> 152 <rect id="rectId" x="40" y="150" width="50" height="50" fill="green"/> 153 </g> 154 <g font-family="SVGFreeSansASCII,sans-serif" font-size="32"> 155 <text id="revision" x="10" y="340" stroke="none" fill="black">$Revision: 1.8 $</text> 156 </g> 157 <rect id="test-frame" x="1" y="1" width="478" height="358" fill="none" stroke="#000000"/> 158 <!-- comment out this watermark once the test is approved --> 159 <!--<g id="draft-watermark"> 160 <rect x="1" y="1" width="478" height="20" fill="red" stroke="black" stroke-width="1"/> 161 <text font-family="SVGFreeSansASCII,sans-serif" font-weight="bold" font-size="20" x="240" 162 text-anchor="middle" y="18" stroke-width="0.5" stroke="black" fill="white">DRAFT</text> 163 </g>--> 164 </svg> 165 </body> 166 </html>