Index: trunk/extensions/VisualEditor/modules/parser/mediawiki.DOMPostProcessor.js |
— | — | @@ -24,55 +24,42 @@ |
25 | 25 | } |
26 | 26 | }; |
27 | 27 | |
| 28 | +// Wrap all top-level inline elements in paragraphs. This should also be |
| 29 | +// applied inside block-level elements, but in that case the first paragraph |
| 30 | +// usually remains plain inline. |
28 | 31 | var process_inlines_in_p = function ( document ) { |
29 | | - // document.body does not always work in jsdom |
| 32 | + // document.body does not always work in jsdom, so work around it. |
30 | 33 | var body = document.getElementsByTagName('body')[0], |
31 | | - children = body.cloneNode(false), |
| 34 | + newP = document.createElement('p'), |
32 | 35 | cnodes = body.childNodes, |
33 | | - inlineStack = []; |
| 36 | + haveInlines = false, |
| 37 | + deleted = 0; |
34 | 38 | |
35 | | - function wrapInlines (inlines) { |
36 | | - var newp = document.createElement('p'); |
37 | | - for(var i = 0, length = inlines.length; i < length; i++) { |
38 | | - newp.appendChild(inlines[i]); |
39 | | - } |
40 | | - body.appendChild(newp); |
41 | | - inlineStack = []; |
42 | | - } |
43 | | - var i, |
44 | | - length = cnodes.length; |
45 | | - // Clear body |
46 | | - for(i = 0; i < length; i++) { |
47 | | - var cnode = body.firstChild; |
48 | | - children.appendChild(cnode); |
49 | | - } |
50 | | - |
51 | 39 | function isElementContentWhitespace ( e ) { |
52 | 40 | return (e.data.match(/^[ \r\n\t]*$/) !== null); |
53 | 41 | } |
54 | 42 | |
55 | | - // Now re-append all block elements and inline elements wrapped in |
56 | | - // paragraphs. |
57 | | - for(i = 0; i < length; i++) { |
58 | | - var child = children.firstChild, |
| 43 | + for(var i = 0, length = cnodes.length; i < length; i++) { |
| 44 | + var child = cnodes[i - deleted], |
59 | 45 | ctype = child.nodeType; |
60 | 46 | //console.log(child + ctype); |
61 | | - if ((ctype === 3 && (inlineStack.length || !isElementContentWhitespace(child))) || |
62 | | - (ctype !== 3 && // text |
63 | | - ctype !== 8 && // comment |
64 | | - !isBlock(child.nodeName))) { |
| 47 | + if (ctype === 3 && (haveInlines || !isElementContentWhitespace(child))) || |
| 48 | + (ctype !== 3 && // text |
| 49 | + ctype !== 8 && // comment |
| 50 | + !isBlock(child.nodeName))) { |
65 | 51 | // text node |
66 | | - inlineStack.push(child); |
67 | | - } else if (inlineStack.length) { |
68 | | - wrapInlines(inlineStack); |
69 | | - body.appendChild(child); |
70 | | - } else { |
71 | | - body.appendChild(child); |
72 | | - } |
| 52 | + newP.appendChild(child); |
| 53 | + haveInlines = true; |
| 54 | + deleted++; |
| 55 | + } else if (haveInlines) { |
| 56 | + body.insertBefore(newP, child); |
| 57 | + newP = document.createElement('p'); |
| 58 | + haveInlines = false; |
| 59 | + } |
73 | 60 | } |
74 | 61 | |
75 | | - if (inlineStack.length) { |
76 | | - wrapInlines(inlineStack); |
| 62 | + if (haveInlines) { |
| 63 | + body.appendChild(newP); |
77 | 64 | } |
78 | 65 | }; |
79 | 66 | |