Sunday, March 13, 2016

tackle 決心做某事 - make determined efforts to deal with (a problem or difficult task)

tackle 決心做某事 - make determined efforts to deal with (a problem or difficult task)

An aspiring software developer looking for their first job in the industry must be well-prepared to tackle the ubiquitous coding challenge, a common feature of the interview process.

Reference:

https://www.codementor.io/learn-programming/9-essential-tips-tackle-coding-challenge

Saturday, March 12, 2016

Why does this CSS margin-top style not work?

Solutions:

1. Set padding-top: 1px; to #outer

2. Set border-top: 1px solid transparent; to #outer

3. Set float: left; to either #outer or #inner

4. Set display: inline-block; to either #outer or #inner

5. Set overflow: auto; (or any value other than visible) to #outer.

Why does this CSS margin-top style not work?

You're actually seeing the top margin of the #inner element collapse into the top edge of the #outer element, leaving only the #outer margin intact (albeit not shown in your images). The top edges of both boxes are flush against each other because their margins are equal.

Here are the relevant points from the W3C spec:

8.3.1 Collapsing margins
In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin. Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin.

Adjoining vertical margins collapse [...]

Two margins are adjoining if and only if:

- both belong to in-flow block-level boxes that participate in the same block formatting context
- no line boxes, no clearance, no padding and no border separate them
- both belong to vertically-adjacent box edges, i.e. form one of the following pairs:
-- top margin of a box and top margin of its first in-flow child

The reason why doing any of the following prevents the margin from collapsing:

- Floating either of your div elements
- Making either of your div elements inline blocks
- Setting overflow of #outer to auto (or any value other than visible)

Is because:

- Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).
- Margins of elements that establish new block formatting contexts (such as floats and elements with 'overflow' other than 'visible') do not collapse with their in-flow children.
- Margins of inline-block boxes do not collapse (not even with their in-flow children).

The left and right margins behave as you expect because:

Horizontal margins never collapse.

Reference:

http://www.w3.org/TR/CSS21/box.html#collapsing-margins

http://stackoverflow.com/questions/9519841/why-does-this-css-margin-top-style-not-work

Friday, March 11, 2016

hone - refine or perfect (something) over a period of time.

Developers spend many years honing their craft. Some have languages thrust upon them, but those who reach Ninja level usually make their own choice based on a host of factors.

hone:

1. sharpen (a blade)
2. refine or perfect (something) over a period of time.

craft:

1. an activity involving skill in making things by hand.

Reference:

http://www.sitepoint.com/sitepoint-smackdown-php-vs-node-js/

Thursday, March 10, 2016

jQuery animate fired the callback twice

Animate would fire the callback twice because it calls its callback once for each element in the set you call animate on.

Because the animation works on body on some browsers but on html on other browsers. We can solve this issue by using .promise().then:

    $('html, body').animate({
      scrollTop: $('#test').offset().top,
    }, 400, function(){
      console.log('this will be fired twice');
    })
    .promise().then(function() {
      // Called when the animation in total is complete
      console.log('this will be fired once');
    });

Wednesday, March 9, 2016

Display a video from a Blob Javascript

<video autoplay controls poster="../images/poster.jpg" preload="metadata">Your browser does not support the video element</video>
<script src="js/main.js"></script>

js/main.js:

'use strict';

/* globals FileError */

// get video file via XHR
// store with File API
// read Blob from File API and set as video src using createObjectUrl()
// play video

var video = document.querySelector('video');

function getVideo(fileEntry) {
  get('../video/chrome.webm', function(uInt8Array) {
    var blob = new Blob([uInt8Array], {
      type: 'video/webm'
    });
    writeToFile(fileEntry, blob);
  });
}

function get(url, callback) {
  var xhr = new XMLHttpRequest();
  xhr.open('GET', url, true);
  xhr.responseType = 'arraybuffer';
  xhr.send();

  xhr.onload = function() {
    if (xhr.status !== 200) {
      alert('Unexpected status code ' + xhr.status + ' for ' + url);
      return false;
    }
    callback(new Uint8Array(xhr.response));
  };
}

// code adapted from HTML5 Rocks article by Eric Bidelman
// http://www.html5rocks.com/en/tutorials/file/filesystem/

// init a FileSystem
// create a file
// write to the file
// read from the file

window.requestFileSystem =
window.requestFileSystem || window.webkitRequestFileSystem;

window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, // 5MB
  handleInitSuccess, handleError);

