Skip to content

Commit 421a892

Browse files
authored
Merge pull request #658 from BenjaminVega/add-target-text-function-content-property-capabilities
Add feature: target-text() for content property capabilities
2 parents 02719e8 + aa5bab0 commit 421a892

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-1
lines changed

openhtmltopdf-core/src/main/java/com/openhtmltopdf/context/ContentFunctionFactory.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public ContentFunctionFactory() {
4444
_functions.add(new PageCounterFunction());
4545
_functions.add(new PagesCounterFunction());
4646
_functions.add(new TargetCounterFunction());
47+
_functions.add(new TargetTextFunction());
4748
_functions.add(new LeaderFunction());
4849
_functions.add(new FsIfCutOffFunction());
4950
}
@@ -232,6 +233,56 @@ public boolean canHandle(LayoutContext c, FSFunction function) {
232233
}
233234
}
234235

236+
237+
private static class TargetTextFunction implements ContentFunction {
238+
@Override
239+
public boolean isStatic() {
240+
return false;
241+
}
242+
243+
@Override
244+
public String calculate(RenderingContext c, FSFunction function, InlineText text) {
245+
String uri = text.getParent().getElement().getAttribute("href");
246+
if (uri != null && uri.startsWith("#")) {
247+
String anchor = uri.substring(1);
248+
Box target = c.getBoxById(anchor);
249+
if (target != null) {
250+
StringBuilder strBuilder = new StringBuilder();
251+
target.collectText(c, strBuilder);
252+
return strBuilder.toString();
253+
}
254+
}
255+
return "";
256+
}
257+
258+
@Override
259+
public String calculate(LayoutContext c, FSFunction function) {
260+
return null;
261+
}
262+
263+
@Override
264+
public String getLayoutReplacementText() {
265+
return "ABCABC";
266+
}
267+
268+
@Override
269+
public boolean canHandle(LayoutContext c, FSFunction function) {
270+
if (c.isPrint() && function.getName().equals("target-text")) {
271+
List<PropertyValue> parameters = function.getParameters();
272+
if(parameters.size() == 1) {
273+
FSFunction f = parameters.get(0).getFunction();
274+
if (f == null ||
275+
f.getParameters().size() != 1 ||
276+
! f.getParameters().get(0).getStringValue().equals("href")) {
277+
return false;
278+
}
279+
return true;
280+
}
281+
}
282+
return false;
283+
}
284+
}
285+
235286
/**
236287
* Partially implements leaders as specified here:
237288
* http://www.w3.org/TR/2007/WD-css3-gcpm-20070504/#leaders

openhtmltopdf-core/src/main/java/com/openhtmltopdf/css/parser/property/ContentPropertyBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,6 @@ private boolean isFunctionAllowed(FSFunction function) {
9999
String name = function.getName();
100100
return name.equals("attr") || name.equals("counter") || name.equals("counters") ||
101101
name.equals("element") || name.startsWith("-fs") || name.equals("target-counter") ||
102-
name.equals("leader");
102+
name.equals("leader") || name.startsWith("target-text");
103103
}
104104
}

openhtmltopdf-examples/src/main/resources/visualtest/html/content-property-capabilities.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
content: url(../../demos/images/flyingsaucer.png) "..." url(../../demos/images/flyingsaucer.png);
1818
}
1919
}
20+
table {
21+
line-height: 100%;
22+
}
23+
table tr {
24+
font-size: small;
25+
}
2026
body {
2127
margin: 10px;
2228
max-width: 340px;
@@ -37,12 +43,28 @@
3743
background-color: orange;
3844
content: url(../../demos/images/flyingsaucer.png) "..." url(../../demos/images/flyingsaucer.png);
3945
}
46+
#four::after {
47+
content: "Table: " target-text(attr(href)) leader(dotted) target-counter(attr(href), page);
48+
}
49+
#five::after {
50+
content: target-text(attr(href)) leader(dotted) target-counter(attr(href), page);
51+
}
4052
</style>
4153
</head>
4254
<body>
4355
<a id="zero" href="#three" style="display: inline-block;width: 100%;"></a>
56+
<a id="four" href="#table" style="display: inline-block;width: 100%;"></a>
57+
<a id="five" href="#title" style="display: inline-block;width: 100%;"></a>
4458
<div id="one">ONE</div>
4559
<div id="two" data-msg="HELLO"> WORLD</div>
4660
<div id="three" style="page-break-before: always;width: 150%; background-color: gray;color: white;">With images: </div>
61+
<h1 id="title" style="font-size: medium">Title 1</h1>
62+
<table>
63+
<caption id="table" style="font-size: x-small">Population by country</caption>
64+
<thead><tr><th>Country</th><th>World share</th></tr></thead>
65+
<tbody><tr><td>China</td><td>18.47%</td></tr></tbody>
66+
<tbody><tr><td>India</td><td>17.70%</td></tr></tbody>
67+
<tbody><tr><td>United States</td><td>4.25%</td></tr></tbody>
68+
</table>
4769
</body>
4870
</html>

0 commit comments

Comments
 (0)