<commit_before>import React from 'react';
import settings from 'settings';
import domOnlyProps from 'form/domOnlyProps';
import fieldIsInvalid from 'form/fieldIsInvalid';
import getLinkInputErrors from 'form/getLinkInputErrors';
import wrapPetitionLinks from 'helpers/wrapPetitionLinks';
import Icon from 'components/Icon';
import ExternalTeaser from 'components/ExternalTeaser';
import styles from './petition-links-field.scss';

const PetitionLinksField = React.createClass({
  getClassname (element, error) {
    return [
      styles[element || 'input'],
      styles[error ? 'invalid' : 'valid']
    ].join(' ');
  },

  getInitialState: () => ({ value: '' }),

  componentWillMount () {
    const { helper } = this.props;
    const links = wrapPetitionLinks(helper.value);
    helper.onChange(links);
  },

  handleChange (e) { this.setState({ value: e.target.value }); },

  handleLinkAdded (e) {
    const { helper, config } = this.props;
    helper.touched = true;
    helper.onBlur();

    if (e.key === 'Enter' || e.type === 'blur') {
      // Disable form submitting when pressing ENTER
      e.preventDefault();

      const value = this.state.value.trim();
      let links = helper.value;

      if (value) {
        // Remove any protocol from the URL. In the teaser, the URL will be
        // displayed without the protocol, and the link to it will be a relative
        // protocol URL.
        const PROTOCOL_REGEX = new RegExp(/^(https?:\/\/)/i);
        const protocolFreeURL = value.replace(PROTOCOL_REGEX, '');

        // Specific validation for the link field.
        const error = getLinkInputErrors(protocolFreeURL, links, config);

        if (error) {
          helper.error = error;
          return;
        }

        helper.error = false;

        links.push({ url: protocolFreeURL });
        helper.onChange(links); // Update redux-form value
        this.setState({ value: '' }); // Clear input value

        // Fetch open graph data for the last link
        this.props.fetchOpenGraph(protocolFreeURL).then(({ openGraph }) => {
          const newValue = { url: protocolFreeURL, og: openGraph };<commit_msg>Fixed similar links with www not being detected<commit_after>import React from 'react';
import settings from 'settings';
import domOnlyProps from 'form/domOnlyProps';
import fieldIsInvalid from 'form/fieldIsInvalid';
import getLinkInputErrors from 'form/getLinkInputErrors';
import wrapPetitionLinks from 'helpers/wrapPetitionLinks';
import Icon from 'components/Icon';
import ExternalTeaser from 'components/ExternalTeaser';
import styles from './petition-links-field.scss';

const PetitionLinksField = React.createClass({
  getClassname (element, error) {
    return [
      styles[element || 'input'],
      styles[error ? 'invalid' : 'valid']
    ].join(' ');
  },

  getInitialState: () => ({ value: '' }),

  componentWillMount () {
    const { helper } = this.props;
    const links = wrapPetitionLinks(helper.value);
    helper.onChange(links);
  },

  handleChange (e) { this.setState({ value: e.target.value }); },

  handleLinkAdded (e) {
    const { helper, config } = this.props;
    helper.touched = true;
    helper.onBlur();

    if (e.key === 'Enter' || e.type === 'blur') {
      // Disable form submitting when pressing ENTER
      e.preventDefault();

      const value = this.state.value.trim();
      let links = helper.value;

      if (value) {
        // Remove any protocol from the URL. In the teaser, the URL will be
        // displayed without the protocol, and the link to it will be a relative
        // protocol URL.
        const PROTOCOL_REGEX = new RegExp(/^(https?:\/\/)?(www\.)?/i);
        const protocolFreeURL = value.replace(PROTOCOL_REGEX, '');

        // Specific validation for the link field.
        const error = getLinkInputErrors(protocolFreeURL, links, config);

        if (error) {
          helper.error = error;
          return;
        }

        helper.error = false;

        links.push({ url: protocolFreeURL });
        helper.onChange(links); // Update redux-form value
        this.setState({ value: '' }); // Clear input value

        // Fetch open graph data for the last link
        this.props.fetchOpenGraph(protocolFreeURL).then(({ openGraph }) => {
          const newValue = { url: protocolFreeURL, og: openGraph };
{'fraction_non_alphanumeric': 0.12687427912341406, 'fraction_numerical': 0.0, 'mean_word_length': 2.8922800718132855, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6975674', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1203, 'n_words': 355}