function handleInitSuccess(fileSystem) {
  window.fileSystem = fileSystem;
  log('Initiated FileSystem: ' + fileSystem.name);
  createFile('video.webm');
}

function createFile(fullPath) {
  window.fileSystem.root.getFile(fullPath, {
    create: true
    /* exclusive: true */
  },
  function(fileEntry) {
    log('Created file: ' + fileEntry.fullPath);
    getVideo(fileEntry);
  }, handleError);
}

function writeToFile(fileEntry, blob) {
  // Create a FileWriter object for fileEntry
  fileEntry.createWriter(function(fileWriter) {
    fileWriter.onwriteend = function() {
      // read from file
      log('Wrote to file ' + fileEntry.fullPath);
      readFromFile(fileEntry.fullPath);
    };
    fileWriter.onerror = function(e) {
      log('Write failed: ' + e.toString());
    };
    // Create a new Blob and write it to file
    fileWriter.write(blob);
  }, handleError);
}

function readFromFile(fullPath) {
  window.fileSystem.root.getFile(fullPath, {}, function(fileEntry) {
    // Get a File object representing the file
    // then use FileReader to read its contents
    fileEntry.file(function(file) {
      var reader = new FileReader();
      reader.onloadend = function() {
        // video.src = this.result;
        video.src = URL.createObjectURL(new Blob([this.result]));
      };
      // reader.readAsDataURL(file);
      reader.readAsArrayBuffer(file);
    }, handleError);
  }, handleError);
}

function handleError(e) {
  switch (e.code) {
  case FileError.QUOTA_EXCEEDED_ERR:
    log('QUOTA_EXCEEDED_ERR');
    break;
  case FileError.NOT_FOUND_ERR:
    log('NOT_FOUND_ERR');
    break;
  case FileError.SECURITY_ERR:
    log('SECURITY_ERR');
    break;
  case FileError.INVALID_MODIFICATION_ERR:
    log('INVALID_MODIFICATION_ERR');
    break;
  case FileError.INVALID_STATE_ERR:
    log('INVALID_STATE_ERR');
    break;
  default:
    log('Unknown error');
    break;
  }
}

var data = document.getElementById('data');

function log(text) {
  data.innerHTML += text + '<br />';
}

document.querySelector('video').addEventListener('loadedmetadata', function() {
  var fileName = this.currentSrc.replace(/^.*[\\\/]/, '');
  document.querySelector('#videoSrc').innerHTML = 'currentSrc: ' + fileName +
  '<br /> videoWidth: ' + this.videoWidth + 'px<br /> videoHeight: ' + this
  .videoHeight + 'px';
});

Reference:

http://stackoverflow.com/questions/14317179/display-a-video-from-a-blob-javascript

http://simpl.info/video/offline/

https://github.com/samdutton/simpl/blob/gh-pages/video/offline/index.html

Monday, March 7, 2016

use Unicode in AutoHotKey

To use Unicode in AutoHotKey, save your script file to UTF-8 + Bom (UTF-8 won't work).

Wednesday, March 2, 2016

special permission setuid setgid sticky bit

Linux offers three types of special permission bits that may be set on executable files or directories to allow them to respond differently for certain operations:

setuid (set user identifier) bit:

When setuid bit is set on executable file at the file owner level, the file is executed by other regular users with the same privileges as that of the file owner.

# chmod 4755 test.sh

Or

# chmod u+s test.sh

# ls -l test.sh

-rwsr-xr-x. 1 root root 0 Jan 23 16:37 test.sh

# find . -perm -4000

./test.sh

setgid (set group identifier) bit:

When setgid attribute is set on executable files at the group level, the file is executed by non-owners with the exact same privileges that the group members have. The setgid bit can also be set on group-shared directories to allow files and sub-directories created in that directory to automatically inherit the directory's owning group.

# chmod 2555 test

Or

# chmod g+s test

# ls -ld test

dr-xr-sr-x. 2 root root 6 Jan 23 16:40 test

# find . -perm -2000

./test

sticky bit:

The sticky bit is set on public writable directories (or other directories with rw permission for everyone) to protect files and sub-directories owned by regular users from being deleted or moved by other regular users.

# chmod 1755 test

or

# chmod o+t test

# ls -ld test

drwxr-x--T. 2 root root 6 Jan 23 16:46 test

# find . -perm -1000

./test

Reference:

RHCSA & RHCE Red Hat Enterprise Linux 7: Training and Exam Preparation Guide (EX200 and EX300), Third Edition
http://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Dstripbooks&field-keywords=rhcsa+rhce+red+hat+