knockout.punches icon indicating copy to clipboard operation
knockout.punches copied to clipboard

Disable text interpolation for target block

Open SerafimArts opened this issue 8 years ago • 7 comments

Im does not found this option in docs. Is this exists?

Like:

<div class="{{ works }}">
    <code data-bind="punches: false">
        Some {{ example with }} {{ raw }} text
    </code>
</div>

UPD Fastfix (slow and have bugs):

for (let node of document.body.querySelectorAll('[data-punches="false"]')) {
    node.innerHTML = node.innerHTML
       .replace(/(\{\{|\}\})/g, p => `{{"${p}"}}`)
}

UPD Fastfix No2 (does not work):

for (let node of document.body.querySelectorAll('[data-punches="false"]')) {
    node.innerHTML = node.innerHTML
        .replace(/\{\{/g, '&#123; &#123;')
        .replace(/\}\}/g, '&#125; &#125;');
}

UPD Fastfix No3 (works but has side effects)

for (let node of document.body.querySelectorAll('[data-punches="false"]')) {
    node.innerHTML = node.innerHTML
        .replace(/\{\{([\s\S]*?)\}\}/g, (i, j) => `&#123;&#8203;&#123;${j}&#125;&#8203;&#125;`)
}

SerafimArts avatar Feb 20 '17 00:02 SerafimArts

I do not know how fix this trouble =(

SerafimArts avatar Feb 20 '17 01:02 SerafimArts

See https://github.com/mbest/knockout.punches/issues/60

jlaustill avatar Feb 20 '17 01:02 jlaustill

@jlaustill the code is not created dynamically

SerafimArts avatar Feb 20 '17 01:02 SerafimArts

True, just thought it might provoke ideas since it was a similar issue :)

jlaustill avatar Feb 20 '17 01:02 jlaustill

@jlaustill hmmm... yep, there are ideas, will think.

Thanks =)

SerafimArts avatar Feb 20 '17 01:02 SerafimArts

Another variant:

ko.bindingHandlers.interpolation = {
    init: (node, enabled) => {
        if (!ko.unwrap(enabled())) {
            node.innerHTML = node.innerHTML
                .replace(/{{([\s\S]*?)}}/g, (i, j) => `{{${j}}&#8203;}`); 
               // &#8203; is a Magic Invisible Char 
        }
    }
}
<div class="{{ works }}">
    <code data-bind="interpolation: false">
        Some {{ example with }} {{ raw }} text
    </code>
</div>

In the source code did not find how to remove interpolation for the node...

SerafimArts avatar Feb 20 '17 01:02 SerafimArts

Try using allowBindings as described in the Knockout docs.

mbest avatar Feb 21 '17 00:02 mbest