Skip to content

Commit

Permalink
Merge pull request #658 from BenjaminVega/add-target-text-function-co…
Browse files Browse the repository at this point in the history
…ntent-property-capabilities

Add feature: target-text() for content property capabilities
  • Loading branch information
danfickle authored Feb 22, 2021
2 parents 02719e8 + aa5bab0 commit 421a892
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public ContentFunctionFactory() {
_functions.add(new PageCounterFunction());
_functions.add(new PagesCounterFunction());
_functions.add(new TargetCounterFunction());
_functions.add(new TargetTextFunction());
_functions.add(new LeaderFunction());
_functions.add(new FsIfCutOffFunction());
}
Expand Down Expand Up @@ -232,6 +233,56 @@ public boolean canHandle(LayoutContext c, FSFunction function) {
}
}


private static class TargetTextFunction implements ContentFunction {
@Override
public boolean isStatic() {
return false;
}

@Override
public String calculate(RenderingContext c, FSFunction function, InlineText text) {
String uri = text.getParent().getElement().getAttribute("href");
if (uri != null && uri.startsWith("#")) {
String anchor = uri.substring(1);
Box target = c.getBoxById(anchor);
if (target != null) {
StringBuilder strBuilder = new StringBuilder();
target.collectText(c, strBuilder);
return strBuilder.toString();
}
}
return "";
}

@Override
public String calculate(LayoutContext c, FSFunction function) {
return null;
}

@Override
public String getLayoutReplacementText() {
return "ABCABC";
}

@Override
public boolean canHandle(LayoutContext c, FSFunction function) {
if (c.isPrint() && function.getName().equals("target-text")) {
List<PropertyValue> parameters = function.getParameters();
if(parameters.size() == 1) {
FSFunction f = parameters.get(0).getFunction();
if (f == null ||
f.getParameters().size() != 1 ||
! f.getParameters().get(0).getStringValue().equals("href")) {
return false;
}
return true;
}
}
return false;
}
}

/**
* Partially implements leaders as specified here:
* http://www.w3.org/TR/2007/WD-css3-gcpm-20070504/#leaders
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ private boolean isFunctionAllowed(FSFunction function) {
String name = function.getName();
return name.equals("attr") || name.equals("counter") || name.equals("counters") ||
name.equals("element") || name.startsWith("-fs") || name.equals("target-counter") ||
name.equals("leader");
name.equals("leader") || name.startsWith("target-text");
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
content: url(../../demos/images/flyingsaucer.png) "..." url(../../demos/images/flyingsaucer.png);
}
}
table {
line-height: 100%;
}
table tr {
font-size: small;
}
body {
margin: 10px;
max-width: 340px;
Expand All @@ -37,12 +43,28 @@
background-color: orange;
content: url(../../demos/images/flyingsaucer.png) "..." url(../../demos/images/flyingsaucer.png);
}
#four::after {
content: "Table: " target-text(attr(href)) leader(dotted) target-counter(attr(href), page);
}
#five::after {
content: target-text(attr(href)) leader(dotted) target-counter(attr(href), page);
}
</style>
</head>
<body>
<a id="zero" href="#three" style="display: inline-block;width: 100%;"></a>
<a id="four" href="#table" style="display: inline-block;width: 100%;"></a>
<a id="five" href="#title" style="display: inline-block;width: 100%;"></a>
<div id="one">ONE</div>
<div id="two" data-msg="HELLO"> WORLD</div>
<div id="three" style="page-break-before: always;width: 150%; background-color: gray;color: white;">With images: </div>
<h1 id="title" style="font-size: medium">Title 1</h1>
<table>
<caption id="table" style="font-size: x-small">Population by country</caption>
<thead><tr><th>Country</th><th>World share</th></tr></thead>
<tbody><tr><td>China</td><td>18.47%</td></tr></tbody>
<tbody><tr><td>India</td><td>17.70%</td></tr></tbody>
<tbody><tr><td>United States</td><td>4.25%</td></tr></tbody>
</table>
</body>
</html>

0 comments on commit 421a892

Please sign in to comment.