<commit_before>/*
 * Copyright 2011 eBay Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';
require('raptor-polyfill/string/startsWith');
var isObjectEmpty = require('raptor-util/isObjectEmpty');
var fs = require('fs');
var nodePath = require('path');

var markoWidgets = require('../');

function getWidgetNode(node) {
    if (!node) {
        return;
    }

    while (true) {
        if (node.qName === 'w-widget') {
            return node;
        }

        node = node.parentNode;
        if (!node) {
            break;
        }
    }
}

function getDefaultWidgetModule(dirname) {
    if (fs.existsSync(nodePath.join(dirname, 'widget.js'))) {
        return './widget';
    } else if (fs.existsSync(nodePath.join(dirname, 'index.js'))) {
        return './';
    } else {
        return null;
    }
}

exports.process =function (node, compiler, template) {
    var props = node.getProperties();


    if (!node._ts) {
        node._ts = Math.random();
    }

    var widgetTypes = [];

    function registerType(target) {
        var typePathExpression;
        var targetExpression;

        if (compiler.hasExpression(target)) {
            return '__markoWidgets.getDynamicClientWidgetPath(' + compiler.convertType(target, 'string', true) + ')';
        }

        // Resolve the static string to a full path at compile time
        typePathExpression = template.addStaticVar(target === './' ? '__widgetPath' : target, JSON.stringify(markoWidgets.getClientWidgetPath(target, template.dirname)));
        targetExpression = 'require(' + JSON.stringify(target) + ')';

        widgetTypes.push({
            name: typePathExpression,
            target: targetExpression
        });

        template.addStaticCode(function(writer) {
            writer.line('if (typeof window != "undefined") {');
            writer.incIndent();
            widgetTypes.forEach(function(registeredType) {
                writer.line('__markoWidgets.registry.register(' + registeredType.name + ', ' + registeredType.target + ');');
            });

            writer.decIndent();
            writer.line('}');
        });

        return typePathExpression;
    }

    var bind;

    if ((bind = props['w-bind']) != null) {

        if (bind === '') {
            bind = getDefaultWidgetModule(template.dirname);
            if (!bind) {
                node.addError('Unable to find default widget module when using w-bind without a value');
                return;
            }
        }

        template.addStaticVar('__markoWidgets', 'require("marko-widgets")');

        // A widget is bound to the node
        var widgetAttrsVar = template.addStaticVar('_widgetAttrs', '__markoWidgets.attrs');

        var typePathExpression = registerType(bind);

        var config;
        var assignedId;
        var scope;
        var id;

        var widgetNode = compiler.createTagHandlerNode('w-widget');
        node.parentNode.replaceChild(widgetNode, node);
        widgetNode.appendChild(node);

        widgetNode.setAttribute('module', typePathExpression);

        if ((config = props['w-config'])) {
            widgetNode.setProperty('config', config);
        }

        if ((assignedId = props['w-assigned-id'])) {
            widgetNode.setProperty('assignedId', assignedId);
        }

        if ((scope = props['w-scope'])) {
            widgetNode.setProperty('scope', scope);
        }

        if ((id = node.getAttribute('id'))) {
            id = compiler.convertType(id, 'string', true);
            widgetNode.setProperty('id', id);
        }

        node.setAttribute('id', '${widget.elId()}');

        node.addDynamicAttributes(template.makeExpression(widgetAttrsVar + '(widget)'));
    } else {
        var widgetId;
        var widgetExtend;
        var widgetElIdExpression;
        var widgetFor;

        var widgetArgs = {};

        if ((widgetId = props['w-id'])) {
            // Handle the "w-id" attribute
            delete props['w-id'];
            widgetArgs.id = widgetId;
        } else if ((widgetExtend = props['w-extend'])) {
            // Handle the "w-extend" attribute
            delete props['w-extend'];
            template.addStaticVar('__markoWidgets', 'require("marko-widgets")');
            widgetArgs.extend = registerType(widgetExtend);
            widgetArgs.extendConfig = template.makeExpression('data.widgetConfig');
        } else if ((widgetElIdExpression = props['w-el-id'])) {
            // Handle the "w-el-id" attribute
            if (node.hasAttribute('id')) {<commit_msg>Allow w-extend attr value to be empty<commit_after>/*
 * Copyright 2011 eBay Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';
require('raptor-polyfill/string/startsWith');
var isObjectEmpty = require('raptor-util/isObjectEmpty');
var fs = require('fs');
var nodePath = require('path');

var markoWidgets = require('../');

function getWidgetNode(node) {
    if (!node) {
        return;
    }

    while (true) {
        if (node.qName === 'w-widget') {
            return node;
        }

        node = node.parentNode;
        if (!node) {
            break;
        }
    }
}

function getDefaultWidgetModule(dirname) {
    if (fs.existsSync(nodePath.join(dirname, 'widget.js'))) {
        return './widget';
    } else if (fs.existsSync(nodePath.join(dirname, 'index.js'))) {
        return './';
    } else {
        return null;
    }
}

exports.process =function (node, compiler, template) {
    var props = node.getProperties();


    if (!node._ts) {
        node._ts = Math.random();
    }

    var widgetTypes = [];

    function registerType(target) {
        var typePathExpression;
        var targetExpression;

        if (compiler.hasExpression(target)) {
            return '__markoWidgets.getDynamicClientWidgetPath(' + compiler.convertType(target, 'string', true) + ')';
        }

        // Resolve the static string to a full path at compile time
        typePathExpression = template.addStaticVar(target === './' ? '__widgetPath' : target, JSON.stringify(markoWidgets.getClientWidgetPath(target, template.dirname)));
        targetExpression = 'require(' + JSON.stringify(target) + ')';

        widgetTypes.push({
            name: typePathExpression,
            target: targetExpression
        });

        template.addStaticCode(function(writer) {
            writer.line('if (typeof window != "undefined") {');
            writer.incIndent();
            widgetTypes.forEach(function(registeredType) {
                writer.line('__markoWidgets.registry.register(' + registeredType.name + ', ' + registeredType.target + ');');
            });

            writer.decIndent();
            writer.line('}');
        });

        return typePathExpression;
    }

    var bind;

    if ((bind = props['w-bind']) != null) {

        if (bind === '') {
            bind = getDefaultWidgetModule(template.dirname);
            if (!bind) {
                node.addError('Unable to find default widget module when using w-bind without a value');
                return;
            }
        }

        template.addStaticVar('__markoWidgets', 'require("marko-widgets")');

        // A widget is bound to the node
        var widgetAttrsVar = template.addStaticVar('_widgetAttrs', '__markoWidgets.attrs');

        var typePathExpression = registerType(bind);

        var config;
        var assignedId;
        var scope;
        var id;

        var widgetNode = compiler.createTagHandlerNode('w-widget');
        node.parentNode.replaceChild(widgetNode, node);
        widgetNode.appendChild(node);

        widgetNode.setAttribute('module', typePathExpression);

        if ((config = props['w-config'])) {
            widgetNode.setProperty('config', config);
        }

        if ((assignedId = props['w-assigned-id'])) {
            widgetNode.setProperty('assignedId', assignedId);
        }

        if ((scope = props['w-scope'])) {
            widgetNode.setProperty('scope', scope);
        }

        if ((id = node.getAttribute('id'))) {
            id = compiler.convertType(id, 'string', true);
            widgetNode.setProperty('id', id);
        }

        node.setAttribute('id', '${widget.elId()}');

        node.addDynamicAttributes(template.makeExpression(widgetAttrsVar + '(widget)'));
    } else {
        var widgetId;
        var widgetExtend;
        var widgetElIdExpression;
        var widgetFor;

        var widgetArgs = {};

        if ((widgetId = props['w-id'])) {
            // Handle the "w-id" attribute
            delete props['w-id'];
            widgetArgs.id = widgetId;
        } else if ((widgetExtend = props['w-extend']) != null) {
            if (widgetExtend === '') {
                widgetExtend = getDefaultWidgetModule(template.dirname);
                if (!widgetExtend) {
                    node.addError('Unable to find default widget module when using w-extend without a value');
                    return;
                }
            }

            // Handle the "w-extend" attribute
            delete props['w-extend'];
            template.addStaticVar('__markoWidgets', 'require("marko-widgets")');
            widgetArgs.extend = registerType(widgetExtend);
            widgetArgs.extendConfig = template.makeExpression('data.widgetConfig');
        } else if ((widgetElIdExpression = props['w-el-id'])) {
            // Handle the "w-el-id" attribute
            if (node.hasAttribute('id')) {
{'fraction_non_alphanumeric': 0.12924071082390953, 'fraction_numerical': 0.001520478950869524, 'mean_word_length': 2.303201506591337, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3491567', 'n_tokens_mistral': 2854, 'n_tokens_neox': 2783, 'n_words': 686}