<?php
/**
 * This file is part of the transcoding library. It contains the
 * definition of the {@link TranscodingActionDeleteEmbeds} class.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package TransPythia
 * @version $Revision: 1.1 $
 * @license http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html W3C Software Notice and License
 * @copyright Copyright (c) 2009, W3C (MIT, ERCIM, Keio)
 */


/**
 * Include the {@link TranscodingAction} base class definition.
 */
require_once(dirname(__FILE__) . '/transcodingaction.php');


/**
 * Transcoding action that removes embedded elements within the content that
 * are not allowed in XHTML Basic 1.1 when the requesting device is identified
 * as mobile: applet, embed and iframe are removed.
 * 
 * Removed elements are replaced by their alternative content when possible.
 * 
 * The transcoding action leaves regular object elements untouched.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package TransPythia
 * @version $Revision: 1.1 $
 * @license http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html W3C Software Notice and License
 * @copyright Copyright (c) 2009, W3C (MIT, ERCIM, Keio)
 */
class TranscodingActionDeleteEmbeds extends TranscodingAction {
	
	/**
	 * Removes embedded elements that are not supported by most mobile devices
	 * when the requesting device is identified as mobile.
	 * 
	 * @param string $content The HTML content to transcode.
	 * @param Evidence $evidence The evidence that identifies the requesting device.
	 * @return string The transcoded content.
	 * @exception SystemException The evidence is not valid.
	 */
	public function apply($content, $evidence){
		$this->initPropertyValues($evidence);
		
		$this->initproperty('mobile_device',
			TranscodingAction::$WURFL_MOBILE_DEVICE,
			TranscodingAction::$WURFL_VOCABULARY,
			TranscodingAction::$WURFL_DEFAULT_ASPECT);
			
		$property = $this->getOption('mobile_device');
		$is_mobile_device = $this->getPropertyValuePr($property);
		
		if(!isset($is_mobile_device) || !$is_mobile_device->getBoolean()){
		  	return $content;
		}
		
		// Replace embedded objects by their content when defined.
		// (note the regular expression would not work if an iframe
		// was defined within another iframe. Such constructs are forbidden
		// in HTML/XHTML.
		$adaptedContent = preg_replace(
			'/<(applet|embed|iframe)(\s[^>]*)?(?<!\/)>(.*)<\/(applet|embed|iframe)>/Usi',
			'<span>\3</span>',
			$content);
		
		// Suppress empty embedded objects
		$adaptedContent = preg_replace(
			'/<(applet|embed|iframe)(\s[^>]*)?\/?>/Usi',
			'',
			$adaptedContent);
		
		return $adaptedContent;
	}
}

?>