/**
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
import"../polymer/polymer-legacy.js";import"./marked-import.js";import{Polymer as t}from"../polymer/lib/legacy/polymer-fn.js";import{dom as e}from"../polymer/lib/legacy/polymer.dom.js";import{html as n}from"../polymer/lib/utils/html-tag.js";t({_template:n`
    <style>
      :host {
        display: block;
      }
    </style>
    <slot name="markdown-html">
      <div id="content"></div>
    </slot>
  `,is:"marked-element",properties:{markdown:{type:String,value:null},breaks:{type:Boolean,value:!1},pedantic:{type:Boolean,value:!1},renderer:{type:Function,value:null},sanitize:{type:Boolean,value:!1},sanitizer:{type:Function,value:null},disableRemoteSanitization:{type:Boolean,value:!1},smartypants:{type:Boolean,value:!1},callback:{type:Function,value:null},xhr:{type:Object,notify:!0,readOnly:!0}},observers:["render(markdown, breaks, pedantic, renderer, sanitize, sanitizer, smartypants, callback)"],ready:function(){this.markdown||(this._markdownElement=e(this).querySelector('[type="text/markdown"]'),this._markdownElement&&(this._markdownElement.src&&this._request(this._markdownElement.src),""!==this._markdownElement.textContent.trim()&&(this.markdown=this._unindent(this._markdownElement.textContent)),new MutationObserver(this._onScriptAttributeChanged.bind(this)).observe(this._markdownElement,{attributes:!0})))},attached:function(){this._attached=!0,this._outputElement=this.outputElement,this.render()},detached:function(){this._attached=!1},unindent:function(t){return this._unindent(t)},get outputElement(){return e(this).queryDistributedElements('[slot="markdown-html"]')[0]||this.$.content},render:function(){if(this._attached)if(this.markdown){var t=new marked.Renderer;this.renderer&&this.renderer(t);var n={renderer:t,highlight:this._highlight.bind(this),breaks:this.breaks,sanitize:this.sanitize,sanitizer:this.sanitizer,pedantic:this.pedantic,smartypants:this.smartypants};e(this._outputElement).innerHTML=marked(this.markdown,n,this.callback),this.fire("marked-render-complete",{},{composed:!0})}else e(this._outputElement).innerHTML=""},_highlight:function(t,e){return this.fire("syntax-highlight",{code:t,lang:e},{composed:!0}).detail.code||t},_unindent:function(t){if(!t)return t;var e=t.replace(/\t/g,"  ").split("\n"),n=e.reduce((function(t,e){if(/^\s*$/.test(e))return t;var n=e.match(/^(\s*)/)[0].length;return null===t||n<t?n:t}),null);return e.map((function(t){return t.substr(n)})).join("\n")},_request:function(t){this._setXhr(new XMLHttpRequest);var e=this.xhr;if(e.readyState>0)return null;e.addEventListener("error",this._handleError.bind(this)),e.addEventListener("loadend",function(t){var e=this.xhr.status||0;0===e||e>=200&&e<300?(this.sanitize=!this.disableRemoteSanitization,this.markdown=t.target.response):this._handleError(t),this.fire("marked-loadend",t)}.bind(this)),e.open("GET",t),e.setRequestHeader("Accept","text/markdown"),e.send()},_handleError:function(t){this.fire("marked-request-error",t,{cancelable:!0}).defaultPrevented||(this.markdown="Failed loading markdown source")},_onScriptAttributeChanged:function(t){"src"===t[0].attributeName&&this._request(this._markdownElement.src)}});