Skip to content

Commit 01758b1

Browse files
committed
Clean up implementation of mark attributes
Issue #5673
1 parent ce1f0cd commit 01758b1

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

doc/manual.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,9 +1716,10 @@ <h3 id="api_marker">Text-marking methods</h3>
17161716
to <code>startStyle</code>, but for the rightmost span.</dd>
17171717
<dt id="mark_css"><code><strong>css</strong>: string</code></dt>
17181718
<dd>A string of CSS to be applied to the covered text. For example <code>"color: #fe3"</code>.</dd>
1719-
<dt id="mark_attributes"><code><strong>attributes</strong>:
1720-
object</code></dt><dd>When given, will give the nodes created
1721-
for this span a HTML <code>attributes</code> with the given value.</dd>
1719+
<dt id="mark_attributes"><code><strong>attributes</strong>: object</code></dt>
1720+
<dd>When given, add the attributes in the given object to the
1721+
elements created for the marked text. Adding <code>class</code> or
1722+
<code>style</code> attributes this way is not supported.</dd>
17221723
<dt id="mark_shared"><code><strong>shared</strong>: boolean</code></dt><dd>When the
17231724
target document is <a href="#linkedDoc">linked</a> to other
17241725
documents, you can set <code>shared</code> to true to make the

src/line/line_data.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,8 @@ function buildToken(builder, text, style, startStyle, endStyle, css, attributes)
184184
if (endStyle) fullStyle += endStyle
185185
let token = elt("span", [content], fullStyle, css)
186186
if (attributes) {
187-
for (let attr in attributes){
188-
if (attributes.hasOwnProperty(attr) && attr !== "style" && attr !== "class") {
189-
token.setAttribute(attr, attributes[attr])
190-
}
191-
}
187+
for (let attr in attributes) if (attributes.hasOwnProperty(attr) && attr != "style" && attr != "class")
188+
token.setAttribute(attr, attributes[attr])
192189
}
193190
return builder.content.appendChild(token)
194191
}
@@ -263,7 +260,7 @@ function insertLineContent(line, builder, styles) {
263260
for (;;) {
264261
if (nextChange == pos) { // Update current marker set
265262
spanStyle = spanEndStyle = spanStartStyle = css = ""
266-
attributes = {}
263+
attributes = null
267264
collapsed = null; nextChange = Infinity
268265
let foundBookmarks = [], endStyles
269266
for (let j = 0; j < spans.length; ++j) {
@@ -281,8 +278,11 @@ function insertLineContent(line, builder, styles) {
281278
if (m.endStyle && sp.to == nextChange) (endStyles || (endStyles = [])).push(m.endStyle, sp.to)
282279
// support for the old title property
283280
// https://github.com/codemirror/CodeMirror/pull/5673
284-
if (m.title) {attributes.title = m.title}
285-
if (m.attributes) { attributes = m.attributes }
281+
if (m.title) (attributes || (attributes = {})).title = m.title
282+
if (m.attributes) {
283+
for (let attr in m.attributes)
284+
(attributes || (attributes = {}))[attr] = m.attributes[attr]
285+
}
286286
if (m.collapsed && (!collapsed || compareCollapsedMarkers(collapsed.marker, m) < 0))
287287
collapsed = sp
288288
} else if (sp.from > pos && nextChange > sp.from) {

src/model/mark_text.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ export function markText(doc, from, to, options, type) {
211211
if (updateMaxLine) cm.curOp.updateMaxLine = true
212212
if (marker.collapsed)
213213
regChange(cm, from.line, to.line + 1)
214-
else if (marker.className || marker.startStyle || marker.endStyle || marker.css || marker.attributes )
214+
else if (marker.className || marker.startStyle || marker.endStyle || marker.css ||
215+
marker.attributes || marker.title)
215216
for (let i = from.line; i <= to.line; i++) regLineChange(cm, i, "text")
216217
if (marker.atomic) reCheckSelection(cm.doc)
217218
signalLater(cm, "markerAdded", cm, marker)

test/test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,10 @@ testCM("markTextCSS", function(cm) {
549549
function present() {
550550
var spans = cm.display.lineDiv.getElementsByTagName("span");
551551
for (var i = 0; i < spans.length; i++)
552-
if (spans[i].style.color == "cyan" && spans[i].textContent == "cdefg") return true;
552+
if (spans[i].style.color == "cyan" && spans[i].textContent == "cdef") return true;
553553
}
554554
var m = cm.markText(Pos(0, 2), Pos(0, 6), {css: "color: cyan"});
555+
is(present());
555556
m.clear();
556557
is(!present());
557558
}, {value: "abcdefgh"});
@@ -560,9 +561,10 @@ testCM("markTextWithAttributes", function(cm) {
560561
function present() {
561562
var spans = cm.display.lineDiv.getElementsByTagName("span");
562563
for (var i = 0; i < spans.length; i++)
563-
if (spans[i].getAttribute('label') == "label" && span[i].textContent == "cdefg") return true;
564+
if (spans[i].getAttribute("label") == "label" && spans[i].textContent == "cdef") return true;
564565
}
565566
var m = cm.markText(Pos(0, 2), Pos(0, 6), {attributes: {label: "label"}});
567+
is(present());
566568
m.clear();
567569
is(!present());
568570
}, {value: "abcdefgh"});

0 commit comments

Comments
 (0)