2
0
Эх сурвалжийг харах

php : fixed issues with XML parsing

Franco Ponticelli 14 жил өмнө
parent
commit
baa9f56e08

+ 1 - 0
doc/CHANGES.txt

@@ -50,6 +50,7 @@
 	all : added named local functions (allow self-recursion)
 	all : use left-assoc for (==,!=,>,>=,<,<=)(==,!=,>,>=,<,<=) (&&)(&&) and (||)(||)
 	all : give prefix unary operators higher priority than ?:
+	php : fixed XML parsing
 
 2010-08-14: 2.06
 	neko : change serializer to be able to handle instances of basic classes from other modules

+ 23 - 21
std/php/_std/Xml.hx

@@ -61,11 +61,6 @@ enum XmlType {
 	}
 
 	private static function __character_data_handler(parser : Dynamic, data : String) : Void {
-		// TODO: this function can probably be simplified
-//		var lc : Xml = (build._children == null || build._children.length == 0) ? null : build._children[build._children.length-1];
-//		if(lc != null && Xml.PCData == lc.nodeType) {
-//			lc.nodeValue = lc.nodeValue + untyped __call__("htmlentities", data);
-//		} else
 		if((untyped __call__("strlen", data) == 1 && __call__("htmlentities", data) != data) || untyped __call__("htmlentities", data) == data) {
 			build.addChild(createPCData(untyped __call__("htmlentities", data)));
 		} else
@@ -73,10 +68,13 @@ enum XmlType {
 	}
 
 	private static function __default_handler(parser : Dynamic, data : String) : Void {
-		build.addChild(createPCData(data));
+		if ("<!--" == data.substr(0, 4))
+			build.addChild(createComment(data.substr(4, data.length-7)));
+		else
+			build.addChild(createPCData(data));
 	}
 
-	static var xmlChecker = new EReg("\\s*(<\\?xml|<!DOCTYPE)", "mi");
+	static var reHeader = ~/\s*(?:<\?(.+?)\?>)?(?:<!DOCTYPE ([^>]+)>)?/mi;
 
 	public static function parse( str : String ) : Xml {
 		build = createDocument();
@@ -87,10 +85,9 @@ enum XmlType {
 		untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_CASE_FOLDING"), 0);
 		untyped __call__("xml_parser_set_option", xml_parser, __php__("XML_OPTION_SKIP_WHITE"), 0);
 
-		var isComplete = xmlChecker.match(str);
-
-		if(!isComplete)
-			str = "<doc>"+str+"</doc>";
+		reHeader.match(str);
+		
+		str = "<doc>"+reHeader.matchedRight()+"</doc>";
 
 		if(1 != untyped __call__("xml_parse", xml_parser, str, true)) {
 			throw "Xml parse error ("+untyped __call__("xml_error_string", __call__("xml_get_error_code", xml_parser)) + ") line #" + __call__("xml_get_current_line_number", xml_parser);
@@ -98,15 +95,20 @@ enum XmlType {
 
 		untyped __call__("xml_parser_free", xml_parser);
 
-		if(isComplete) {
-			return build;
-		} else {
-			build = build._children[0];
-			build._parent = null;
-			build._nodeName = null;
-			build.nodeType = Document;
-			return build;
-		}
+		build = build._children[0];
+		build._parent = null;
+		build._nodeName = null;
+		build.nodeType = Document;
+		
+		var doctype = reHeader.matched(2);
+		if (null != doctype)
+			build.insertChild(createDocType(doctype), 0);
+			
+		var prolog = reHeader.matched(1);
+		if (null != prolog)
+			build.insertChild(createProlog(prolog), 0);
+
+		return build;
 	}
 
 	private function new() : Void;
@@ -378,7 +380,7 @@ enum XmlType {
 			return "<!--"+_nodeValue+"-->";
 		else if( nodeType == Xml.DocType )
 			return "<!DOCTYPE "+_nodeValue+">";
-		else if( nodeType == Xml.Prolog )
+		else if ( nodeType == Xml.Prolog )
 			return "<?"+_nodeValue+"?>";
 		
 

+ 2 - 2
tests/unit/TestXML.hx

@@ -76,8 +76,8 @@ class TestXML extends Test {
 
 		var x = Xml.parse(header + doctype + comment + xml);
 
-		#if (flash || php)
-		// doctype is well parsed but is not present in the parsed Xml (php, f8 and f9)
+		#if flash
+		// doctype is well parsed but is not present in the parsed Xml (f8 and f9)
 		doctype = '';
 		#end