Skip to content

Json ld in html #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions common/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,6 @@ function unComment(doc, content) {
return content
.replace(/<!--/, '')
.replace(/-->/, '')
.replace(/< !--/g, '<!--')
.replace(/-- >/g, '-->');
.replace(/< !\s*-\s*-/g, '<!--')
.replace(/-\s*- >/g, '-->');
}
28 changes: 20 additions & 8 deletions common/extract-examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,18 @@ def save_example(examples:, element:, title:, example_number:, error:, warn:)
$stdout.write "F".colorize(:red)
next
end

# Get base from document, if present
html_base = doc.at_xpath('/html/head/base/@href')
ex[:base] = html_base.to_s if html_base

script_content = doc.at_xpath(xpath)
if script_content
# Remove (faked) XML comments and unescape sequences
content = script_content
.inner_html
.sub(/^\s*< !--/, '')
.sub(/-- >\s*$/, '')
.sub(/^\s*< !\s*-\s*-/, '')
.sub(/-\s*- >\s*$/, '')
.gsub(/&lt;/, '<')
end

Expand Down Expand Up @@ -438,7 +443,15 @@ def save_example(examples:, element:, title:, example_number:, error:, warn:)
# Set argument to referenced content to be parsed
args[0] = if examples[ex[:result_for]][:ext] == 'html' && method == :expand
# If we are expanding, and the reference is HTML, find the first script element.
doc = Nokogiri::HTML.parse(examples[ex[:result_for]][:content])
doc = Nokogiri::HTML.parse(
examples[ex[:result_for]][:content]
.sub(/^\s*< !\s*-\s*-/, '')
.sub(/-\s*- >\s*$/, ''))

# Get base from document, if present
html_base = doc.at_xpath('/html/head/base/@href')
options[:base] = html_base.to_s if html_base

