summaryrefslogtreecommitdiff
path: root/src/Readability.php
diff options
context:
space:
mode:
authorAndres Rey <[email protected]>2017-02-21 18:40:42 +0000
committerAndres Rey <[email protected]>2017-02-21 18:40:42 +0000
commit0f99c53420f05187dc3af9fe04f1129e14dfe96e (patch)
tree17e9d6ea450c972243374eee52317e4e086e90e0 /src/Readability.php
parent268d1130247042c9f3952a143f43ed74522cd64c (diff)
Fixed test cases and added function to replace font tags with span + param to setNodeTag to keep attributes from original node.
Diffstat (limited to 'src/Readability.php')
-rw-r--r--src/Readability.php20
1 files changed, 14 insertions, 6 deletions
diff --git a/src/Readability.php b/src/Readability.php
index 027858f..44633b2 100644
--- a/src/Readability.php
+++ b/src/Readability.php
@@ -253,7 +253,7 @@ class Readability extends Element implements ReadabilityInterface
{
// Check if the setAttribute method exists, as some elements lack of it (and calling it anyway throws an exception)
if (method_exists($this->node, 'setAttribute')) {
- $this->contentScore = (float) $score;
+ $this->contentScore = (float)$score;
// Set score in an attribute of the tag to prevent losing it while creating new Readability objects.
$this->node->setAttribute('data-readability', $this->contentScore);
@@ -286,8 +286,9 @@ class Readability extends Element implements ReadabilityInterface
* element with the new tag name and importing it to the main DOMDocument.
*
* @param string $value
+ * @param bool $importAttributes
*/
- public function setNodeTag($value)
+ public function setNodeTag($value, $importAttributes = false)
{
$new = new \DOMDocument();
$new->appendChild($new->createElement($value));
@@ -298,6 +299,13 @@ class Readability extends Element implements ReadabilityInterface
$new->firstChild->appendChild($import);
}
+ if ($importAttributes) {
+ // Import attributes from the original node.
+ foreach ($this->node->attributes as $attribute) {
+ $new->firstChild->setAttribute($attribute->nodeName, $attribute->nodeValue);
+ }
+ }
+
// The import must be done on the firstChild of $new, since $new is a DOMDocument and not a DOMElement.
$import = $this->node->ownerDocument->importNode($new->firstChild, true);
$this->node->parentNode->replaceChild($import, $this->node);
@@ -335,7 +343,7 @@ class Readability extends Element implements ReadabilityInterface
* for parents.
*
* @param Readability $originalNode
- * @param bool $ignoreSelfAndKids
+ * @param bool $ignoreSelfAndKids
*
* @return Readability
*/
@@ -411,7 +419,7 @@ class Readability extends Element implements ReadabilityInterface
* Creates a new node based on the text content of the original node.
*
* @param Readability $originalNode
- * @param string $tagName
+ * @param string $tagName
*
* @return Readability
*/
@@ -458,8 +466,8 @@ class Readability extends Element implements ReadabilityInterface
* provided one.
*
* @param Readability $node
- * @param string $tagName
- * @param int $maxDepth
+ * @param string $tagName
+ * @param int $maxDepth
*
* @return bool
*/