Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: lib/src/prism/plugins/keep-markup/prism-keep-markup.js

Issue 1418513006: update elements and fix some bugs (Closed) Base URL: git@github.com:dart-lang/polymer_elements.git@master
Patch Set: code review updates Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 (function () {
2
3 if (typeof self === 'undefined' || !self.Prism || !self.document || !doc ument.createRange) {
4 return;
5 }
6
7 Prism.hooks.add('before-highlight', function (env) {
8 var firstWhiteSpaces = false;
9 var pos = 0;
10 var data = [];
11 var f = function (elt, baseNode) {
12 var o = {};
13 if (!baseNode) {
14 // Clone the original tag to keep all attributes
15 o.clone = elt.cloneNode(false);
16 o.posOpen = pos;
17 data.push(o);
18 }
19 for (var i = 0, l = elt.childNodes.length; i < l; i++) {
20 var child = elt.childNodes[i];
21 if (child.nodeType === 1) { // element
22 f(child);
23 } else if(child.nodeType === 3) { // text
24 if(!firstWhiteSpaces) {
25 // We need to ignore the first w hite spaces in the code block
26 child.data = child.data.replace( /^(?:\r?\n|\r)/, '');
27 firstWhiteSpaces = true;
28 }
29 pos += child.data.length;
30 }
31 }
32 if (!baseNode) {
33 o.posClose = pos;
34 }
35 };
36 f(env.element, true);
37
38 if (data && data.length) {
39 // data is an array of all existing tags
40 env.keepMarkup = data;
41 }
42 });
43
44 Prism.hooks.add('after-highlight', function (env) {
45 if(env.keepMarkup && env.keepMarkup.length) {
46
47 var walk = function (elt, nodeState) {
48 for (var i = 0, l = elt.childNodes.length; i < l ; i++) {
49
50 var child = elt.childNodes[i];
51
52 if (child.nodeType === 1) { // element
53 if (!walk(child, nodeState)) {
54 return false;
55 }
56
57 } else if (child.nodeType === 3) { // te xt
58 if(!nodeState.nodeStart && nodeS tate.pos + child.data.length > nodeState.node.posOpen) {
59 // We found the start po sition
60 nodeState.nodeStart = ch ild;
61 nodeState.nodeStartPos = nodeState.node.posOpen - nodeState.pos;
62 }
63 if(nodeState.nodeStart && nodeSt ate.pos + child.data.length >= nodeState.node.posClose) {
64 // We found the end posi tion
65 nodeState.nodeEnd = chil d;
66 nodeState.nodeEndPos = n odeState.node.posClose - nodeState.pos;
67 }
68
69 nodeState.pos += child.data.leng th;
70 }
71
72 if (nodeState.nodeStart && nodeState.nod eEnd) {
73 // Select the range and wrap it with the clone
74 var range = document.createRange ();
75 range.setStart(nodeState.nodeSta rt, nodeState.nodeStartPos);
76 range.setEnd(nodeState.nodeEnd, nodeState.nodeEndPos);
77 nodeState.node.clone.appendChild (range.extractContents());
78 range.insertNode(nodeState.node. clone);
79 range.detach();
80
81 // Process is over
82 return false;
83 }
84 }
85 return true;
86 };
87
88 // For each tag, we walk the DOM to reinsert it
89 env.keepMarkup.forEach(function (node) {
90 walk(env.element, {
91 node: node,
92 pos: 0
93 });
94 });
95 }
96 });
97 }());
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698