| OLD | NEW |
| 1 /** | 1 /** |
| 2 * Copyright 2017 Google Inc. All rights reserved. | 2 * Copyright 2017 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Licensed under the Apache License, Version 2.0 (the "License"); | 4 * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 * you may not use this file except in compliance with the License. | 5 * you may not use this file except in compliance with the License. |
| 6 * You may obtain a copy of the License at | 6 * You may obtain a copy of the License at |
| 7 * | 7 * |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 | 8 * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 * | 9 * |
| 10 * Unless required by applicable law or agreed to in writing, software | 10 * Unless required by applicable law or agreed to in writing, software |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 resetTemplates() { | 94 resetTemplates() { |
| 95 this.findAll('template[data-stamped]', this._document).forEach(t => { | 95 this.findAll('template[data-stamped]', this._document).forEach(t => { |
| 96 t.removeAttribute('data-stamped'); | 96 t.removeAttribute('data-stamped'); |
| 97 }); | 97 }); |
| 98 } | 98 } |
| 99 | 99 |
| 100 /** | 100 /** |
| 101 * @param {string} text | 101 * @param {string} text |
| 102 * @return {!Element} | 102 * @return {!Element} |
| 103 */ | 103 */ |
| 104 convertMarkdownLinkSnippets(text) { | 104 createSpanFromMarkdown(text) { |
| 105 const element = this.createElement('span'); | 105 const element = this.createElement('span'); |
| 106 | 106 |
| 107 // Split on markdown links (e.g. [some link](https://...)). | 107 // Split on markdown links (e.g. [some link](https://...)). |
| 108 const parts = text.split(/\[(.*?)\]\((https?:\/\/.*?)\)/g); | 108 const parts = text.split(/\[(.*?)\]\((https?:\/\/.*?)\)/g); |
| 109 | 109 |
| 110 while (parts.length) { | 110 while (parts.length) { |
| 111 // Pop off the same number of elements as there are capture groups. | 111 // Pop off the same number of elements as there are capture groups. |
| 112 const [preambleText, linkText, linkHref] = parts.splice(0, 3); | 112 const [preambleText, linkText, linkHref] = parts.splice(0, 3); |
| 113 element.appendChild(this._document.createTextNode(preambleText)); | 113 element.appendChild(this._document.createTextNode(preambleText)); |
| 114 | 114 |
| 115 // Append link if there are any. | 115 // Append link if there are any. |
| 116 if (linkText && linkHref) { | 116 if (linkText && linkHref) { |
| 117 const a = /** @type {!HTMLAnchorElement} */ (this.createElement('a')); | 117 const a = /** @type {!HTMLAnchorElement} */ (this.createElement('a')); |
| 118 a.rel = 'noopener'; | 118 a.rel = 'noopener'; |
| 119 a.target = '_blank'; | 119 a.target = '_blank'; |
| 120 a.textContent = linkText; | 120 a.textContent = linkText; |
| 121 a.href = (new URL(linkHref)).href; | 121 a.href = (new URL(linkHref)).href; |
| 122 element.appendChild(a); | 122 element.appendChild(a); |
| 123 } | 123 } |
| 124 } | 124 } |
| 125 | 125 |
| 126 return element; | |
| 127 } | |
| 128 | |
| 129 /** | |
| 130 * @param {string} text | |
| 131 * @return {!Element} | |
| 132 */ | |
| 133 convertMarkdownCodeSnippets(text) { | |
| 134 const element = this.createElement('span'); | |
| 135 | |
| 136 const parts = text.split(/`(.*?)`/g); // Split on markdown code slashes | |
| 137 while (parts.length) { | |
| 138 // Pop off the same number of elements as there are capture groups. | |
| 139 const [preambleText, codeText] = parts.splice(0, 2); | |
| 140 element.appendChild(this._document.createTextNode(preambleText)); | |
| 141 if (codeText) { | |
| 142 const pre = /** @type {!HTMLPreElement} */ (this.createElement('code')); | |
| 143 pre.textContent = codeText; | |
| 144 element.appendChild(pre); | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 return element; | 126 return element; |
| 149 } | 127 } |
| 150 | 128 |
| 151 /** | 129 /** |
| 152 * @return {!Document} | 130 * @return {!Document} |
| 153 */ | 131 */ |
| 154 document() { | 132 document() { |
| 155 return this._document; | 133 return this._document; |
| 156 } | 134 } |
| 157 | 135 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 179 findAll(query, context) { | 157 findAll(query, context) { |
| 180 return Array.from(context.querySelectorAll(query)); | 158 return Array.from(context.querySelectorAll(query)); |
| 181 } | 159 } |
| 182 } | 160 } |
| 183 | 161 |
| 184 if (typeof module !== 'undefined' && module.exports) { | 162 if (typeof module !== 'undefined' && module.exports) { |
| 185 module.exports = DOM; | 163 module.exports = DOM; |
| 186 } else { | 164 } else { |
| 187 self.DOM = DOM; | 165 self.DOM = DOM; |
| 188 } | 166 } |
| OLD | NEW |