-
Notifications
You must be signed in to change notification settings - Fork 114
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
Allow control characters inside JSON-LD strings #57
Conversation
Codecov Report
@@ Coverage Diff @@
## master #57 +/- ##
==========================================
+ Coverage 85.14% 85.24% +0.09%
==========================================
Files 7 7
Lines 303 305 +2
Branches 53 53
==========================================
+ Hits 258 260 +2
Misses 43 43
Partials 2 2
Continue to review full report at Codecov.
|
extruct/jsonld.py
Outdated
@@ -15,6 +15,12 @@ | |||
class JsonLdExtractor(object): | |||
_xp_jsonld = lxml.etree.XPath('descendant-or-self::script[@type="application/ld+json"]') | |||
|
|||
def __init__(self, allow_control_characters=True): | |||
if allow_control_characters: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could even set self.strict = not allow_control_characters
to simplify this.
@@ -15,6 +15,12 @@ | |||
class JsonLdExtractor(object): | |||
_xp_jsonld = lxml.etree.XPath('descendant-or-self::script[@type="application/ld+json"]') | |||
|
|||
def __init__(self, allow_control_characters=True): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, makes sense to me to have it not-strict by default.
|
||
jsonlde = JsonLdExtractor() | ||
data = jsonlde.extract(body) | ||
self.assertEqual(data, expected) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably also want to test with strict=True
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @vu3jej
Thanks for the review, @redapple 🙇♂️ I've added the changes as per your suggestions. Please let me know if anything is amiss. |
Sorry, I'll have to close this PR as the issue was ultimately fixed in #85, sorry for letting this PR slip through. |
As of now, control characters are not allowed inside strings, causing errors such as this one:
This commit adds an option
allow_control_characters
toJsonLdExtractor
toset the
strict
parameter insidejson.loads
. default is true(strict=false),based on @kmike's suggestion.
Thanks!