From a0f80593e9dea304371147e8f86359213ed11478 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Fri, 9 Nov 2018 07:57:29 -0800 Subject: [PATCH] move xpath query to parent helper class --- lib/XRay/Formats/Format.php | 31 +++++++++++++++++++++++++++++++ lib/XRay/Formats/HTML.php | 28 +--------------------------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/lib/XRay/Formats/Format.php b/lib/XRay/Formats/Format.php index 41ae28a..6cd7c5c 100644 --- a/lib/XRay/Formats/Format.php +++ b/lib/XRay/Formats/Format.php @@ -104,5 +104,36 @@ abstract class Format implements iFormat { return trim(str_replace(['
','
'],"\n", $sanitized)); } + protected static function findLinksInDocument(&$xpath, $target) { + $found = []; + self::xPathFindNodeWithAttribute($xpath, 'a', 'href', function($u) use($target, &$found){ + if($u == $target) { + $found[$u] = null; + } + }); + self::xPathFindNodeWithAttribute($xpath, 'img', 'src', function($u) use($target, &$found){ + if($u == $target) { + $found[$u] = null; + } + }); + self::xPathFindNodeWithAttribute($xpath, 'video', 'src', function($u) use($target, &$found){ + if($u == $target) { + $found[$u] = null; + } + }); + self::xPathFindNodeWithAttribute($xpath, 'audio', 'src', function($u) use($target, &$found){ + if($u == $target) { + $found[$u] = null; + } + }); + return $found; + } + + public static function xPathFindNodeWithAttribute($xpath, $node, $attr, $callback) { + foreach($xpath->query('//'.$node.'[@'.$attr.']') as $el) { + $v = $el->getAttribute($attr); + $callback($v); + } + } } diff --git a/lib/XRay/Formats/HTML.php b/lib/XRay/Formats/HTML.php index 3f83893..9f9528b 100644 --- a/lib/XRay/Formats/HTML.php +++ b/lib/XRay/Formats/HTML.php @@ -47,26 +47,7 @@ class HTML extends Format { $found = []; if($target) { - self::xPathFindNodeWithAttribute($xpath, 'a', 'href', function($u) use($target, &$found){ - if($u == $target) { - $found[$u] = null; - } - }); - self::xPathFindNodeWithAttribute($xpath, 'img', 'src', function($u) use($target, &$found){ - if($u == $target) { - $found[$u] = null; - } - }); - self::xPathFindNodeWithAttribute($xpath, 'video', 'src', function($u) use($target, &$found){ - if($u == $target) { - $found[$u] = null; - } - }); - self::xPathFindNodeWithAttribute($xpath, 'audio', 'src', function($u) use($target, &$found){ - if($u == $target) { - $found[$u] = null; - } - }); + $found = self::findLinksInDocument($xpath, $target); } if(!$found) { @@ -178,13 +159,6 @@ class HTML extends Format { return mb_convert_encoding($input, 'HTML-ENTITIES', mb_detect_encoding($input)); } - private static function xPathFindNodeWithAttribute($xpath, $node, $attr, $callback) { - foreach($xpath->query('//'.$node.'[@'.$attr.']') as $el) { - $v = $el->getAttribute($attr); - $callback($v); - } - } - private static function xPathGetElementById($xpath, $id) { $element = null; foreach($xpath->query("//*[@id='$id']") as $el) {