/* * Copyright (C)2005-2017 Haxe Foundation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ package php; import php.Lib; enum XmlType { } typedef NativeXml = Xml; class Xml { public static var Element(default,null) : XmlType; public static var PCData(default,null) : XmlType; public static var CData(default,null) : XmlType; public static var Comment(default,null) : XmlType; public static var DocType(default,null) : XmlType; public static var ProcessingInstruction(default,null) : XmlType; public static var Document(default,null) : XmlType; public var nodeType(default,null) : XmlType; public var nodeName(get,set) : String; public var nodeValue(get,set) : String; public var parent(get,null) : Xml; var _nodeName : String; var _nodeValue : String; var _attributes : haxe.ds.StringMap; var _children : Array; var _parent : Xml; var _fromCustomParser:Bool; private static var build : Xml; private static function __start_element_handler(parser : Dynamic, name : String, attribs : ArrayAccess) : Void { var node = createElement(name); untyped __php__("foreach($attribs as $k => $v) $node->set($k, $v)"); build.addChild(node); build = node; } private static function __end_element_handler(parser : Dynamic, name : String) : Void { build = build.parent; } private static function __decodeattr(value : String) : String { return untyped __call__("str_replace", "'", ''', __call__("htmlspecialchars", value, __php__('ENT_COMPAT'), 'UTF-8')); } private static function __decodeent(value : String) : String { return untyped __call__("str_replace", "'", ''', __call__("htmlentities", value, __php__('ENT_COMPAT'), 'UTF-8')); } private static function __character_data_handler(parser : Dynamic, data : String) : Void { var d = __decodeent(data); if ((untyped __call__("strlen", data) == 1 && d != data) || d == data) { var last = build._children[build._children.length - 1]; if (null != last && last.nodeType == Xml.PCData) { last.nodeValue += d; } else build.addChild(createPCData(d)); } else { build.addChild(createCData(data)); } } private static function __default_handler(parser : Dynamic, data : String) : Void { //On some PHP setups (seems to happen when libexpat is used) we may get called for such "entities" although character_data will correctly be called afterward. if(data == "") return; if (""; else if( nodeType == Xml.DocType ) return ""; else if ( nodeType == Xml.ProcessingInstruction ) return ""; for( x in iterator() ) s += x.toString(); if( nodeType == Xml.Element ) { s += ""; } return s; } static function __init__() : Void untyped { Xml.Element = "element"; Xml.PCData = "pcdata"; Xml.CData = "cdata"; Xml.Comment = "comment"; Xml.DocType = "doctype"; Xml.ProcessingInstruction = "processingInstruction"; Xml.Document = "document"; } }