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 createSpanFromMarkdown(text) { | 104 convertMarkdownLinkSnippets(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; | 126 return element; |
127 } | 127 } |
128 | 128 |
129 /** | 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; |
| 149 } |
| 150 |
| 151 /** |
130 * @return {!Document} | 152 * @return {!Document} |
131 */ | 153 */ |
132 document() { | 154 document() { |
133 return this._document; | 155 return this._document; |
134 } | 156 } |
135 | 157 |
136 /** | 158 /** |
137 * Guaranteed context.querySelector. Always returns an element or throws if | 159 * Guaranteed context.querySelector. Always returns an element or throws if |
138 * nothing matches query. | 160 * nothing matches query. |
139 * @param {string} query | 161 * @param {string} query |
(...skipping 17 matching lines...) Expand all Loading... |
157 findAll(query, context) { | 179 findAll(query, context) { |
158 return Array.from(context.querySelectorAll(query)); | 180 return Array.from(context.querySelectorAll(query)); |
159 } | 181 } |
160 } | 182 } |
161 | 183 |
162 if (typeof module !== 'undefined' && module.exports) { | 184 if (typeof module !== 'undefined' && module.exports) { |
163 module.exports = DOM; | 185 module.exports = DOM; |
164 } else { | 186 } else { |
165 self.DOM = DOM; | 187 self.DOM = DOM; |
166 } | 188 } |
OLD | NEW |