Comments (6)
Regarding <p> </p>
, we've tried several XPath selector variations, to no avail. Here is the general ignore rule setup we're testing:
$config = array(
'class' => 'IgnoreRule',
'selector' => $selector,
);
$rule = IgnoreRule::createFrom($config);
$transformer->addRule($rule);
where $selector
has been //*[text()=" "]
, //*[text()="\u0160"]
, //*[text()=codepoints-to-string((160))]
(which breaks), and other variations using normalize-space()
. Is anyone able to find a way to make this work, or would we need to introduce changes in the SDK to allow it?
from facebook-instant-articles-sdk-php.
Hey @m4olivei , thanks for digging into this!
This is cool that this solved your cases. I just want to warn you to be aware of this usage.
The Transformer Rules were not designed to have a double pass on same node. So if one specific node is selected by context and the selector, it wont leave this node for another rule.
This way, the empty rule will need a selector that selects only empty #text content.
Im working on a solution right now and will have a pull request soon.
This PR will be:
- All Elements will have a method isValid()
- toDOMElements will have an if (!$this->isValid()) -> Wont output itself.
- Transformer will check all isValid() from elements, if not valid, will add a warning to the transformation
This way we wont:
- Output invalid/empty elements
- Wont fail silently (because we will add warnings to the Transformer process)
from facebook-instant-articles-sdk-php.
OK, so I think I've worked out a nice custom Rule class that I'm happy with that works for this purpose:
<?php
/**
* @file
* Contains \EmptyRule
*/
use Facebook\InstantArticles\Transformer\Rules\ConfigurationSelectorRule;
/**
* Matches empty nodes given a selector. Here empty nodes are defined as nodes
* in a DOMDocument that have no children, or have only text children containing
* whitespace characters.
*/
class EmptyRule extends ConfigurationSelectorRule {
public function __construct() {
}
public static function create() {
return new EmptyRule();
}
public static function createFrom($configuration) {
return self::create()->withSelector($configuration['selector']);
}
public function getContextClass() {
return array(
InstantArticle::getClassName(),
Header::getClassName(),
Footer::getClassName(),
TextContainer::getClassName(),
);
}
public function matchesContext($context) {
return TRUE;
}
/**
* @param \DOMNode $node
* @return mixed
*/
public function matchesNode($node) {
// We're only interested in elements here, testing if they are empty.
if ($node->nodeType !== XML_ELEMENT_NODE) {
return FALSE;
}
// Limit by the selector passed in the configuration.
if (!parent::matchesNode($node)) {
return FALSE;
}
// Match iff the node has no children and/or all children are empty text
// nodes.
if ($node->hasChildNodes()) {
/* @var \DOMNode $child */
foreach ($node->childNodes as $child) {
if ($child->nodeName !== '#text') {
return FALSE;
}
else {
// @see https://stackoverflow.com/a/27990195/142145
$trimmed = trim($child->nodeValue, " \t\n\r\0\x0B\xC2\xA0");
if (!empty($trimmed)) {
return FALSE;
}
}
}
}
return TRUE;
}
public function apply($transformer, $context, $element) {
return $context;
}
}
Then assuming you've got a \Facebook\InstantArticles\Transformer\Transformer
instance called $transfomer
kicking around, add a config rule like so:
$transformer->addRule(
EmptyRule::createFrom(array(
'class' => 'EmptyRule',
'selector' => '//p|//div|//span',
))
);
The method EmptyRule::matchesNode
is the key. It'll match any element node, matched by the selector given, that either has no child nodes or has only text child nodes containing only whitespace. This reeeeeally annoying part is the need for this trim statement: trim($child->nodeValue, " \t\n\r\0\x0B\xC2\xA0")
, which will match the usual whitespace suspects, but also NBSP.
Working for me so far. If you guys are into this, I'd be up for re-jigging this into a proper pull request.
from facebook-instant-articles-sdk-php.
@everton-rosario yeah your right. It's only working for me b/c I have it as the last rule added to the Transformer, which means its the first rule that gets checked against all nodes.
Sounds like a neat solution, look forward to testing it. Thanks for looking into this as well!
from facebook-instant-articles-sdk-php.
Any update on this @everton-rosario.
from facebook-instant-articles-sdk-php.
Yes @Faiyaz.
Please follow and also help review this PR: #71
from facebook-instant-articles-sdk-php.
Related Issues (20)
- Failed Connection with graph.facebook.com HOT 2
- Handling of Ads Settings fields
- Support paragraphs inside Blockquote
- FAN on AMP? HOT 2
- Validate Analytics Configuration
- Installation problem on laravel 5.6 HOT 1
- RTL problem HOT 2
- Unable to use ConstantGetter with InteractiveRule HOT 4
- autoplay video in fullscreen HOT 1
- Transformer's transform method does not account for settings
- Problem in rule "multiple" - IA HOT 3
- Query params in related articles
- [BUG] : Problem with the Span and Anchor Rule
- Travis CI Php 5.5 and Php 5.4
- Fetch
- Integration in Symfony 5 is not possible because of css selector version in composer.json HOT 1
- FBInterstitialAd is gives all time "server error"
- support php 8 HOT 2
- Use InstantArticleInterface instead of InstantArticle as parameter type for Transformer methods
- How to create a scheduled facebook instant article through PHP SDK
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from facebook-instant-articles-sdk-php.