<commit_before>// Copyright (c) 2011 <NAME>. All rights reserved.
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.

var port;
var nextEditorId = 1;

var editorCallbacks = {}

function ensurePortConnected() {
    if (port)
	return;
    port = chrome.extension.connect({name: "content_script"});

    port.onMessage.addListener(function (msg) {
	if (msg.type === "edit_done") {
	    if (editorCallbacks[msg.source])
		editorCallbacks[msg.source](msg.text);
	}
    });
}

function positioned(elem) {
    var position = window.getComputedStyle(elem).position;
    return (position === "absolute" ||
	    position === "relative" ||
	    position === "fixed");
}

function hookTextArea(node) {
    var editorId = nextEditorId++;

    function attachEmacs() {
	if (!node.parentNode)
	    return;
	var container = document.createElement("embed");
	container.type = "application/x-embedded-emacs-container";
	function relayout() {
	    container.style.setProperty("width", node.offsetWidth + "px",
				     "important");
	    container.style.setProperty("height", node.offsetHeight + "px",
				     "important");
	    // Apparently offsetParent is a lie.
	    var parent = node.offsetParent;
	    var top = node.offsetTop;
	    var left = node.offsetLeft;
	    var body = node.ownerDocument.body;
	    var docElem = node.ownerDocument.documentElement;
	    while (parent &&
		   parent !== body && parent !== docElem &&
		   !positioned(parent)) {
		top += parent.offsetTop;
		left += parent.offsetLeft;
		parent = parent.offsetParent;
	    }
	    container.style.setProperty("top", top + "px", "important");
	    container.style.setProperty("left", left + "px", "important");
	    if (window.getComputedStyle(node).position === "fixed") {
		container.style.setProperty("position", "fixed", "important");
	    } else {
		container.style.setProperty("position", "absolute", "important");
	    }
	}
	relayout();
	node.parentNode.insertBefore(container, node);

	function onAttrModified(ev) {
	    if (ev.attrName !== "style")
		return;
	    relayout();
	}
	// FIXME: This doesn't actually catch CSS changes.
	node.addEventListener("DOMAttrModified", onAttrModified);
	window.addEventListener("resize", relayout);

	// FIXME: If another script decides to change this value
	// again, act accordingly.
	var oldVisibility = node.style.visibility;
	node.style.visibility = "hidden";
	port.postMessage({
	    type: "start_editor",
	    id: editorId,
            text: node.value,
            windowId: container.windowId
	});

	editorCallbacks[editorId] = function (text) {
	    node.removeEventListener("DOMAttrModified", onAttrModified);<commit_msg>Draw a border around the text box<commit_after>// Copyright (c) 2011 <NAME>. All rights reserved.
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.

var port;
var nextEditorId = 1;

var editorCallbacks = {}

function ensurePortConnected() {
    if (port)
	return;
    port = chrome.extension.connect({name: "content_script"});

    port.onMessage.addListener(function (msg) {
	if (msg.type === "edit_done") {
	    if (editorCallbacks[msg.source])
		editorCallbacks[msg.source](msg.text);
	}
    });
}

function positioned(elem) {
    var position = window.getComputedStyle(elem).position;
    return (position === "absolute" ||
	    position === "relative" ||
	    position === "fixed");
}

function hookTextArea(node) {
    var editorId = nextEditorId++;

    function attachEmacs() {
	if (!node.parentNode)
	    return;
	var container = document.createElement("div");
	var embed = document.createElement("embed");
	embed.type = "application/x-embedded-emacs-container";
	embed.style.setProperty("width", "100%", "important");
	embed.style.setProperty("height", "100%", "important");
	embed.style.setProperty("padding", "0px", "important");
	embed.style.setProperty("margin", "0px", "important");
	container.appendChild(embed);
	container.style.setProperty("border", "1px solid black", "important");
	container.style.setProperty("padding", "0px", "important");
	container.style.setProperty("margin", "0px", "important");
	function relayout() {
	    container.style.setProperty("width", node.offsetWidth + "px",
					"important");
	    container.style.setProperty("height", node.offsetHeight + "px",
					"important");
	    // Apparently offsetParent is a lie.
	    var parent = node.offsetParent;
	    var top = node.offsetTop;
	    var left = node.offsetLeft;
	    var body = node.ownerDocument.body;
	    var docElem = node.ownerDocument.documentElement;
	    while (parent &&
		   parent !== body && parent !== docElem &&
		   !positioned(parent)) {
		top += parent.offsetTop;
		left += parent.offsetLeft;
		parent = parent.offsetParent;
	    }
	    container.style.setProperty("top", top + "px", "important");
	    container.style.setProperty("left", left + "px", "important");
	    if (window.getComputedStyle(node).position === "fixed") {
		container.style.setProperty("position", "fixed", "important");
	    } else {
		container.style.setProperty("position", "absolute", "important");
	    }
	}
	relayout();
	node.parentNode.insertBefore(container, node);

	function onAttrModified(ev) {
	    if (ev.attrName !== "style")
		return;
	    relayout();
	}
	// FIXME: This doesn't actually catch CSS changes.
	node.addEventListener("DOMAttrModified", onAttrModified);
	window.addEventListener("resize", relayout);

	// FIXME: If another script decides to change this value
	// again, act accordingly.
	var oldVisibility = node.style.visibility;
	node.style.visibility = "hidden";
	port.postMessage({
	    type: "start_editor",
	    id: editorId,
            text: node.value,
            windowId: embed.windowId
	});

	editorCallbacks[editorId] = function (text) {
	    node.removeEventListener("DOMAttrModified", onAttrModified);
{'fraction_non_alphanumeric': 0.1465986394557823, 'fraction_numerical': 0.0035714285714285713, 'mean_word_length': 3.852310231023102, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1171069', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1707, 'n_words': 412}