Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit a0d065a

Browse files
authored
Merge pull request #315 from facebook/fix/dateTextContent-support
Added support for new dateTextContent attr in GlobalRule
2 parents 3743979 + e2197da commit a0d065a

File tree

6 files changed

+303
-4
lines changed

6 files changed

+303
-4
lines changed

src/Facebook/InstantArticles/Transformer/Getters/DateGetter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ public function get($node)
5151
$element = $elements->item(0);
5252

5353
if ($this->attribute) {
54-
return \DateTime::createFromFormat($this->format, $element->getAttribute($this->attribute));
54+
return \DateTime::createFromFormat('!'.$this->format, $element->getAttribute($this->attribute));
5555
}
56-
return \DateTime::createFromFormat($this->format, $element->textContent);
56+
return \DateTime::createFromFormat('!'.$this->format, trim($element->textContent));
5757
}
5858
return null;
5959
}

tests/Facebook/InstantArticles/Transformer/GlobalHtml/GlobalTransformerTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,31 @@ public function testSelfTransformerContent()
3939
$this->assertEqualsHtml($expected, $result);
4040
$this->assertCount(0, $transformer->getWarnings());
4141
}
42+
43+
public function testSelfTransformerContentWithTestDate()
44+
{
45+
date_default_timezone_set('UTC');
46+
47+
$json_file = file_get_contents(__DIR__ . '/global-rules-with-text-date.json');
48+
49+
$instant_article = InstantArticle::create();
50+
$transformer = new Transformer();
51+
$transformer->loadRules($json_file);
52+
53+
$html_file = file_get_contents(__DIR__ . '/global-with-text-date.html');
54+
55+
libxml_use_internal_errors(true);
56+
$document = new \DOMDocument();
57+
$document->loadHTML($html_file);
58+
libxml_use_internal_errors(false);
59+
60+
$transformer->transform($instant_article, $document);
61+
$instant_article->addMetaProperty('op:generator:version', '1.0.0');
62+
$instant_article->addMetaProperty('op:generator:transformer:version', '1.0.0');
63+
$result = $instant_article->render('', true)."\n";
64+
$expected = file_get_contents(__DIR__ . '/global-ia-with-text-date.html');
65+
66+
$this->assertEqualsHtml($expected, $result);
67+
$this->assertCount(0, $transformer->getWarnings());
68+
}
4269
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<html>
2+
<head>
3+
<link rel="canonical" href="http://domain.com/article_full_example.html"/>
4+
<meta charset="utf-8"/>
5+
<meta property="op:generator" content="facebook-instant-articles-sdk-php"/>
6+
<meta property="op:generator:version" content="1.0.0"/>
7+
<meta property="op:generator:transformer" content="facebook-instant-articles-sdk-php"/>
8+
<meta property="op:generator:transformer:version" content="1.0.0"/>
9+
<meta property="op:markup_version" content="v1.0"/>
10+
</head>
11+
<body>
12+
<article>
13+
<header>
14+
<figure>
15+
<img src="http://domain.com/header-image-body.png"/>
16+
</figure>
17+
<h1>The <b>article</b> title</h1>
18+
<time class="op-published" datetime="2018-02-05T00:00:00+00:00">February 5th, 12:00am</time>
19+
<address><a href="http://facebook.com/author" rel="facebook" title="Journalist">Author Name</a>Some author description</address>
20+
</header>
21+
<p>Lorem <b>ipsum</b> dolor sit amet.</p>
22+
<figure>
23+
<img src="http://domain.com/image-body.png"/>
24+
<figcaption>The caption for image.</figcaption>
25+
</figure>
26+
</article>
27+
</body>
28+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
{
2+
"rules":[
3+
{
4+
"class":"TextNodeRule"
5+
},
6+
{
7+
"class":"IgnoreRule",
8+
"selector":"div.author"
9+
},
10+
{
11+
"class":"GlobalRule",
12+
"selector":"html",
13+
"properties":{
14+
"article.title":{
15+
"type":"element",
16+
"selector":"div.header h1"
17+
},
18+
"author.url":{
19+
"type":"string",
20+
"selector":"div.author a",
21+
"attribute":"href"
22+
},
23+
"author.name":{
24+
"type":"string",
25+
"selector":"div.author a"
26+
},
27+
"author.role_contribution" : {
28+
"type" : "string",
29+
"selector" : "div.author a",
30+
"attribute": "title"
31+
},
32+
"author.description" : {
33+
"type" : "string",
34+
"selector" : "div.author span"
35+
},
36+
"article.canonical": {
37+
"type" : "string",
38+
"selector": "head meta",
39+
"attribute": "content"
40+
},
41+
"article.publish": {
42+
"selector": "div.date",
43+
"type": "date",
44+
"format": "M j, Y"
45+
},
46+
"article.body":{
47+
"selector":"article",
48+
"type":"element"
49+
},
50+
"image.url":{
51+
"selector":"div.hero-image img",
52+
"type":"string",
53+
"attribute":"src"
54+
}
55+
}
56+
},
57+
{
58+
"class":"PassThroughRule",
59+
"selector":"head, script, body"
60+
},
61+
{
62+
"class":"ItalicRule",
63+
"selector":"i"
64+
},
65+
{
66+
"class":"BoldRule",
67+
"selector":"b"
68+
},
69+
{
70+
"class":"ParagraphRule",
71+
"selector":"p"
72+
},
73+
{
74+
"class":"HeaderTitleRule",
75+
"selector":"h1"
76+
},
77+
{
78+
"class":"HeaderSubTitleRule",
79+
"selector":"h2"
80+
},
81+
{
82+
"class":"HeaderRule",
83+
"selector":"div.header"
84+
},
85+
{
86+
"class":"AuthorRule",
87+
"selector":"span.author",
88+
"properties":{
89+
"author.name":{
90+
"type":"string",
91+
"selector":"span"
92+
}
93+
}
94+
},
95+
{
96+
"class":"CaptionRule",
97+
"selector":"div.image-caption"
98+
},
99+
{
100+
"class":"CaptionRule",
101+
"selector":"span.caption",
102+
"properties":{
103+
"caption.default":{
104+
"type":"string",
105+
"selector":"img",
106+
"attribute":"alt"
107+
}
108+
}
109+
},
110+
{
111+
"class":"ImageRule",
112+
"selector":"div.image",
113+
"properties":{
114+
"image.url":{
115+
"type":"string",
116+
"selector":"img",
117+
"attribute":"src"
118+
}
119+
}
120+
},
121+
{
122+
"class":"ImageRule",
123+
"selector":"div.prefix",
124+
"properties":{
125+
"image.url":{
126+
"type":"string",
127+
"selector":"img",
128+
"attribute":"src",
129+
"prefix":"http:"
130+
},
131+
"image.caption":{
132+
"type":"element",
133+
"selector":"div.image-caption"
134+
}
135+
}
136+
},
137+
{
138+
"class":"ImageRule",
139+
"selector":"div.suffix",
140+
"properties":{
141+
"image.url":{
142+
"type":"string",
143+
"selector":"img",
144+
"attribute":"src",
145+
"suffix":"?suffix=yes"
146+
},
147+
"image.caption":{
148+
"type":"element",
149+
"selector":"div.image-caption"
150+
}
151+
}
152+
},
153+
{
154+
"class":"ImageRule",
155+
"selector":"div.prefixsuffix",
156+
"properties":{
157+
"image.url":{
158+
"type":"string",
159+
"selector":"img",
160+
"attribute":"src",
161+
"prefix":"http:",
162+
"suffix":"?suffix=yes"
163+
},
164+
"image.caption":{
165+
"type":"element",
166+
"selector":"div.image-caption"
167+
}
168+
}
169+
},
170+
{
171+
"class":"IgnoreRule",
172+
"selector":"div.hero-image"
173+
},
174+
{
175+
"class":"SocialEmbedRule",
176+
"selector":"div.embed",
177+
"properties":{
178+
"socialembed.iframe":{
179+
"type":"children",
180+
"selector":"*"
181+
}
182+
}
183+
},
184+
{
185+
"class":"FooterRule",
186+
"selector":"footer"
187+
},
188+
{
189+
"class":"RelatedArticlesRule",
190+
"selector":"ul.op-related-articles",
191+
"properties":{
192+
"related.title":{
193+
"type":"exists",
194+
"selector":"ul.op-related-articles",
195+
"attribute":"title"
196+
}
197+
}
198+
},
199+
{
200+
"class":"FooterRelatedArticlesRule",
201+
"selector":"ul.op-related-articles"
202+
},
203+
{
204+
"class":"RelatedItemRule",
205+
"selector":"li",
206+
"properties":{
207+
"related.sponsored":{
208+
"type":"exists",
209+
"selector":"li",
210+
"attribute":"data-sponsored"
211+
},
212+
"related.url":{
213+
"type":"string",
214+
"selector":"a",
215+
"attribute":"href"
216+
}
217+
}
218+
}
219+
]
220+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<html>
2+
<head>
3+
<script type="text/javascript" href="http://domain.com/javascript.js" />
4+
<meta property="og:url" content="http://domain.com/article_full_example.html" />
5+
</head>
6+
<body>
7+
<div class="header">
8+
<h1>The <b>article</b> title</h1>
9+
<div class="date">Feb 5, 2018</div>
10+
</div>
11+
<article>
12+
<p>Lorem <b>ipsum</b> dolor sit amet.</p>
13+
<div class="image">
14+
<img src="http://domain.com/image-body.png"/>
15+
<span class="caption">The caption for image.</span>
16+
</div>
17+
<div class="hero-image">
18+
<img src="http://domain.com/header-image-body.png"/>
19+
</div>
20+
<div class="author">
21+
By: <a href="http://facebook.com/author" title="Journalist">Author Name</a>
22+
<span>Some author description</span>
23+
</div>
24+
</article>
25+
</body>
26+
</html>

tests/Facebook/InstantArticles/Transformer/TransformerTest.php

-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ public function testSelfTransformerContent()
9292
$instant_article->addMetaProperty('op:generator:transformer:version', '1.0.0');
9393
$result = $instant_article->render('', true)."\n";
9494

95-
//var_dump($result);
96-
// print_r($warnings);
9795
$this->assertEqualsHtml($html_file, $result);
9896
}
9997

0 commit comments

Comments
 (0)