kirby-deprecated-plugins / kirby-seo

51 stars 10 forks source link

Apostrophe in the page title breaks the edition in panel #46

Closed LeBenLeBen closed 7 years ago

LeBenLeBen commented 7 years ago

In case your page title includes an apostrophe, like:

L'Université

It triggers a JavaScript error:

Uncaught SyntaxError: Unexpected end of JSON input

while jQuery try to parse the JSON in the following function:

$.fn.controller = function() {
    var json = $('.seo').attr('data-seo-controller');
    controller = jQuery.parseJSON( json );
};

This is caused by apostrophes not being encoded in the JSON embedded in the DOM.

LeBenLeBen commented 7 years ago

It looks like the same problem occur if a text field include one or many ". Those should be escaped as well then.

luxlogica commented 7 years ago

@LeBenLeBen Indeed, you're correct.

Your fix #47 introduces this line:

<div class="seo" data-seo-controller='<?php echo json_encode( $controller, JSON_HEX_APOS ); ?>'>

In order to cater for a much wider range of use-cases, I would suggest using this - copied from the PHP Manual entry for json_encode():

<div class="seo" data-seo-controller='<?php echo json_encode( $controller, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE ); ?>'>

According to the PHP Manual, this should cover quotes, apostrophes, ampersands, tags, but leave unicode characters alone.

LeBenLeBen commented 7 years ago

I’ve updated the PR with all the flags. I’ve tested it on my installation and it works with both types of quotes now.

jenstornell commented 7 years ago

As you both @LeBenLeBen and @luxlogica have tried this successfully, I've now merged it.