script_content = doc.at_xpath(xpath)
unless script_content
errors << "Example #{ex[:number]} at line #{ex[:line]} references example #{ex[:result_for].inspect} with no JSON-LD script element"
Expand All @@ -447,12 +460,13 @@ def save_example(examples:, element:, title:, example_number:, error:, warn:)
end
StringIO.new(script_content
.inner_html
.sub(/^\s*< !--/, '')
.sub(/-- >\s*$/, '')
.gsub(/&lt;/, '<'))
elsif examples[ex[:result_for]][:ext] == 'html' && ex[:target]
# Only use the targeted script
doc = Nokogiri::HTML.parse(examples[ex[:result_for]][:content])
doc = Nokogiri::HTML.parse(
examples[ex[:result_for]][:content]
.sub(/^\s*< !\s*-\s*-/, '')
.sub(/-\s*- >\s*$/, ''))
script_content = doc.at_xpath(xpath)
unless script_content
errors << "Example #{ex[:number]} at line #{ex[:line]} references example #{ex[:result_for].inspect} with no JSON-LD script element"
Expand All @@ -461,8 +475,6 @@ def save_example(examples:, element:, title:, example_number:, error:, warn:)
end
StringIO.new(script_content
.to_html
.sub(/^\s*< !--/, '')
.sub(/-- >\s*$/, '')
.gsub(/&lt;/, '<'))
else
StringIO.new(examples[ex[:result_for]][:content])
Expand Down
677 changes: 402 additions & 275 deletions index.html

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions tests/compact-manifest.jsonld
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,42 @@
"input": "compact/ep15-in.jsonld",
"context": "compact/ep15-context.jsonld",
"expect": "invalid container mapping"
}, {
"@id": "#th001",
"@type": ["jld:PositiveEvaluationTest", "jld:CompactTest"],
"name": "Compacts embedded JSON-LD script element",
"purpose": "Tests embedded JSON-LD in HTML",
"input": "compact/h001-in.html",
"context": "compact/h001-context.jsonld",
"expect": "compact/h001-out.jsonld",
"option": {"specVersion": "json-ld-1.1"}
}, {
"@id": "#th002",
"@type": ["jld:PositiveEvaluationTest", "jld:CompactTest"],
"name": "Compacts first embedded JSON-LD script element",
"purpose": "Tests embedded JSON-LD in HTML",
"input": "compact/h002-in.html",
"context": "compact/h002-context.jsonld",
"expect": "compact/h002-out.jsonld",
"option": {"specVersion": "json-ld-1.1"}
}, {
"@id": "#th003",
"@type": ["jld:PositiveEvaluationTest", "jld:CompactTest"],
"name": "Compacts targeted JSON-LD script element",
"purpose": "Tests embedded JSON-LD in HTML with fragment identifier",
"input": "compact/h003-in.html#second",
"context": "compact/h003-context.jsonld",
"expect": "compact/h003-out.jsonld",
"option": {"specVersion": "json-ld-1.1"}
}, {
"@id": "#th004",
"@type": ["jld:PositiveEvaluationTest", "jld:CompactTest"],
"name": "Compacts all embedded JSON-LD script elements with extractAllScripts option",
"purpose": "Tests embedded JSON-LD in HTML extracting all elements",
"input": "compact/h004-in.html",
"context": "compact/h004-context.jsonld",
"expect": "compact/h004-out.jsonld",
"option": {"specVersion": "json-ld-1.1", "extractAllScripts": true}
}, {
"@id": "#tm001",
"@type": ["jld:PositiveEvaluationTest", "jld:CompactTest"],
Expand Down
5 changes: 5 additions & 0 deletions tests/compact/h001-context.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"@context": {
"foo": {"@id": "http://example.com/foo", "@container": "@list"}
}
}
12 changes: 12 additions & 0 deletions tests/compact/h001-in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<head>
<script type="application/ld+json">
{
"@context": {
"foo": {"@id": "http://example.com/foo", "@container": "@list"}
},
"foo": [{"@value": "bar"}]
}
</script>
</head>
</html>
6 changes: 6 additions & 0 deletions tests/compact/h001-out.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"@context": {
"foo": {"@id": "http://example.com/foo", "@container": "@list"}
},
"foo": ["bar"]
}
5 changes: 5 additions & 0 deletions tests/compact/h002-context.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"@context": {
"foo": {"@id": "http://example.com/foo", "@container": "@list"}
}
}
21 changes: 21 additions & 0 deletions tests/compact/h002-in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html>
<head>
<script type="application/ld+json">
{
"@context": {
"foo": {"@id": "http://example.com/foo", "@container": "@list"}
},
"foo": [{"@value": "bar"}]
}
</script>
<script type="application/ld+json">
{
"@context": {"ex": "http://example.com/"},
"@graph": [
{"ex:foo": {"@value": "foo"}},
{"ex:bar": {"@value": "bar"}}
]
}
</script>
</head>
</html>
6 changes: 6 additions & 0 deletions tests/compact/h002-out.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"@context": {
"foo": {"@id": "http://example.com/foo", "@container": "@list"}
},
"foo": ["bar"]
}
3 changes: 3 additions & 0 deletions tests/compact/h003-context.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"@context": {"ex": "http://example.com/"}
}
21 changes: 21 additions & 0 deletions tests/compact/h003-in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html>
<head>
<script id="first" type="application/ld+json">
{
"@context": {
"foo": {"@id": "http://example.com/foo", "@container": "@list"}
},
"foo": [{"@value": "bar"}]
}
</script>
<script id="second" type="application/ld+json">
{
"@context": {"ex": "http://example.com/"},
"@graph": [
{"ex:foo": {"@value": "foo"}},
{"ex:bar": {"@value": "bar"}}
]
}
</script>
</head>
</html>
7 changes: 7 additions & 0 deletions tests/compact/h003-out.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"@context": {"ex": "http://example.com/"},
"@graph": [
{"ex:foo": "foo"},
{"ex:bar": "bar"}
]
}
6 changes: 6 additions & 0 deletions tests/compact/h004-context.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"@context": {
"ex": "http://example.com/",
"foo": {"@id": "http://example.com/foo", "@container": "@list"}
}
}
21 changes: 21 additions & 0 deletions tests/compact/h004-in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<html>
<head>
<script type="application/ld+json">
{
"@context": {
"foo": {"@id": "http://example.com/foo", "@container": "@list"}
},
"foo": [{"@value": "bar"}]
}
</script>
<script type="application/ld+json">
{
"@context": {"ex": "http://example.com/"},
"@graph": [
{"ex:foo": {"@value": "foo"}},
{"ex:bar": {"@value": "bar"}}
]
}
</script>
</head>
</html>
15 changes: 15 additions & 0 deletions tests/compact/h004-out.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"@context": {
"ex": "http://example.com/",
"foo": {"@id": "http://example.com/foo", "@container": "@list"}
},
"@graph": [
{"foo": ["bar"]},
{
"@graph": [
{"ex:foo": "foo"},
{"ex:bar": "bar"}
]
}
]
}
Loading