WordPress/wp-admin/js/press-this.min.js
Adam Silverstein 499e2a0ff9 Move sanitizeText and stripTags from press this to wp.sanitize.
Introduce the `wp.sanitize` namespace and add two helpers for text sanitization. `stripTags` strips HTML tags from a string using regex.

Fixes #40635.

Built from https://develop.svn.wordpress.org/trunk@41061


git-svn-id: http://core.svn.wordpress.org/trunk@40911 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2017-07-15 15:48:46 +00:00

1 line
11 KiB
JavaScript

!function(a,b){var c=function(){function c(a){return a&&b.pressThisL10n?b.pressThisL10n[a]||a:a||""}function d(b){return b=a.trim(b||""),/^(?:https?:)?\/\//.test(b)?(b=wp.sanitize.stripTags(b),b.replace(/["\\]+/g,"")):""}function e(){a(".spinner").addClass("is-active"),a(".post-actions button").attr("disabled","disabled")}function f(){a(".spinner").removeClass("is-active"),a(".post-actions button").removeAttr("disabled")}function g(a){var c,d;G&&!G.isHidden()||(a="reset"===a||Z&&Z>X.value.length,d=X.style.height,a?(c=b.pageYOffset,X.style.height="auto",X.style.height=Math.max(X.scrollHeight,Y)+"px",b.scrollTo(b.pageXOffset,c)):parseInt(X.style.height,10)<X.scrollHeight&&(X.style.height=X.scrollHeight+"px"),Z=X.value.length)}function h(){if(!G)return!1;var a,b,c,d=G.selection.getNode();if(G.wp&&G.wp.getView&&(b=G.wp.getView(d)))c=b.getBoundingClientRect();else{a=G.selection.getRng();try{c=a.getClientRects()[0]}catch(e){}c||(c=d.getBoundingClientRect())}return!!c.height&&c}function i(a){var c=b.tinymce.util.VK,d=a.keyCode;d<=47&&d!==c.SPACEBAR&&d!==c.ENTER&&d!==c.DELETE&&d!==c.BACKSPACE&&d!==c.UP&&d!==c.LEFT&&d!==c.DOWN&&d!==c.UP||d>=91&&d<=93||d>=112&&d<=123||144===d||145===d||j(d)}function j(a){var c,d,e,f=h(),g=50,i=65,j=b.tinymce.util.VK;f&&(c=f.top+G.iframeElement.getBoundingClientRect().top,d=c+f.height,c-=g,d+=i,e=J.height(),e<f.height||(c<0&&(a===j.UP||a===j.LEFT||a===j.BACKSPACE)?b.scrollTo(b.pageXOffset,c+b.pageYOffset):d>e&&b.scrollTo(b.pageXOffset,d+b.pageYOffset-e)))}function k(){var b=a("#title-container");return b.find("img.emoji").each(function(){var b=a(this);b.replaceWith(a("<span>").text(b.attr("alt")))}),wp.sanitize.sanitizeText(b.text())}function l(){var c=a("#pressthis-form"),d=a('<input type="hidden" name="post_category[]" value="">');G&&G.save(),a("#post_title").val(k()),b.tagBox&&a("div.tagsdiv").each(function(){b.tagBox.flushTags(this,!1,1)}),a(".categories-select .category").each(function(b,e){var f=a(e);f.hasClass("selected")&&c.append(d.clone().val(f.attr("data-term-id")||""))})}function m(d){var g;L=!1,e(),"publish"===d&&a("#post_status").val("publish"),l(),g=a("#pressthis-form").serialize(),a.ajax({type:"post",url:b.ajaxurl,data:g}).always(function(){f(),s(),a(".publish-button").removeClass("is-saving")}).done(function(a){if(a.success){if(a.data.redirect)if(b.opener&&(N.redirInParent||a.data.force))try{b.opener.location.href=a.data.redirect,b.setTimeout(function(){b.self.close()},200)}catch(c){b.location.href=a.data.redirect}else b.location.href=a.data.redirect}else r(a.data.errorMessage)}).fail(function(){r(c("serverError"))})}function n(a){var c,e,f="";c=d(a.attr("data-wp-src")||""),e=d(O.u),a.hasClass("is-image")?(e||(e=c),f='<a href="'+e+'"><img class="alignnone size-full" src="'+c+'" alt="" /></a>'):f="[embed]"+c+"[/embed]",G&&!G.isHidden()?Q?G.execCommand("mceInsertContent",!1,f):G.setContent("<p>"+f+"</p>"+G.getContent()):b.QTags&&b.QTags.insertContent(f)}function o(){var c,d=a("#new-category").val();d&&(c={action:"press-this-add-category",post_id:a("#post_ID").val()||0,name:d,new_cat_nonce:a("#_ajax_nonce-add-category").val()||"",parent:a("#new-category-parent").val()||0},a.post(b.ajaxurl,c,function(b){if(b.success){var c,d,e=a("ul.categories-select");a.each(b.data,function(b,f){var g=a("<li>").append(a('<div class="category selected" tabindex="0" role="checkbox" aria-checked="true">').attr("data-term-id",f.term_id).text(f.name));f.parent?(d&&d.length||(c=e.find('div[data-term-id="'+f.parent+'"]').parent(),d=c.find("ul.children:first"),d.length||(d=a('<ul class="children">').appendTo(c))),d.prepend(g)):e.prepend(g),g.focus()}),F()}else r(b.data.errorMessage)}))}function p(){O.hasData&&a("#scanbar").hide()}function q(b,c){var d=a(".editor-wrapper div.alerts"),e=c?"is-error":"is-notice";d.append(a('<p class="alert '+e+'">').text(b))}function r(a){q(a,!0)}function s(){a("div.alerts").empty()}function t(){O.errors&&a.each(O.errors,function(a,b){r(b)})}function u(a,b,d){var e=I.clone().addClass("is-image");e.attr("data-wp-src",a).css("background-image","url("+b+")").find("span").text(c("suggestedImgAlt").replace("%d",d+1)),H.append(e)}function v(){var b=0;H=a("ul.media-list"),I=a('<li class="suggested-media-thumbnail" tabindex="0"><span class="screen-reader-text"></span></li>'),O._embeds&&a.each(O._embeds,function(a,e){var f="",g="",h=I.clone().addClass("is-embed");e=d(e),e.indexOf("youtube.com/")>-1?(f="https://i.ytimg.com/vi/"+e.replace(/.+v=([^&]+).*/,"$1")+"/hqdefault.jpg",g+=" is-video"):e.indexOf("youtu.be/")>-1?(f="https://i.ytimg.com/vi/"+e.replace(/\/([^\/])$/,"$1")+"/hqdefault.jpg",g+=" is-video"):e.indexOf("dailymotion.com")>-1?(f=e.replace("/video/","/thumbnail/video/"),g+=" is-video"):g+=e.indexOf("soundcloud.com")>-1?" is-audio":e.indexOf("twitter.com")>-1?" is-tweet":" is-video",h.attr("data-wp-src",e).find("span").text(c("suggestedEmbedAlt").replace("%d",a+1)),f&&h.css("background-image","url("+f+")"),H.append(h),b++}),O._images&&a.each(O._images,function(a,c){var e,f=new Image;c=d(c),e=c.replace(/^(http[^\?]+)(\?.*)?$/,"$1"),e=c.indexOf("files.wordpress.com/")>-1?e.replace(/\?.*$/,"")+"?w="+P:c.indexOf("gravatar.com/")>-1?e.replace(/\?.*$/,"")+"?s="+P:c,f.onload=function(){f.width&&f.width<256||f.height&&f.height<128||u(c,e,a)},f.src=c,b++}),b&&a(".media-list-container").addClass("has-media")}function w(){var b=a(".post-options"),c=a(".post-option"),d=a(".setting-modal"),e=a(".modal-close");c.on("click",function(){var c=a(this).index(),e=d.eq(c);b.addClass(S).one($,function(){a(this).addClass(T)}),e.removeClass(U).one($,function(){a(this).find(".modal-close").focus()})}),e.on("click",function(){var d=a(this).parent(),e=d.index();b.removeClass(U),d.addClass(S),$?d.one($,function(){a(this).addClass(T),c.eq(e-1).focus()}):setTimeout(function(){d.addClass(T),c.eq(e-1).focus()},350)})}function x(){M=!0,a(".options").removeClass("closed").addClass("open"),a(".press-this-actions, #scanbar").addClass(T),a(".options-panel-back").removeClass(T),a(".options-panel").removeClass(U).one($,function(){a(".post-option:first").focus()})}function y(){M=!1,a(".options").removeClass("open").addClass("closed"),a(".options-panel-back").addClass(T),a(".press-this-actions, #scanbar").removeClass(T),a(".options-panel").addClass(S).one($,function(){a(this).addClass(T),a(".post-options").removeClass(U),a(".setting-modal").addClass(U)})}function z(){var c=a("#title-container"),d=a(".post-title-placeholder");c.on("focus",function(){d.addClass("is-hidden")}).on("blur",function(){c.text()||c.html()||d.removeClass("is-hidden")}).on("keyup",function(){L=!0}).on("paste",function(d){var e,f,g=d.originalEvent.clipboardData||b.clipboardData;if(g){try{e=g.getData("Text")||g.getData("text/plain"),e&&(e=a.trim(e.replace(/\s+/g," ")),b.getSelection?(f=b.getSelection().getRangeAt(0),f&&(f.collapsed||f.deleteContents(),f.insertNode(document.createTextNode(e)))):document.selection&&(f=document.selection.createRange(),f&&(f.text=e)))}catch(h){}d.preventDefault()}L=!0,setTimeout(function(){c.text(k())},50)}),(c.text()||c.html())&&d.addClass("is-hidden")}function A(a){a.hasClass("selected")?a.removeClass("selected").attr("aria-checked","false"):a.addClass("selected").attr("aria-checked","true")}function B(){a(".categories-select").on("click.press-this keydown.press-this",function(b){var c=a(b.target);if(c.is("div.category")){if("keydown"===b.type&&32!==b.keyCode)return;A(c),b.preventDefault()}})}function C(){a(".split-button").removeClass("is-open"),a(".split-button-toggle").attr("aria-expanded","false")}function D(){p(),v(),t(),b.tagBox&&b.tagBox.init(),V&&a(document.body).css("cursor","pointer")}function E(){var d=a(".split-button");return K.on("tinymce-editor-init",function(a,b){G=b,G.on("nodechange",function(){Q=!0}),G.on("focus",function(){C()}),G.on("show",function(){setTimeout(function(){G.execCommand("wpAutoResize")},300)}),G.on("hide",function(){setTimeout(function(){g("reset")},100)}),G.on("keyup",i),G.on("undo redo",j)}).on("click.press-this keypress.press-this",".suggested-media-thumbnail",function(b){"click"!==b.type&&13!==b.keyCode||n(a(this))}).on("click.press-this",function(b){a(b.target).closest("button").hasClass("split-button-toggle")||C()}),a(".post-actions").on("click.press-this",function(c){var e,f=a(c.target),g=f.closest("button");g.length&&(g.hasClass("draft-button")?(a(".publish-button").addClass("is-saving"),m("draft")):g.hasClass("publish-button")?(g.addClass("is-saving"),b.history.replaceState&&(e=b.location.href,e+=e.indexOf("?")!==-1?"&":"?",e+="wp-press-this-reload=true",b.history.replaceState(null,null,e)),m("publish")):g.hasClass("preview-button")?(l(),b.opener&&b.opener.focus(),a("#wp-preview").val("dopreview"),a("#pressthis-form").attr("target","_blank").submit().attr("target",""),a("#wp-preview").val("")):g.hasClass("standard-editor-button")?(a(".publish-button").addClass("is-saving"),a("#pt-force-redirect").val("true"),m("draft")):g.hasClass("split-button-toggle")&&(d.hasClass("is-open")?(d.removeClass("is-open"),g.attr("aria-expanded","false")):(d.addClass("is-open"),g.attr("aria-expanded","true"))))}),w(),z(),B(),a(".options").on("click.press-this",function(){a(this).hasClass("open")?y():x()}),a(".options-panel, .options-panel-back").on("focusout.press-this",function(){setTimeout(function(){var b=document.activeElement,c=a(b);!M||!b||c.hasClass("options-panel-back")||"BODY"!==b.nodeName&&(c.closest(".options-panel").length||c.closest(".options").length)||y()},50)}),a("#post-formats-select input").on("change",function(){var b=a(this);b.is(":checked")&&a("#post-option-post-format").text(a('label[for="'+b.attr("id")+'"]').text()||"")}),J.on("beforeunload.press-this",function(){if(L||G&&G.isDirty())return c("saveAlert")}).on("resize.press-this",function(){G&&!G.isHidden()||g("reset")}),a("button.add-cat-toggle").on("click.press-this",function(){var b=a(this);b.toggleClass("is-toggled"),b.attr("aria-expanded","false"===b.attr("aria-expanded")?"true":"false"),a(".setting-modal .add-category, .categories-search-wrapper").toggleClass("is-hidden")}),a("button.add-cat-submit").on("click.press-this",o),a(".categories-search").on("keyup.press-this",function(){var b=a(this).val().toLowerCase()||"";/[\x20-\xFF]+/.test(b)&&b.length<2||(a.each(R,function(a,b){b.node.removeClass("is-hidden searched-parent")}),b&&a.each(R,function(a,c){c.text.indexOf(b)===-1?c.node.addClass("is-hidden"):c.parents.addClass("searched-parent")}))}),W.on("focus.press-this input.press-this propertychange.press-this",g),!0}function F(){a(".categories-select").find("li").each(function(){var b=a(this);R.push({node:b,parents:b.parents("li"),text:b.children(".category").text().toLowerCase()})})}var G,H,I,J=a(b),K=a(document),L=!1,M=!1,N=b.wpPressThisConfig||{},O=b.wpPressThisData||{},P=128,Q=!1,R=[],S="is-off-screen",T="is-hidden",U=S+" "+T,V=/iPad|iPod|iPhone/.test(b.navigator.userAgent),W=a("#pressthis"),X=W[0],Y=600,Z=0,$=function(){var a=document.documentElement.style;return"undefined"!=typeof a.transition?"transitionend":"undefined"!=typeof a.WebkitTransition&&"webkitTransitionEnd"}();return K.ready(function(){D(),E(),F()}),{renderNotice:q,renderError:r}};b.wp=b.wp||{},b.wp.pressThis=new c}(jQuery,window);