| 1 | /*! |
| 2 | * VisualEditor parsing utilities, used when converting HTMLDocuments and strings. |
| 3 | * |
| 4 | * @copyright See AUTHORS.txt |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * Check whether a given DOM element has a block element type. |
| 9 | * |
| 10 | * @param {HTMLElement|string} element Element or element name |
| 11 | * @return {boolean} Element is a block element |
| 12 | */ |
| 13 | ve.isBlockElement = function ( element ) { |
| 14 | const elementName = typeof element === 'string' ? element : element.nodeName; |
| 15 | return ve.elementTypes.block.indexOf( elementName.toLowerCase() ) !== -1; |
| 16 | }; |
| 17 | |
| 18 | /** |
| 19 | * Check whether a given DOM element is a void element (can't have children). |
| 20 | * |
| 21 | * @param {HTMLElement|string} element Element or element name |
| 22 | * @return {boolean} Element is a void element |
| 23 | */ |
| 24 | ve.isVoidElement = function ( element ) { |
| 25 | const elementName = typeof element === 'string' ? element : element.nodeName; |
| 26 | return ve.elementTypes.void.indexOf( elementName.toLowerCase() ) !== -1; |
| 27 | }; |
| 28 | |
| 29 | ve.elementTypes = { |
| 30 | block: [ |
| 31 | 'div', 'p', |
| 32 | // Tables |
| 33 | 'table', 'tbody', 'thead', 'tfoot', 'caption', 'th', 'tr', 'td', |
| 34 | // Lists |
| 35 | 'ul', 'ol', 'li', 'dl', 'dt', 'dd', |
| 36 | // HTML5 heading content |
| 37 | 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hgroup', |
| 38 | // HTML5 sectioning content |
| 39 | 'article', 'aside', 'body', 'nav', 'section', 'footer', 'header', 'figure', |
| 40 | 'figcaption', 'fieldset', 'details', 'blockquote', |
| 41 | // Other |
| 42 | 'hr', 'button', 'canvas', 'center', 'col', 'colgroup', 'embed', |
| 43 | 'map', 'object', 'pre', 'progress', 'video' |
| 44 | ], |
| 45 | void: [ |
| 46 | // https://html.spec.whatwg.org/#void-elements |
| 47 | 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', |
| 48 | 'link', 'meta', 'param', 'source', 'track', 'wbr' |
| 49 | ] |
| 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * Match a specific HTML tag that appears once, e.g. 'html' or 'body' |
| 54 | * |
| 55 | * @param {string} html Document HTML |
| 56 | * @param {string} tag Tag name |
| 57 | * @return {string[]|null} Regex match, null if not found |
| 58 | */ |
| 59 | ve.matchTag = function ( html, tag ) { |
| 60 | return html.match( |
| 61 | |
| 62 | new RegExp( '<' + tag + '(>|\\s[^>]*>)' ) |
| 63 | ); |
| 64 | }; |
| 65 | |
| 66 | /** |
| 67 | * Add a tag to `<head>` using HTML string splicing |
| 68 | * |
| 69 | * @param {string} docHtml Document HTML |
| 70 | * @param {string} tagHtml Tag HTML to be added to `<head>` |
| 71 | * @return {string} Document HTML |
| 72 | */ |
| 73 | ve.addHeadTag = function ( docHtml, tagHtml ) { |
| 74 | /** |
| 75 | * Splice text after a regex match |
| 76 | * |
| 77 | * @param {string[]} match Regex match |
| 78 | * @param {string} text Text to insert |
| 79 | * @return {string} |
| 80 | */ |
| 81 | function insertAfter( match, text ) { |
| 82 | const offset = match.index + match[ 0 ].length; |
| 83 | return docHtml.slice( 0, offset ) + |
| 84 | text + |
| 85 | docHtml.slice( offset ); |
| 86 | } |
| 87 | |
| 88 | const headMatch = ve.matchTag( docHtml, 'head' ); |
| 89 | if ( headMatch ) { |
| 90 | return insertAfter( headMatch, tagHtml ); |
| 91 | } else { |
| 92 | const htmlMatch = ve.matchTag( docHtml, 'html' ); |
| 93 | if ( htmlMatch ) { |
| 94 | // <html> but no <head> |
| 95 | return insertAfter( htmlMatch, '<head>' + tagHtml + '</head>' ); |
| 96 | } else { |
| 97 | // No <html> or </head> |
| 98 | return '<head>' + tagHtml + '</head>' + docHtml; |
| 99 | } |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | /** |
| 104 | * Create an HTMLDocument from an HTML string. |
| 105 | * |
| 106 | * The html parameter is supposed to be a full HTML document with a doctype and an `<html>` tag. |
| 107 | * If you pass a document fragment, it will be wrapped in `<body>…</body>`. |
| 108 | * |
| 109 | * To create an empty document, pass the empty string. |
| 110 | * |
| 111 | * If your input is both valid HTML and valid XML, and you need to work around style |
| 112 | * normalization bugs in Internet Explorer, use #parseXhtml and #serializeXhtml. |
| 113 | * |
| 114 | * @param {string} html |
| 115 | * @return {HTMLDocument} Document constructed from the HTML string |
| 116 | */ |
| 117 | ve.createDocumentFromHtml = function ( html ) { |
| 118 | if ( html !== '' ) { |
| 119 | if ( !ve.matchTag( html, 'body' ) ) { |
| 120 | // When the given HTML fragment starts with a <meta> or <style> element, it is placed in the |
| 121 | // automatically generated <head> rather than <body>, and breaks our assumptions. (T273234) |
| 122 | html = '<body>' + html + '</body>'; |
| 123 | } |
| 124 | // Add iOS hack (T116525) |
| 125 | html = ve.addHeadTag( html, '<meta name="format-detection" content="telephone=no" data-ve-tmp/>' ); |
| 126 | } |
| 127 | |
| 128 | const newDocument = new DOMParser().parseFromString( html, 'text/html' ); |
| 129 | |
| 130 | // Remove iOS hack |
| 131 | const tmpMeta = newDocument.querySelector( 'meta[data-ve-tmp]' ); |
| 132 | if ( tmpMeta ) { |
| 133 | tmpMeta.parentNode.removeChild( tmpMeta ); |
| 134 | } |
| 135 | |
| 136 | return newDocument; |
| 137 | }; |
| 138 | |
| 139 | /** |
| 140 | * Take a target document with a possibly relative base URL, and modify it to be absolute. |
| 141 | * The base URL of the target document is resolved using the base URL of the source document. |
| 142 | * |
| 143 | * Note that the fallbackBase parameter will be used if there is no <base> tag, even if |
| 144 | * the document does have a valid base URL: this is to work around Firefox's behavior of having |
| 145 | * documents created by DOMParser inherit the base URL of the main document. |
| 146 | * |
| 147 | * @param {HTMLDocument} targetDoc Document whose base URL should be resolved |
| 148 | * @param {HTMLDocument} sourceDoc Document whose base URL should be used for resolution |
| 149 | * @param {string} [fallbackBase] Base URL to use if resolving the base URL fails or there is no <base> tag |
| 150 | */ |
| 151 | ve.fixBase = function ( targetDoc, sourceDoc, fallbackBase ) { |
| 152 | let baseNode = targetDoc.getElementsByTagName( 'base' )[ 0 ]; |
| 153 | if ( baseNode ) { |
| 154 | // Support: Safari |
| 155 | // In Safari a base node with an invalid href (e.g. protocol-relative) |
| 156 | // in a document which has been dynamically created results in |
| 157 | // 'about:blank' rather than '' or null. The base's href will also be '', |
| 158 | // but that works out just setting the base to fallbackBase, so it's okay. |
| 159 | if ( !targetDoc.baseURI || targetDoc.baseURI === 'about:blank' ) { |
| 160 | // <base> tag present but not valid, try resolving its URL |
| 161 | baseNode.setAttribute( 'href', ve.resolveUrl( baseNode.getAttribute( 'href' ), sourceDoc ) ); |
| 162 | if ( !targetDoc.baseURI && fallbackBase ) { |
| 163 | // That didn't work, use the fallback |
| 164 | baseNode.setAttribute( 'href', fallbackBase ); |
| 165 | } |
| 166 | } |
| 167 | // Support: Chrome |
| 168 | // Chrome just entirely ignores <base> tags with a protocol-relative href attribute. |
| 169 | // Code below is *not a no-op*; reading the href property and setting it back |
| 170 | // will expand the href *attribute* to use an absolute URL if it was relative. |
| 171 | // eslint-disable-next-line no-self-assign |
| 172 | baseNode.href = baseNode.href; |
| 173 | } else if ( fallbackBase ) { |
| 174 | // Support: Firefox |
| 175 | // No <base> tag, add one |
| 176 | baseNode = targetDoc.createElement( 'base' ); |
| 177 | baseNode.setAttribute( 'href', fallbackBase ); |
| 178 | targetDoc.head.appendChild( baseNode ); |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | /** |
| 183 | * Get the actual inner HTML of a DOM node. |
| 184 | * |
| 185 | * In most browsers, .innerHTML is broken and eats newlines in `<pre>` elements, see |
| 186 | * https://bugzilla.mozilla.org/show_bug.cgi?id=838954 . This function detects this behavior |
| 187 | * and works around it, to the extent possible. `<pre>\nFoo</pre>` will become `<pre>Foo</pre>` |
| 188 | * if the browser is broken, but newlines are preserved in all other cases. |
| 189 | * |
| 190 | * @param {HTMLElement} element HTML element to get inner HTML of |
| 191 | * @return {string} Inner HTML |
| 192 | */ |
| 193 | ve.properInnerHtml = function ( element ) { |
| 194 | return ve.fixupPreBug( element ).innerHTML; |
| 195 | }; |
| 196 | |
| 197 | /** |
| 198 | * Get the actual outer HTML of a DOM node. |
| 199 | * |
| 200 | * @see ve.properInnerHtml |
| 201 | * @param {HTMLElement} element HTML element to get outer HTML of |
| 202 | * @return {string} Outer HTML |
| 203 | */ |
| 204 | ve.properOuterHtml = function ( element ) { |
| 205 | return ve.fixupPreBug( element ).outerHTML; |
| 206 | }; |
| 207 | |
| 208 | /** |
| 209 | * Helper function for #properInnerHtml, #properOuterHtml and #serializeXhtml. |
| 210 | * |
| 211 | * Detect whether the browser has broken `<pre>` serialization, and if so return a clone |
| 212 | * of the node with extra newlines added to make it serialize properly. If the browser is not |
| 213 | * broken, just return the original node. |
| 214 | * |
| 215 | * @param {HTMLElement} element HTML element to fix up |
| 216 | * @return {HTMLElement} Either element, or a fixed-up clone of it |
| 217 | */ |
| 218 | ve.fixupPreBug = function ( element ) { |
| 219 | // Support: Chrome, FF |
| 220 | if ( ve.isPreInnerHtmlBroken === undefined ) { |
| 221 | // Test whether newlines in `<pre>` are serialized back correctly |
| 222 | const div = document.createElement( 'div' ); |
| 223 | div.innerHTML = '<pre>\n\n</pre>'; |
| 224 | ve.isPreInnerHtmlBroken = div.innerHTML === '<pre>\n</pre>'; |
| 225 | } |
| 226 | |
| 227 | if ( !ve.isPreInnerHtmlBroken ) { |
| 228 | return element; |
| 229 | } |
| 230 | |
| 231 | // Workaround for T44469: if a `<pre>` starts with a newline, that means .innerHTML will |
| 232 | // screw up and stringify it with one fewer newline. Work around this by adding a newline. |
| 233 | // If we don't see a leading newline, we still don't know if the original HTML was |
| 234 | // `<pre>Foo</pre>` or `<pre>\nFoo</pre>`, but that's a syntactic difference, not a |
| 235 | // semantic one, and handling that is the integration target's job. |
| 236 | const $element = $( element ).clone(); |
| 237 | $element.find( 'pre, textarea, listing' ).each( ( i, el ) => { |
| 238 | let matches; |
| 239 | if ( el.firstChild && el.firstChild.nodeType === Node.TEXT_NODE ) { |
| 240 | matches = el.firstChild.data.match( /^(\r\n|\r|\n)/ ); |
| 241 | if ( matches && matches[ 1 ] ) { |
| 242 | // Prepend a newline exactly like the one we saw |
| 243 | el.firstChild.insertData( 0, matches[ 1 ] ); |
| 244 | } |
| 245 | } |
| 246 | } ); |
| 247 | return $element.get( 0 ); |
| 248 | }; |
| 249 | |
| 250 | /** |
| 251 | * Helper function for #transformStyleAttributes. |
| 252 | * |
| 253 | * Normalize an attribute value. In compliant browsers, this should be |
| 254 | * a no-op, but in IE style attributes are normalized on all elements, |
| 255 | * color and bgcolor attributes are normalized on some elements (like `<tr>`), |
| 256 | * and width and height attributes are normalized on some elements( like `<table>`). |
| 257 | * |
| 258 | * @param {string} name Attribute name |
| 259 | * @param {string} value Attribute value |
| 260 | * @param {string} [nodeName='div'] Element name |
| 261 | * @return {string} Normalized attribute value |
| 262 | */ |
| 263 | ve.normalizeAttributeValue = function ( name, value, nodeName ) { |
| 264 | const node = document.createElement( nodeName || 'div' ); |
| 265 | node.setAttribute( name, value ); |
| 266 | return node.getAttribute( name ); |
| 267 | }; |
| 268 | |
| 269 | /** |
| 270 | * Resolve a URL relative to a given base. |
| 271 | * |
| 272 | * @param {string} url URL to resolve |
| 273 | * @param {HTMLDocument} base Document whose base URL to use |
| 274 | * @return {string} Resolved URL |
| 275 | */ |
| 276 | ve.resolveUrl = function ( url, base ) { |
| 277 | const node = base.createElement( 'a' ); |
| 278 | node.setAttribute( 'href', url ); |
| 279 | // If doc.baseURI isn't set, node.href will be an empty string |
| 280 | // This is crazy, returning the original URL is better |
| 281 | return node.href || url; |
| 282 | }; |