Skip to content

Improved: Fetch articles with selector but do not delete the class attribute. (Simplepie: new method: rename_attribute)#4175

Merged
Alkarex merged 11 commits intoFreshRSS:edgefrom
math-GH:simplepie-rename-attribute
Feb 5, 2022
Merged

Improved: Fetch articles with selector but do not delete the class attribute. (Simplepie: new method: rename_attribute)#4175
Alkarex merged 11 commits intoFreshRSS:edgefrom
math-GH:simplepie-rename-attribute

Conversation

@math-GH
Copy link
Copy Markdown
Contributor

@math-GH math-GH commented Jan 26, 2022

My User Story:

As a reader I want to configure the content of fetched articles, so that I can hide parts that I do not need.

Changes proposed in this pull request:

User side:

  • the id and class attributes will not deleted anymore. It will renamed to data-sanitized-id / data-sanitized-class
  • with the extension CustomCSS, it is possible to style the data-sanitized-class tags (f.e. div[data-feed="XX"] div[data-sanitized-class*="CLASSNAME"]

Here an example, that the fetched article footer could be re-styled with CSS (here with background-color:red)
grafik

technical side:

  • added the method rename_attr()
  • default of $rename_attributes is an empty array
  • for FreshRSS $simplePie->rename_attributes('class'); is set, so that the class attribute is renamed to data-sanitized-class

How to test the feature manually:

  1. fetch articles with a selector
  2. see the HTML
  3. see the data-sanitized-class
  4. do some CSS magic to style it

Pull request checklist:

  • clear commit messages
  • code manually tested
  • if you like it, I would do a PR to the SimplePie repository

@Alkarex
Copy link
Copy Markdown
Member

Alkarex commented Jan 26, 2022

Looks good at a first glance 👍🏻
You should indeed also try to send a PR upstream https://github.com/simplepie/simplepie

Copy link
Copy Markdown
Member

@Frenzie Frenzie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

Comment on lines +151 to +166
if ($tags)
{
if (is_array($tags))
{
$this->rename_attributes = $tags;
}
else
{
$this->rename_attributes = explode(',', $tags);
}
}
else
{
$this->rename_attributes = false;
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could remove a layer by doing:

if (!$tags) {
    $this->rename_attributes = false;
} elseif (is_array($tags)) {
    $this->rename_attributes = $tags;
} else {
    $this->rename_attributes = explode(',', $tags);
}

I find that it makes the code more readable.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also change the order like:

if (is_array($tags)) {
    $this->rename_attributes = $tags;
} elseif (is_string($tags)) {
    $this->rename_attributes = explode(',', $tags);
} else {
    $this->rename_attributes = false;
}

This will reject everything that is not supported. I don't know if this something we want though. In this case if you have a number, it will not be treated as a string.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes sense to reject anything. If it's available as data-whatever you can use it in CSS or JS; if it's not you can't.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first example is not rejecting anything. It's a rewrite without the layers. The second example is just that, an example :)
You're right about not rejecting things at this level.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is copy&paste from strip_htmltags() below (lines 168ff).
I would say that it does not make sense to have another structure.

@math-GH
Copy link
Copy Markdown
Contributor Author

math-GH commented Jan 28, 2022

I changed this PR to draft. Have some discussions in the Simplepie repro. Let's wait for the results there. Had already to change something (some copy&paste faults....)

@math-GH math-GH marked this pull request as ready for review February 5, 2022 13:00
@math-GH
Copy link
Copy Markdown
Contributor Author

math-GH commented Feb 5, 2022

New function is merged in Simplepie upstream.

Ready for review here


protected function rename_attr($attrib, $xpath)
{
$elements = $xpath->query('//*[@' . $attrib . ']');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit late for my feedback, but some kind of sanitising of $attrib could have been good. Not there, but in the public function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please help me to understand. What kind of sanitizing do you mean?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If $attrib happens to be something like ] or any other special character, it will make the XPath crash or worse

@Alkarex Alkarex merged commit cb36fe2 into FreshRSS:edge Feb 5, 2022
@math-GH math-GH deleted the simplepie-rename-attribute branch February 5, 2022 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants